[Python-Dev] Garbage collecting closures
Guido van Rossum
Mon, 14 Apr 2003 12:08:04 -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
> From: Jeremy Hylton <email@example.com>
> 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.
I don't see the reason for this semantic restriction. IMO it could
just as well be a runtime error (e.g. raising UnboundLocalError).
--Guido van Rossum (home page: http://www.python.org/~guido/)