◐ Shell
clean mode source ↗

Message 79664 - Python tracker

Hello. Sorry if this is noise. I expected

__del__
out of function
__del__
out of function
__del__
out of function

on following code, but actually I got

out of function
out of function
out of function
__del__
__del__
__del__

Is this expected behavoir? (I believed `a' would be
freed after returned from f(), so I was suprised)

If I remove the comment of gc.collect(), the code works as expected.

///////////////////////////////

import gc

class A:
    def __del__(self):
        print("__del__")

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

def main():
    for _ in range(3):
        f()
#       gc.collect()
        print("out of function")

if __name__ == '__main__':
    main()