On 7/10/07, Jacob Lee <[EMAIL PROTECTED]> wrote:


Closures have existed in Python since version 2.1 or so:
def f():
        x = 5
        return lambda: x
closure = f()
print closure() # prints 5

Here, the anonymous inner function returned by f is able to refer to
variables defined in outer scopes.


Oh, nice!  This is did not know.  So then are closures available now in IP?

As for the Python 3000 question --
The one current limitation is that you cannot rebind names defined in
outer scopes.


To me that's a feature, not a limitation. ;-)

That is, the following code does not work as expected:

def f():
        x = 5
        def g():
                x = 7 # x is local to g here

You could use the "global" statement to indicate that x is a global
despite it being assigned to inside the function, but there was no
equivalent way to indicate that x refers to a variable in an outer, but
non-global, scope. Python 3000 will introduce the "nonlocal" statement that
works like the global statement to fill this gap. As usual, the best source
is the relevant PEP: http://www.python.org/dev/peps/pep-3104/

Hope this helps.


It does.  Thanks!

--
/M:D

M. David Peterson
http://mdavid.name | http://www.oreillynet.com/pub/au/2354 |
http://dev.aol.com/blog/3155
_______________________________________________
users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to