[Tutor] nested function definition
Tue Apr 29 10:33:09 2003
On Tue, Apr 29, 2003 at 09:03:58AM -0500, Norvell Spearman wrote:
> On Tuesday, 2003.04.29, 10:39:18 +0200, Kristoffer Erlandsson wrote:
> > The most obvious uses for this is to define small "fire and forget" functions
> > that you use once inside an other function.
> > The advantages of this is that if you need a function somewhere (for example as
> > argument to an other function) you can easily define it locally, use it and
> > forget about it and you don't have to clutter the global namespace. The
> > disadvantage (if you can call it that) is that the function isn't usable or
> > visible outside the enclosing function.
> Does this also mean that garbage collection will take out a
> locally-defined function earlier than a globally-defined one?
I don't exactly know how the garbage collection in Python works, but if it
works as I think it does the function is garbed sometime after the call
to the outer function is finished. You have nothing referring to the
function then and hence no reason to keep it. This is assuming you haven't
returned the function and are referencing it from outside the function in
That is, the answer is yes if everything works like I think it does (and as
I've seen in other languages) and nothing fishy is going on :)