[Python-ideas] For-loop variable scope: simultaneous possession and ingestion of cake

Bruce Leban bruce at leapyear.org
Sat Oct 4 01:28:06 CEST 2008


I don't think the for loop is broken. If you want scopes other than global
and function, then you should add that explicitly, maybe something like:
a = []
for i in range(10):
  local k:
    k = i**2
    a.append(lambda: k)
  # k is not accessible here

a[3]() => 9

which is roughly equivalent to:

a = []
for i in range(10):
  def local_k():
    k = i**2
    a.append(lambda: k)
  local_k()

--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20081003/a7730a6b/attachment.html>


More information about the Python-ideas mailing list