Order of constructor/destructor invocation

Duncan Booth duncan at NOSPAMrcp.co.uk
Fri Mar 8 06:00:13 EST 2002


"Reginald B. Charney" <news at charneyday.com> wrote in
news:R29h8.5988$aP6.6654 at rwcrnsc54: 

> Brett is exactly right. I am using an approach that is valid and
> elegant in C++. Since I am fairly new to Python, I had assumed it
> handled contructors/destructors in an ordered (i.e. deterministic)
> fashion. It seems not.

May I just point out that, although the order of destruction of stack based 
variables is well defined in C++, the way you are using the destructors is 
most certainly not elegant. Destructors are called either when the block 
exits normally, or when an exception is raised. If a C++ destructor ever 
throws an exception when in the exception unwinding state then the world 
comes to an end (i.e. your program is immediately terminated). For this 
reason, something apparently as innocuous as printing a string should never 
be done from inside a C++ destructor without a lot of safety nets.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list