Bug or feature?

Ignacio Vazquez-Abrams ignacio at openservices.net
Sun Sep 2 02:13:13 EDT 2001


On Sun, 2 Sep 2001, Gerson Kurz wrote:

> funcs = []
> def a():
>     print "code 1"
> funcs.append(a)
> def a():
>     print "code 2"
> funcs.append(a)
> for func in funcs: func()

This works because def doesn't just define a function; it actually creates a
function object. append()ing the function object to the list increments the
reference count of the original function object so that even though a is
redefined, the original function object still exists and can be accessed
through the list.

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>





More information about the Python-list mailing list