[Baypiggies] What is destructor expected behavior?

Glen Jarvis glen at glenjarvis.com
Wed Feb 27 21:08:09 CET 2008


Summary: Can I assume, 100% of the time, that a destructor is called  
before a program ends?

If not, what is the design pattern to "time" issues in Python  
automatically at program begin time/end time? Futher granularity into  
the object level would be very helpful and preferred.


Details:


I have built the design of my current project around what I thought  
was “expected behavior.” I’m now starting to question my original  
beliefs that this is expected behavior. I come from a C++ background  
where things are allocated and de-allocated manually. I sometimes  
make things harder in Python because I don’t know how it’s “supposed”  
to do things (which has always been much easier than I made it).

My question is when the destructor for an object is called. I had  
always believed that the destructor would be called for any object at  
least by program end. In fact, this is what occurs in the following  
test case to demonstrate:

In my test case that I just built, I get “good” behavior. This is the  
behavior I have expected from C++ and is the assumption I walked into  
this design with.  This is not the behavior I see in my current  
project. So, either
1)       this is not an accurate test case,
2)       we can never expect this behavior to be guaranteed, or
3)       I have a mistake in my code that this test case does not  
reflect.

This can be boiled down to one single question “What should I expect  
from Python?” I’m casting a wider net so I can design around what is  
expected in Python - without just hacking about...

Test case follows:


import datetime
import time


class X:
     def __init__(self):
         self._job_begin_time = datetime.datetime.now()
         print "My job has started at %s " % self._job_begin_time

     def __del__(self):
         self._job_end_time = datetime.datetime.now()
         print "My job has ended at %s " % self._job_end_time
         print "Job took this much time: %s" % (self._job_end_time -
                                                self._job_begin_time )

     def waste_some_time(self):
         time.sleep(5.0)

print "Starting job X"
myX = X()
myX.waste_some_time()
print "Ending job X"



Here is the output…

[gjarvis at gjarvis cron_util]$ python testcase.py
Starting job X
My job has started at 2008-02-27 11:47:59.319781
Ending job X
My job has ended at 2008-02-27 11:48:04.320517
Job took this much time: 0:00:05.000736
[gjarvis at gjarvis cron_util]$ python testcase.py
Starting job X
My job has started at 2008-02-27 11:48:06.307024
Ending job X
My job has ended at 2008-02-27 11:48:11.307496
Job took this much time: 0:00:05.000472
[gjarvis at gjarvis cron_util]$ python testcase.py
Starting job X
My job has started at 2008-02-27 11:48:13.343558
Ending job X
My job has ended at 2008-02-27 11:48:18.344515
Job took this much time: 0:00:05.000957
[gjarvis at gjarvis cron_util]$ python testcase.py
Starting job X
My job has started at 2008-02-27 11:48:20.219613
Ending job X
My job has ended at 2008-02-27 11:48:25.220510
Job took this much time: 0:00:05.000897


Warmest Regards,


Glen Jarvis

--
415-680-3964
glen at glenjarvis.com
http://www.glenjarvis.com

"You must be the change you wish to see in the world." -M. Gandhi


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/baypiggies/attachments/20080227/63d7ae6b/attachment.htm 


More information about the Baypiggies mailing list