Reputation: 5162
Using the decorator module from Michele Simionato, I'm getting a weird behavior with respect to kwargs. Here's an example using the simple example that is in the documentation:
def _trace(f, *args, **kw):
kwstr = ', '.join('%r: %r' % (k, kw[k]) for k in sorted(kw))
print("calling %s with args %s, {%s}" % (f.__name__, args, kwstr))
return f(*args, **kw)
def trace(f):
return decorate(f, _trace)
@trace
def f(a, b, c=1, d=1):
return a + b + c + d
f(1,2,c=2,d=2)
prints calling f with args (1, 2, 3, 4), {}
instead of calling f with args (1, 2), {'c': 2, 'd': 2}
Is it wanted behavior? Why so?
Or did I get something wrong?
Upvotes: 0
Views: 112
Reputation: 1544
This seems to be the expected behavior. From the page you linked:
>>> @trace
... def f(x, y=1, z=2, *args, **kw):
... pass
>>> f(0, 3)
calling f with args (0, 3, 2), {}
>>> print(getargspec(f))
ArgSpec(args=['x', 'y', 'z'], varargs='args', varkw='kw', defaults=(1, 2))
Notice how the y
and z
keyword arguments show up as regular args, not keyword args.
Upvotes: 1
Reputation: 3741
Expectation is not correct with current prototype of f(a, b, c=1, d=1), It is just default argument type. Change f to accept keyargs-
@trace
def f(a, b, **kargs):
return a + b + kargs['c'] + kargs['d']
f(1,2,c=2,d=2)
calling f with args (1, 2), {'c': 2, 'd': 2}
Upvotes: 0