Have look at the instance attributes:
A.__dict__ {'paramA': 2, '_messenger': <__main__.Messenger instance at 0x100468cf8>}
A._messenger.__dict__ {'subA': <__main__.SubClassA instance at 0x100468d40>,
'subB': <__main__.SubClassB instance at 0x100468d88>,
'paramA': 6, 'param1': 1, 'paramB': 3}
Python always looks in the instance dict first. It finds 'paramA'
in A.__dict__
.
If it would not be there it would go up to the class and would call your __getattr__
because you force it with __getattribute__
.
Accordingly, preventing the setting into the instance dict would change the behavior.
When you write self.paramA = total
you call this method:
def __setattr__(self,attr,val):
if attr == '_messenger':
self.__dict__[attr] = val
#otherwise, set the attribute and sync the messenger
else:
setattr(self._messenger,attr,val)
# comment out the following line to get your 6
self.__dict__[attr] = val
The very last line self.__dict__[attr] = val
puts the attribute into the instance dict.
Comment out this line and you get the output you expect. If this makes sense for the problem at hand is a different question though.