```$ python Python 2.7.11 (default, Jan 22 2016, 0...
# general
b
Copy code
$ python
Python 2.7.11 (default, Jan 22 2016, 08:29:18)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> for x in range(20):
...   def foo():
...     print x
...   foos.append(foo)
...
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
NameError: name 'foos' is not defined
>>> foos = []
>>> for x in range(20):
...   def foo():
...     print x
...   foos.append(foo)
...
>>> foos
[<function foo at 0x10677f320>, <function foo at 0x10677f2a8>, <function foo at 0x10677f398>, <function foo at 0x10677f410>, <function foo at 0x10677f488>, <function foo at 0x10677f500>, <function foo at 0x10677f578>, <function foo at 0x10677f5f0>, <function foo at 0x10677f668>, <function foo at 0x10677f6e0>, <function foo at 0x10677f758>, <function foo at 0x10677f7d0>, <function foo at 0x10677f848>, <function foo at 0x10677f8c0>, <function foo at 0x10677f938>, <function foo at 0x10677f9b0>, <function foo at 0x10677fa28>, <function foo at 0x10677faa0>, <function foo at 0x10677fb18>, <function foo at 0x10677fb90>]
>>> for foo in foos: foo()
...
19
19
19
19
19