[Python-Dev] Garbage collecting closures

Jeremy Hylton jeremy@zope.com
14 Apr 2003 11:58:35 -0400

On Mon, 2003-04-14 at 11:52, Phillip J. Eby wrote:
> If I understand correctly, it should also be breakable by deleting 'foo' 
> from the outer function when you're done with it.  E.g.:
> def bar(a):
>       def foo():
>           return None
>           x = a
>           foo()
>       del foo   # clears the cell and breaks the cycle

You haven't tried this, have you? ;-)

SyntaxError: can not delete variable 'foo' referenced in nested scope

Since foo() could escape bar, i.e. become reachable outside of bar(), we
don't allow you to unbind foo.