[Python-Dev] Closures / Python Scopes (original) (raw)
Hatem Nassrat hnassrat at gmail.com
Fri Nov 12 21:08:42 CET 2010
- Previous message: [Python-Dev] r86418 - in python/branches/release27-maint: Doc/library/difflib.rst Lib/difflib.py Lib/test/test_difflib.py Misc/NEWS
- Next message: [Python-Dev] Closures / Python Scopes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
A colleague of mine came across something anecdotal when working with lambdas, it is expressed by the following code snippet.
In [1]: def a(): ...: for i in range(10): ...: def b(): ...: return i ...: yield b ...: ...:
In [2]: funcs = list(a())
In [3]: print [f() for f in funcs] [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
I understand that for loops in python do not have a scope, neither do if statements, and python is awesome for that. Is this something accidental? i.e. will python ever evolve into having scopes for if and for loops (and other blocks that are not functions)? the reason I ask is with the introduction of http://docs.python.org/py3k/reference/simple_stmts.html#nonlocal it seems like something that can happen.
-- Hatem Nassrat
- Previous message: [Python-Dev] r86418 - in python/branches/release27-maint: Doc/library/difflib.rst Lib/difflib.py Lib/test/test_difflib.py Misc/NEWS
- Next message: [Python-Dev] Closures / Python Scopes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]