Whenever I see a decorator in python, I always see that it uses function definitions
def logger(func):
def inner(*args, **kwargs):
print "Arguments were: %s, %s" % (args, kwargs)
return func(*args, **kwargs)
return inner
@logger
def func(x, y=1):
return x * y
func(2)
What would be an equivalent way of doing what the decorator does when defining a function, but allows you to call it when you call the function so that the function can be used with and without a “decorator”?
(Below I hope, and although I do not think that you could do this with a decorator, I use @to show this idea)
def func(x, y=1):
return x * y
func(2)
@logger
func(2)
source
share