Nested decorators? (Python) -


i've discovered can decorate function in 2 different ways achieve same "decoration" on function:

def greeting(expr):     def greeting_decorator(func):         def function_wrapper(x):             print(expr + ", " + func.__name__ + " returns:")             func(x)         return function_wrapper     return greeting_decorator  def foo(x):     return x  greeting2 = greeting("good morning!") foo = greeting2(foo) 

or...

def greeting_decorator2(func, expr):     def function_wrapper(x):         print(expr + ", " + func.__name__ + " returns:")         print(func(x))     return function_wrapper  def foo(x):     return x  foo = greeting_decorator2(foo, "good morning!") 

note first example can changed use "at"-notation:

@greeting("good morning!") def foo(x):     return x 

but using at-notation second decorator function example causes crash, , understandably so.

my question is, 1 of these methods of including additional arguments decorator function apart function wrapped preferred? second 1 seems easier wrap one's head around, , possibly more scalable if 1 needs more additional arguments. there downsides using it?


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -