__getattr__方法等效 [英] __getattr__ equivalent for methods
本文介绍了__getattr__方法等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
找不到属性时,将调用object.__getattr__
.是否有等效的方法来拦截未定义的方法?
When an attribute is not found object.__getattr__
is called. Is there an equivalent way to intercept undefined methods?
推荐答案
方法也是属性. __getattr__
对它们的作用相同:
Methods are attributes too. __getattr__
works the same for them:
class A(object):
def __getattr__(self, attr):
print attr
然后尝试:
>>> a = A()
>>> a.thing
thing
>>> a.thing()
thing
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
这篇关于__getattr__方法等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文