I'm trying to access all arguments passed to a function in the decorator, including optional arguments. Consider this example:
def decorator(fn):
def wrapper(*args, **kwargs):
print 'in wrapper', args, kwargs
fn(*args, **kwargs)
return wrapper
@decorator
def myFn(arg1, arg2, arg3=None):
print 'in myFn', arg1, arg2, arg3
myFn(1,2)
myFn(1,2,3)
If I run this, I'll get:
in wrapper (1, 2) {}
in myFn 1 2 None
in wrapper (1, 2, 3) {}
in myFn 1 2 3
In the first run, since I don't specify 3 arguments, arg3 is defined as None for the purposes of myFn. But the fact that arg3 == None
is not available inside of decorator, either in args or kwargs. If I explicitly pass it to myFn, it'll show up inside the decorator, but if I use the default value, it's nowhere to be found.
Why is this? And how can it be fixed?