I am trying to wrap my head around how __getattribute__
and __getattr__
work in Python with respect to uninstantiated classes and subclassing. Specifically I want to capture a getattribute
call in the super class and potentially modify it accordingly.
In the following code snippet, I would have expected at least some intermediate print statements when any of the methods are called, but I am not getting anything. The class variable is found and returned but seemingly none of the magic methods are called. Why is that?
class SuperbClass:
def __getattribute__(self, attr):
print(f"called in superb, found {attr}")
# get the attribute via `object`
return f"{super(SuperbClass, self).__getattribute__(attr)}MODIFIED_IN_SUPER"
def __getattr__(self, attr):
self.__getattribute__(attr)
class SubberClass(SuperbClass):
MyClassVar = "I am a cool message"
def __getattribute__(self, attr):
print("called in subber")
super(SuperbClass, self).__getattribute__(attr)
def __getattr__(self, attr):
self.__getattribute__(attr)
if __name__ == '__main__':
print(SubberClass.MyClassVar)
# Only prints `I am a cool message`
inst = SubberClass()
print(inst.MyClassVar)
# prints called in subber
# and prints None
EDIT: updated code to reflect the difference between class and instance