[英]__getattr__ equivalent for methods
When an attribute is not found object.__getattr__
is called. 当找不到属性object.__getattr__
被调用。 Is there an equivalent way to intercept undefined methods? 是否存在拦截未定义方法的等效方法?
There is no difference. 没有区别。 A method is also an attribute. 方法也是属性。 (If you want the method to have an implicit "self" argument, though, you'll have to do some more work to "bind" the method). (但是,如果您希望该方法具有隐式“自”参数,则必须执行更多工作来“绑定”该方法)。
Methods are attributes too. 方法也是属性。 __getattr__
works the same for them: __getattr__
对他们的作用相同:
class A(object):
def __getattr__(self, attr):
print attr
Then try: 然后尝试:
>>> 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
you didn't return anything. 你没有退货。
class A(object):
def __getattr__(self, attr):
return attr
should work 应该管用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.