Python closures and stack frames

I use closures quite a lot in python, but the variable lifetime semantics of closures are undocumented as far as I can tell. For example, consider the following code:

    def f():
        def g():
            return b
        return g

If we call ‘f’ and store the value, ‘b’ will be kept alive. Is ‘a’ kept alive? A naive implementation of closures might store a reference to the entire stack frame of ‘f’ inside the ‘g’ closure, causing ‘a’ to live even though it will never be used. This would be very bad, since generating a closure inside a long function could potentially cause a large memory leak. I wouldn’t expect this since python is implemented by smart people, but it’s worth checking.

Happily, it appears the variable lifetime semantics are as expected. Here’s a test program:

    class A(object):
        def __init__(self,name):
            print 'creating',name
        def __del__(self):
            print 'destroying',

    def f():
        def g():
            print 'inspecting',
        def h():
            print 'inspecting',
        return g,h

    print 'only b and c should be alive'
    del h
    print 'only b should be alive'
    del g
    print 'done'

The output of this program in CPython is

    creating a
    creating b
    creating c
    destroying a
    only b and c should be alive
    destroying c
    only b should be alive
    inspecting b
    destroying b

All variables die at the correct times, so CPython appears to have correct lifetime semantics in the presence of closures.

comments powered by Disqus