Order of constructor/destructor invocation

Cliff Wells logiplexsoftware at earthlink.net
Tue Mar 5 14:04:15 EST 2002


On Tue, 05 Mar 2002 12:49:05 -0500
Justin Sheehy wrote:

> "Reginald B. Charney" <news at charneyday.com> writes:
> > Basically, I need to know if the order of destruction is guaranteed to
be
> > the reverse of construction when reference counts on the objects are
the
> > same (e.g., 1 on this case). Without this guarantee, I don't understand
how
> > Python can effectively use destructors - it falls into the same hole as
> > Java. For me, this is a show-stopper.
> 
> This makes no sense.

Actually it makes perfect sense.  He's asking if, when the reference counts
are all equal (in this case 1), the objects will be destroyed in the
reverse order they were created when the reference counts drop.

However, I agree that depending upon the order of destruction of an object
is a bad idea, and worse, won't work at all under Python.  I would consider
this sort of behaviour to be a side-effect and something to be avoided. 
While in the particular example given it looks like an elegant solution
(and would be, if that were the extent of the application - which I doubt
;), this type of logic in anything more complex is going to be at least
somewhat non-obvious and has no real benefit as far as conciseness (he
could avoid using the destructor altogether and simply have an end() method
which he calls explicitly - more obvious at the cost of 3 LOC).

I also think his assertion that Python can't effectively use destructors is
a bit overstated.  What he's really saying is he can't use Python
destructors in the fashion he intends.  However, I've always considered
destructors to be a method for cleaning up /after/ an object, not finishing
its function, which Python seems to do well enough.  I consider using a
method that claims to do one thing (in this case, destruction of an object)
to sneak in something unrelated is a bad idea.
 
Regards,

-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308




More information about the Python-list mailing list