参数如何通过 __getattr__ 传递给函数 [英] How are arguments passed to a function through __getattr__
问题描述
考虑以下代码示例(python 2.7):
class 父类:def __init__(self, child):self.child = 孩子def __getattr__(self, attr):print("调用__getattr__:"+attr)如果 hasattr(self.child, attr):返回 getattr(self.child, attr)别的:引发 AttributeError(attr)班级儿童:def make_statement(self, age=10):print("我是一个Child with age "+str(age))孩子 = 孩子()人 = 父母(孩子)Kid.make_statement(5)person.make_statement(20)
可以看出,函数调用person.make_statement(20)
通过Parent
的调用了Child.make_statement
函数__getattr__
函数.在 __getattr__
函数中,我可以在调用子实例中的相应函数之前打印出属性.到此为止很清楚.
但是调用 person.make_statement(20)
的参数是如何通过 __getattr__
传递的?如何在 __getattr__
函数中打印出数字20"?
您没有在 __getattr__
函数中打印 20
.该函数在 Child 实例上找到 make_statement
attribute 并返回它.碰巧,该属性是一个方法,因此它是可调用的.Python因此调用返回的方法,然后那个方法打印20
.
如果你删除了 ()
调用,它仍然可以工作;我们可以存储该方法并单独调用它以打印20
:
如果您必须查看参数,则必须返回一个包装函数:
def __getattr__(self, attr):print("调用__getattr__:"+attr)如果 hasattr(self.child, attr):def 包装器(*args, **kw):print('用 %r 和 %r' % (args, kw))返回 getattr(self.child, attr)(*args, **kw)返回包装器引发 AttributeError(attr)
现在结果:
<预><代码>>>>person.make_statement(20)调用 __getattr__:make_statement使用 (20,) 和 {} 调用我是 20 岁的 Child 的一个实例Consider the following code example (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)
it can be shown, that the function call person.make_statement(20)
calls the Child.make_statement
function through the Parent
's __getattr__
function. In the __getattr__
function I can print out the attribute, before the corresponding function in the child instance is called. So far so clear.
But how is the argument of the call person.make_statement(20)
passed through __getattr__
? How am I able to print out the number '20' in my __getattr__
function?
You are not printing 20
in your __getattr__
function. The function finds the make_statement
attribute on the Child instance and returns that. As it happens, that attribute is a method, so it is callable. Python thus calls the returned method, and that method then prints 20
.
If you were to remove the ()
call, it would still work; we can store the method and call it separately to get 20
printed:
>>> person.make_statement
Calling __getattr__: make_statement
<bound method Child.make_statement of <__main__.Child instance at 0x10db5ed88>>
>>> ms = person.make_statement
Calling __getattr__: make_statement
>>> ms()
I am an instance of Child with age 10
If you have to see the arguments, you'd have to return a wrapper function instead:
def __getattr__(self, attr):
print("Calling __getattr__: "+attr)
if hasattr(self.child, attr):
def wrapper(*args, **kw):
print('called with %r and %r' % (args, kw))
return getattr(self.child, attr)(*args, **kw)
return wrapper
raise AttributeError(attr)
This now results in:
>>> person.make_statement(20)
Calling __getattr__: make_statement
called with (20,) and {}
I am an instance of Child with age 20
这篇关于参数如何通过 __getattr__ 传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!