I have been studying Python for a little while now, and I've come to understand that overriding __setattr__
correctly can be troublesome (to say the least!).
What are some effective ways to ensure/prove to myself the override has been done correctly? I'm specifically concerned about ensuring the override remains consistent with the descriptor protocol and MRO.
(Tagged as Python 3.x since that's what I am using, but the question is certainly applicable to other versions as well.)
Example code in which the "override" exhibits default behavior (but how can I prove it?):
class MyClass():
def __setattr__(self,att,val):
print("I am exhibiting default behavior!")
super().__setattr__(att,val)
Contrived example in which the override violates the descriptor protocol (instance storage lookup occurs prior to the descriptor lookup - but how can I test it?):
class MyClass():
def __init__(self,mydict):
self.__dict__['mydict'] = mydict
@property
def mydict(self):
return self._mydict
def __setattr__(self,att,val):
if att in self.mydict:
self.mydict[att] = val
else:
super().__setattr__(att, val)
The ideal answer will provide a general test that will succeed when __setattr__
has been overridden correctly, and fail otherwise.
In this case there's a simple solution: add a binding descriptor with a name that's in
mydict
and test that assigning to that name goes thru the descriptor (NB : Python 2.x code, I don't have a Python 3 install here):