When a higher order function returns a new function, a new context has been wrapped around the existing code object. The code of that function is created once, at compile time. There is absolutely no compiler involved at runtime, no new byte code is generated.
>>> def foo(n):
... def bar():
... print n
... return bar
...
>>> a,b = foo(1), foo(2)
>>> a == b
False
>>> a()
1
>>> b()
2
>>> a.__code__ == b.__code__
True
MAGICAL MIRACLES
ReplyDeleteAlso, perused this today:
http://hg.python.org/cpython/file/default/Lib/fractions.py#l211
It's neet.