understanding generators and functions with arguments
Scott David Daniels
Scott.Daniels at Acm.Org
Mon Oct 11 22:38:05 EDT 2004
caw at southernhealth.org.au wrote:
> def msg(text):
> def decorate(f):
> def new_f(*args):
> print text, f(*args)
> return new_f
> return decorate
>
> @msg("Hello, ")
> def f1(s):
> return s
...
> I don't understand the scope of *args, as seen in the argument list of
> new_f. It doesn't appear to be in the static scope of msg, or decorate
> or new_f...
OK, I would probably write this as:
def msg(text):
def decorate(f):
def new_f(*args, **kwargs):
print text, f(*args, **kwargs)
return new_f
return decorate
That is, the *args in your example, and my "*args, **kwargs" are both
meant to represent the parameters passed to the "wrapped" function f.
My example would allow a line in the main section:
f1(s=42)
while your form would fail on that call.
-Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list