object reincarnation

Terry Reedy tjreedy at udel.edu
Wed Jun 10 16:36:27 EDT 2009


Manavan wrote:
> 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.

It might be easier to give the class a status variable so instances can 
be de-activated without destroying info and perhaps re-activated.

> 
> class Deleted(object):
>     pass
> 
> 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