[Python-Dev] recursive closures - reference leak

Kristján Valur Jónsson kristjan at ccpgames.com
Tue Dec 8 13:45:52 CET 2009

Hello there.

Consider this code:

def factorial(n):
                def helper(n):
                               if n:
                                               return n*helper(n-1)
                                               return 1
                return helper(n)

Ok, this is a bit contrived, but there are other valid reasons for calling a closure recursively.
The problem with this is that once you have called factorial() once, you end up with a recursive cycle.  "factorial" has become a cell object, referencing the "helper" function, which again refers to the outer cell object.  This requires "gc" to clean up.  Also, it is entirely non-obvious.  the problem becomes worse if the inner function also refers to some large, temporary variable, since it will get caught up in the reference loop.

A workaround is this.  Replace the final line with:
                               return helper(n)
                                helper = None

This clears the cell before function exit.  Note that it is not possible to "del" it, you get a syntax error for that.

Now, the reason I'm posting this here, and not to python-lang, is this:  Is there a way to implement  this differently so that this pattern doesn't result in a cycle?
Possible ideas would be:

1)      clear "helper" on function exit.  Not good if the internal function is exported, though.

2)      Use have 'helper' store a weakref to the cell object

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20091208/8a2b56b2/attachment.htm>

More information about the Python-Dev mailing list