Message167437
> 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? |
|
Date |
User |
Action |
Args |
2012-08-04 20:36:09 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, sbt |
2012-08-04 20:36:08 | serhiy.storchaka | link | issue15528 messages |
2012-08-04 20:36:07 | serhiy.storchaka | create | |
|