object reincarnation

M

Manavan

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):
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

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Deleted' object has no attribute 'attrs'
 
T

Terry Reedy

Manavan said:
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.
 
A

alex23

Since the real world objects often needs to be deleted even if they
have some reference from some other object [...]

From this it sounds like you're trying to implement some form of weak
referencing. Are you familiar with weakref?

"A weak reference to an object is not enough to keep the object alive:
when the only remaining references to a referent are weak references,
garbage collection is free to destroy the referent and reuse its
memory for something else. A primary use for weak references is to
implement caches or mappings holding large objects, where it’s desired
that a large object not be kept alive solely because it appears in a
cache or mapping."

http://docs.python.org/library/weakref.html
 
C

Carl Banks

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):
    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


<__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'

Cute, but let me suggest that changing the class of the object would
make it harder to track down the original error. So just delete the
attributes. I'd also recommend using self.__dict__.clear() for that,
it gets all of them (usually) and doesn't need to be updated with you
add a new attribute to a class.


Carl Banks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,687
Latest member
IngridXxj

Latest Threads

Top