I need your opinion on the following code:
def __getattr__( Self , Attribute ):
print( f"[ WARNING ] The Specified Attribute '{Attribute}' Could Not be Found !" )
# -----------------------------------------------------------------------------------------------------------
def __getattribute__( Self , Attribute ):
CLS = type( Self )
if Attribute == '__mro__' : return type( Self ).__mro__
if Attribute.startswith( '__' ) and Attribute.endswith( '__' ) :
return object.__getattribute__( Self, Attribute )
# ---------------------------------------------------------------
instance_dict = object.__getattribute__( Self, '__dict__' )
if Attribute in instance_dict :
return instance_dict[ Attribute ]
# ---------------------------------------------------------------
if Attribute in CLS.__dict__ :
attr = CLS.__dict__[ Attribute ]
print( f"[ DEBUG ] CLS : Found Attribute '{attr}'" )
# print( dir( attr ) )
# print( object.__getattribute__( attr , '__dir__' ) )
if '__get__' in dir( attr ) and callable( attr.__get__ ) :
print( f"[ DEBUG ] CLS : Found Descriptor For Attribute '{attr}'" )
return attr.__get__( Self , CLS )
return attr
# ---------------------------------------------------------------
for base in CLS.__mro__:
if Attribute in base.__dict__:
attr = base.__dict__[ Attribute ]
if '__get__' in dir( attr ) and callable( attr.__get__ ):
print( f"[ DEBUG ] MRO : Found Descriptor For Attribute '{attr}'" )
return attr.__get__( Self , CLS )
return attr
return Self.__getattr__( Attribute )
As you can see, I have tried to recreate the __getattribute__
mechanism.
Der Test durch Ausgabe der verschiedenen Variablentypen funktioniert:
print( T.__class__ )
print( T.__mro__ )
print( T.Examples_Variables_X ) # Instance Variable
print( T.attr_A ) # Base Variable
print( T.Test_Method ) # Base Method
print( T._Instance ) # Class Variable
Is it correct or are there any suggestions for improvement?
8