make objetcs 'commit suicide'

Chris Tavares christophertavares at earthlink.net
Sun Jan 12 17:43:37 EST 2003


"polux" <polux2001 at wanadoo.fr> wrote in message
news:avrv71$m2q$2 at news-reader10.wanadoo.fr...
> class a:
> def go(self):
> self.__del__            # or del(self)
>
>
> and then
> b=a()
> b.go()
>
>
> b is not destructed
>

Python is not C++. And del is not the same as delete.

del removes a name from the current namespace. Objects go away when there
are no more references to them. In your go() method, del(self) simply
removes the name "self" from your current namespace. However, the calling
namespace still has "b" pointing at the same object, so the object's still
alive.

You need to think about objects more like you would in a garbage collected
language. Objects don't have destructors, they have finalizers, that will
get called "eventually". If you have cleanup that MUST happen, put it in its
own method and call it explicitly.

Yes, I know it's hard to get used to this coming from C++ - it took me quite
a while to come to grips with it. But once you do, programming gets a LOT
easier (and not just in Python).

-Chris







More information about the Python-list mailing list