Order of constructor/destructor invocation

Justin Sheehy justin at iago.org
Thu Mar 7 11:12:13 EST 2002


"Reginald B. Charney" <news at charneyday.com> writes:

> Brett is exactly right. I am using an approach that is valid and elegant in
> C++.

Since elegance is largely a matter of opinion, I'll simply disagree on
that point.  Destructors and related methods generally ought to be
used for object cleanup related to their reclamation, not for doing
additional new processing.  Abusing them in this fashion feels deeply
inelegant to me, especially since there are so many straightforwward
ways to solve the problem without resorting to such means.

Elegance aside, your approach is invalid regardless of the
programming language.

You cannot create a correct HTML-writing program in the style you
demonstrated, given the structure of HTML.  Your linear style simply
will not work for anything beyond trivial examples, since HTML is
fundamentally hierarchical.

As another poster mentioned, your method breaks down very quickly.
Your style would not work for this fairly simple document because the
structure is unclear:

-----------------------------
h = HTML()
b = BODY()
l = LIST()
le = LISTELEMENT()
p = PARAGRAPH()
-----------------------------

This will not work because you must specify both the start and end of
a tag in this sort of language.  It cannot be usefully inferred from
any linear structure.  This has little to do with Python, and will
break just as badly in any language.

A few examples of alternate strawman methods that might work:

-----------------------------
h = HTML() 
b = BODY() 
l = LIST() 
le = LISTELEMENT() 
p = PARAGRAPH() 
h.start()
b.start()
l.start()
le.start()
le.end()
l.end()
p.start()
p.end()
b.end()
h.end()
-----------------------------
HTMLstart()
BODYstart()
LISTstart()
LISTELEMENTstart()
LISTELEMENTend()
LISTend()
PARAGRAPHstart()
PARAGRAPHend()
BODYend()
HTMLend()
----------------------------- 
HTML(BODY(LIST(LISTELEMENT()), PARAGRAPH()))
-----------------------------  

The first two of these are not quite as concise as yours, but unlike
yours all three are able to express the needed structure to correctly
generate HTML.

> While Justin is correct - there are a number of packages out there to
> generate HTML, I was writing an article and trying to use Python for the
> simple examples in the article. (The article was not about Python, per se).
> I had to remove all references to Python when I found that the order of
> destruction was non-deterministic.

This doesn't follow.  There are plenty of simple ways to do what you
describe that work without trying to misuse the methods designed to
aid with garbage collection.

> It is interesting that destruction/garbage collection (something
> usually done after everything has been completed) has so profound
> effect on the design of programs and thus, implementation of
> algorithms.

It wouldn't have nearly as much effect if you took heed of your own
parenthetical note and realized that all of an object's interesting
work should be done before the destructor, as opposed to inside the
destructor.

I apologize if my tone here is a bit confrontational, but that is
largely due to the fact that you immediately began blaming Python for
not doing what you want in spite of an incorrect design.

-Justin

 







More information about the Python-list mailing list