Scoping: Is Python and does it matter?

Cliff Crawford cjc26 at nospam.cornell.edu
Sun May 28 08:10:36 EDT 2000


* noone <nanotech at pacifier.com> menulis:
| 
| In Perl, the second 'print_X' would succeed because 'x' would be found in
| the outer scope. Python scopes the var to the function due to the
| assignment (it seems). One could use 'global', but then the assignment
| would affect the module var (where in Perl, the assignment might be
| my($x)=2, in which case the "global" x would not be modified).

Well as you know Python only has two scopes, global and local.  If you
want nested scopes you can use a class instead:

class incr_by:
    def __init__(self, incr):
        self.incr = incr
    def __call__(self, num):
        return num + self.incr

print incr_by(4)(5)   ==>  9

The class incr_by is like a curried function.


-- 
cliff crawford    -><-    http://www.people.cornell.edu/pages/cjc26/
                          Synaesthesia now!            icq 68165166



More information about the Python-list mailing list