from functools import wraps
def logged(func):
@wraps(func)
def with_logging(*args, **kwargs):
print func.__name__ + " was called"
return func(*args, **kwargs)
return with_logging
@logged
def f(x):
"""does some math"""
return x + x * x
print f.__name__ # prints 'f'
print f.__doc__ # prints 'does some math'
Given this sample code, how would I be able to do @logged(variable)
?
I tried this
from functools import wraps
def logged(func):
def outer(var):
@wraps(func)
def with_logging(*args, **kwargs):
print func.__name__ + " was called"
return func(*args, **kwargs)
return with_logging
return outer
I was hoping to execute like this: logged(func)(session_variable)
But doesn't work. Any idea? I want to be able to do @logged and @logged(var) ( or even @logged(var1, var2)) Thanks.