class A:
def x ( self ):
print( self.__class__ )
class B ( A ):
pass
b = B()
b.x()
In the above situation, is there any way for the method x to get a reference to class A instead of B? Of course simply writing print( A ) is not allowed, as I want to add some functionality with a decorator that needs the class A (and as I can’t pass A itself to the decorator directly, as the class doesn’t exist yet at that point).
Unless you have
super-calls involved (or more generally subclasses that overridexand call directly toA.x(self)as part of their override’s implementation), looking for the first item intype(self).mro()that has anxattribute would work —If you do need to cover for
super-calls, it’s harder — unless you happen to know that no super-class ofAdefines anxattribute (and of course it’s easier to know about your superclasses than your subclasses, though multiple inheritance does complicate it;-) in which case you only need the last appropriate item of themroinstead of the first one.