Advertisement
lessientelrunya

Function with args and decorator

Apr 6th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. # It’s not black magic, you just have to let the wrapper
  2. # pass the argument:
  3.  
  4. def a_decorator_passing_arguments(function_to_decorate):
  5.     def a_wrapper_accepting_arguments(arg1, arg2):
  6.         print("I got args! Look: {0}, {1}".format(arg1, arg2))
  7.         function_to_decorate(arg1, arg2)
  8.     return a_wrapper_accepting_arguments
  9.  
  10. # Since when you are calling the function returned by the decorator, you are
  11. # calling the wrapper, passing arguments to the wrapper will let it pass them to
  12. # the decorated function
  13.  
  14. @a_decorator_passing_arguments
  15. def print_full_name(first_name, last_name):
  16.     print("My name is {0} {1}".format(first_name, last_name))
  17.  
  18. print_full_name("Felicity", "Smoak")
  19. # outputs:
  20. #I got args! Look: Felicity Smoak
  21. #My name is Felicity Smoak
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement