understanding generators and functions with arguments

caw at southernhealth.org.au caw at southernhealth.org.au
Mon Oct 11 21:00:40 EDT 2004


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...

Could someone help me with this...

Thanks

chris wright



More information about the Python-list mailing list