Simple Closure Example : nested function « Function « Python Tutorial






def counter(start_at=0):
     count = [start_at]
     def incr():
         count[0] += 1
         return count[0]
     return incr

count = counter(5)
print count()
print count()
count2 = counter(100)
print count2()
print count()








10.12.nested function
10.12.1.Number of Scopes
10.12.2.Simple Closure Example
10.12.3.Scope and lambda
10.12.4.Nested functions.