次のコード例(python 2.7)について考えてみます。
class Parent:
def __init__(self, child):
self.child = child
def __getattr__(self, attr):
print("Calling __getattr__: "+attr)
if hasattr(self.child, attr):
return getattr(self.child, attr)
else:
raise AttributeError(attr)
class Child:
def make_statement(self, age=10):
print("I am an instance of Child with age "+str(age))
kid = Child()
person = Parent(kid)
kid.make_statement(5)
person.make_statement(20)
関数呼び出しがperson.make_statement(20)
のChild.make_statement
関数を介して関数を呼び出すことを示すことができます。関数では、子インスタンスの対応する関数が呼び出される前に、属性を出力できます。これまでのところ明確です。Parent
__getattr__
__getattr__
しかし、呼び出しの引数はどのようにperson.make_statement(20)
渡され__getattr__
ますか?関数で数値「20」を出力するにはどうすればよいです__getattr__
か?