Order of constructor/destructor invocation

Reginald B. Charney news at charneyday.com
Fri Mar 8 04:42:52 EST 2002


Thanks Justine,

I don't feel that your tone is confrontational. You have expressed a view,
reflected by many others here, that my approach is not appropriate for
Python. While this is a small sample of respondents, it may be
representative, so the style may not be acceptable in the sense that it is
not idiomatic for this language.

That said, let me comment on your example. The use of block structures can
define scope so that your example:

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

could be written, using my approach, as:

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

Believe it or not, I was not trying to start a religious war. I was trying
to use a well-defined approach in one language to another language that I
thought was similar enough in structure to work in the same way. However, as
Brett g Porter (an earlier poster) said, "Down that path lies madness."
Thanks to you all who were good enough to respond to my question and my
apologies if I started out to strongly or offended anyone.

Reg.

"Justin Sheehy" <justin at iago.org> wrote in message
news:mailman.1015517608.2847.python-list at python.org...
> "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