object reincarnation

Manavan manavan.j at gmail.com
Wed Jun 10 15:34:56 EDT 2009

Hello everyone,
   Since the real world objects often needs to be deleted even if they
have some reference from some other object, I am going to use this
approach to better model this situation, by cleaning up the attributes
and assigning self.__class__ to a different class.
 Any comment on this approach.

class Deleted(object):

class RealWorldObj(object):
    def __init__(self, *args):
        self.attrs = args
    def getAttrs(self,):
        return self.attrs
    def delete(self,):
        del self.attrs
        self.__class__ = Deleted

>>> a = RealWorldObj(1,2,3)
>>> print a.attrs
(1, 2, 3)
>>> a.delete()
>>> a
<__main__.Deleted object at 0x893ae2c>
>>> a.attrs
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Deleted' object has no attribute 'attrs'

More information about the Python-list mailing list