New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Object lifetime and inner recursive function #49171
Comments
Hello. Sorry if this is noise. I expected __del__ on following code, but actually I got out of function Is this expected behavoir? (I believed `a' would be 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() |
Since g calls "itself" in its own scope, it is stored as one of its own If g didn't keep a reference to its cell vars, closures wouldn't be (g doesn't have to be recursive, it's enough that it makes a reference def f():
a = A()
def g():
a
g or even: def f():
a = A()
def g():
a
h
h = g ) |
Thank you for explanation. The combination of inner function + method |
A little followup. Attached script q112.py (some puzzle program) ate up |
Well, tracking memory consumption of each container would be better than |
Would it be possible for function self-reference cell vars to be weak references? This wouldn't solve the issue for co-recursive inner functions, but would at least prevent reference cycles for the more common case of simple recursive functions. |
For anyone doing archaeology - this came up on python-dev about a year after this issue was filed: https://mail.python.org/pipermail/python-dev/2009-December/094439.html |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: