A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2003-April/034596.html below:

[Python-Dev] Garbage collecting closures

[Python-Dev] Garbage collecting closures [Python-Dev] Garbage collecting closuresSamuele Pedroni pedronis@bluewin.ch
Mon, 14 Apr 2003 22:37:34 +0200
At 12:32 14.04.03 -0700, Paul Prescod wrote:

>Buggy:
>
>def b():
>     def a():
>         if something:
>             a()
>     a()
>     # ten thousand lines of code
>     junk = com_object.do_something()

a should refer and close over junk otherwise nothing bad happens.

 >>> class X:
...  def __del__(self):
...   print "dying"
...
 >>> def b(x):
...  def a(n):
...    if n: a(n-1)
...  a(1)
...  junk = x
...
 >>> b(X())
dying

vs.

 >>> def b(x):
...   def a(n):
...    if n: a(n-1)
...    junk
...   junk = x
...
 >>> b(X())
 >>> gc.collect()
dying
10

regards 




RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4