I have a collection of user-created objects in my Python application.
If the user deletes one of these objects, for the purposes of error checking only, I'd like to flag it as having been deleted, such that any attempts to use that object in future result in an error.
I considered some options:
del
: this deletes the reference and leaves any dangling references intact.__dict__.clear()
this gives a confusingAttributeError
when the object is accessed, and removes all the information which may be helpful in tracking the object down
I also considered overriding __getattribute__
however, changing this seems to be ineffective.
class MyClass:
def __init__( self ):
self.a = 1
self.b = 2
self.c = 3
m = MyClass()
print( m.a )
print( m.b )
print( m.c )
def fn_deleted( self, attribute ):
raise ValueError( "This object should have been deleted :(" )
m.__getattribute__ = fn_deleted
print( m.a ) # <-- NO ERROR
I feel there should be a simple way of checking for dangling references. Any ideas?