does python have useless destructors?

Donn Cave donn at drizzle.com
Sat Jun 12 10:54:39 EDT 2004


Quoth Duncan Booth <me at privacy.net>:
| "Roger Binns" <rogerb at rogerbinns.com> wrote in
| news:q3ipp1-4bp.ln1 at home.rogerbinns.com: 
...
|> But for this thread, the actual problem is that __del__ methods may
|> not be called on objects, and if you read the doc implies they almost
|> never will be called.  So we have the counter-intuitive (and IMHO user
|> hostile) situation where someone marks a class as needing extra code
|> to run to destroy it, and Python addresses that by saying you will
|> be lucky if the object will be destroyed (ie by not destroying the
|> object).
|
| No, Python guarantees that the object will (almost always) be destroyed. It 
| just doesn't make guarantees about when that will happen. If you want 
| guarantees about resources being released you have to write the code to do 
| that yourself (e.g. with try..finally). The same applies for other 
| languages such as Java, or languages running in Microsoft's .Net framework. 

Er, Python guarantees that the object will almost always be destroyed?
What the heck does that mean?

| Guaranteeing that all resources will be released at a specific time has 
| implications for performance, and finalisers are actually pretty rare in 
| practice, so the usual solution is to compromise.

It isn't clear to me what it takes to guarantee immediate finalization,
so if it is to you, that might be worth spelling out a little more.
It doesn't cost anything in the ordinary case, I mean with C Python's
reference count storage model, if you get it, you get it for free.

As for whether finalizers are rare in practice, of course they would be
in languages like Java where they're evidently about useless, and maybe
in Python where they're of limited value and more or less deprecated.
It's kind of a circular argument for leaving it broken, though.  (Maybe
broken is the wrong word - finalization really does work.  It can take
some extra care to make it work reliably, but that extra attention to
releasing exception tracebacks, avoiding cycles, etc., could be not
altogether a bad thing.)

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list