nested functions

Fredrik Lundh fredrik at pythonware.com
Sat Apr 15 05:11:41 EDT 2006


Lawrence D'Oliveiro wrote:

> >BUT 'b' and 'c' simply do not exist outside the 'a' world.
>
> It's worth distinguishing between the _names_ 'b' and 'c' and the
> _functions_ referred to by those names. The _names_ certainly do not
> exist outside of the scope of the function referred to by 'a' (any
> occurrences of 'b' and 'c' outside that scope refer to _different_
> names), but the _functions_ they refer to certainly do exist.

that's a bit misleading.  "def" is an executable statement, and it
*creates* a function object when it's executed.  that object is
handled in exactly the same way as any other object.

in the following example, the function referred to by "c" doesn't
exist before a call to "a", and it doesn't exist after the function
has returned:

    def a():
      def c():
        print "c"
      c()

if you call the function again, a new function object is created.

</F>






More information about the Python-list mailing list