Antoine Pitrou <pit...@free.fr> added the comment:

Since g calls "itself" in its own scope, it is stored as one of its own
cell vars, which creates a reference cycle. a is also part of its
reference cycle for the same reason, so it must wait for garbage
collection to be reclaimed.

If g didn't keep a reference to its cell vars, closures wouldn't be
possible, because the cell vars wouldn't survive the end of f's scope.

(g doesn't have to be recursive, it's enough that it makes a reference
to itself in its own scope:

def f():
    a = A()
    def g():
        a
        g

or even:

def f():
    a = A()
    def g():
        a
        h
    h = g

)

----------
nosy: +pitrou

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue4921>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to