This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients sbt, serhiy.storchaka
Date 2012-08-04.20:36:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <201208042335.53774.storchaka@gmail.com>
In-reply-to <1344108271.0.0.326102479203.issue15528@psf.upfronthosting.co.za>
Content
> finalize does not "compete" with contextlib.ExitStack, atexit and
> weakref.WeakKeyDictionary.  It only competes with __del__ and weakref
> callbacks.

What kind of problems you solve with __del__  and weakref callbacks? For most 
of them contextlib.ExitStack and atexit are safer and better fit.

> Points 1 and 2 in my first message are the main points.  Also, read the
> warning at

For point 1: global weakref.WeakKeyDictionary is good store for weak refs with 
callbacks. 

global weakdict
weakdict[kenny] = weakref.ref(kenny, lambda _: print("you killed kenny!"))

If you do not want to work at a low level, in most cases fit the above-
mentioned high-level tools.

For point 2 Antoine has noted that it is a known issue and there is a solution 
(issue812369).

>   http://docs.python.org/py3k/reference/datamodel.html#object.__del__
> 
> which also applies to weakref callbacks.

Is this your point 2?

> Other problems with __del__:
> 
> * Ref cycles which contain an object with a __del__ method are immortal

It is use case for weakrefs which break reference loops.

> * __del__ methods can "ressurect" the object.

What you are meaning?

Relying on GC is dangerous thing for resource releasing. And it even more 
dangerous for alternative implementations without reference counting. That is 
why in recent times in Python there has been a tendency to RAII strategy 
(context managers).

Can you give examples, where the use of finalize necessary or more convenient 
the other ways?
History
Date User Action Args
2012-08-04 20:36:09serhiy.storchakasetrecipients: + serhiy.storchaka, sbt
2012-08-04 20:36:08serhiy.storchakalinkissue15528 messages
2012-08-04 20:36:07serhiy.storchakacreate