赞
踩
>>> li
=
[
"Larry"
,
"Curly"
]
>>> li.pop
<built
-
in
method pop of
list
object
at
0xb76b364c
>
>>>
getattr
( li,
"pop"
)
<built
-
in
method pop of
list
object
at
0xb76b364c
>
>>>
getattr
( li,
"append"
)(
"Moe"
)
>>> li
[
'Larry'
,
'Curly'
,
'Moe'
]
>>>
|
从上面的代码可以看出li.pop 等用于 getattr( li, "pop" ),但是这样不是调用pop函数,真正的
的调用是getattr( li, "append" )("Moe")。
回到我们的stateful代码,old__getattr__ = getattr(cls,'__getattribute__'),当__getattr__中传来name参数,就相当于执行了cls.__getattribute__.name,如果查找name属性失败,pass这个异常,继续进行getattr(curr(self),name),这里curr(host)返回的是Workday类和Weekday类,name也是day,然后检查f是否是callable的,然后return f的偏函数,传入一个参数self,最后替换cls里的__init__和__getattr__,返回cls。Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。