understanding generators and functions with arguments

Terry Reedy tjreedy at udel.edu
Tue Oct 12 20:57:56 EDT 2004


<caw at southernhealth.org.au> wrote in message 
news:c6dd4e35.0410111700.2be6c24c at posting.google.com...
> this is the smallest bit of code I could get to demonstrate what I
> want to understand...
>
> 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
>
>
> if __name__ == '__main__':
>    f1("world!")
>
>
> ~/src/python caw$ python dec_play.py
> Hello,  world!
>
> OK... so I can give a decorator and argument, and that will then
> return the function <decorate>. This expects one argument (and is a
> decorator (?)). When decorate is called with f1 as the argument, it
> returns new_f, which, when called will print <text> (passed to the
> original decorator), together with the result of calling f1 with its
> args.
>
> 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...

As with all parameters, 'args' is a local variable in the local namespace 
of  the function it is a parameter for, in this case new_f.  I presume you 
would call that 'local scope'.  I do not know what you mean by 'static 
scope' since that is not a term usually used in describing Python.

Terry J. Reedy






More information about the Python-list mailing list