scoping weirdness

Paul Rubin phr-n2001 at nightsong.com
Sat Aug 25 02:02:52 EDT 2001


    from __future__ import nested_scopes   # python 2.1.1

    def foo (n):
        a = lambda: 'you said (%d)' % n
        n += 3
        return a

    x = foo(1)
    print x()

This prints 4, rather than 1 as I would have expected.
I think I understand what's going on, but is it really what was intended?
Is there a preferred way of writing this kind of function?

Thanks.



More information about the Python-list mailing list