Closures in python

Hallvard B Furuseth (nospam nospam) h.b.furuseth at usit.uio.no
Thu Sep 18 08:12:36 EDT 2003


'closure' is a much-abused word.
This is a closure over foo's x variable:

def foo():
  x = 3
  def bar():
    x += 1
    return x
  return bar

f = foo()
print f()  # print 4
g = foo()
print f()  # print 5
print g()  # print 4

...or it would be, if it worked.

-- 
Hallvard




More information about the Python-list mailing list