__del__ pattern?

Michael Hudson mwh at python.net
Mon Aug 15 11:58:09 EDT 2005


"Chris Curvey" <ccurvey at gmail.com> writes:

> I need to ensure that there is only one instance of my python class on
> my machine at a given time.

I recommend modifying your requirements such that you ensure that
there is only one "active" instance of your class at any one time (or
something like that), and then use try:finally: blocks to ensure your
locks get removed.

> Is there a better pattern to follow than using a __del__ method?  I
> just need to be absolutely, positively sure of two things:
>
> 1) There is only one instance of my special class on the machine at a
> time.
> 2) If my special class is destroyed for any reason, I need to be able
> to create another instance of the class.

As another poster mentioned, you also need to work out what you're
going to do if your process gets killed in a way that doesn't allow
finally blocks to run (this doesn't have much to do with Python).

Cheers,
mwh

-- 
  The above comment may be extremely inflamatory. For your
  protection, it has been rot13'd twice.
                           -- the signature of "JWhitlock" on slashdot



More information about the Python-list mailing list