[Tutor] debugging classes with a __setattr__ method
Lloyd Kvam
lkvam@venix.com
Mon, 01 Apr 2002 17:29:37 -0500
I have a class for processing DataBase records that keeps a dictionary of the
actual field values from the database in a separate dictionary from the
Class's __dict__ dictionary. When I try to step through the program using the
debugger, I can no longer step into the called methods. Deleting the
__setattr__ method restores normal debugging capabilities.
Is this a normal side effect on the debugger? Very little of the code depends
on the __setattr__ method, so I can remove it, debug and then put it back.
However, there may be a better way. Any suggestions?
class Record:
def __getattr__(self, key):
""" This is called if the object lacks the attribute.
If the key is not found, it raises an error in the
same way that the attribute access would were no
__getattr__ method present.
"""
try:
return self._record[key]
except:
raise AttributeError, "%s not in _record dictionary" % (key)
def __setattr__(self, key, value):
if hasattr(self, "_record") and self._record.has_key(key):
self._record[key] = value
else:
self.__dict__[key] = value
def __delattr__(self, key):
if hasattr(self, "_record") and self._record.has_key(key):
raise AttributeError, "Must not delete record attributes. %s in _record." % (key)
elif self.__dict__.has_key(key):
del self.__dict__[key]
else:
raise AttributeError, "delete non-existing instance attribute: %s" % (key)
--
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358
voice:
603-443-6155
fax:
801-459-9582