未找到属性时object.__getattr__
调用。是否有等效的方法来拦截未定义的方法?
问问题
6025 次
3 回答
11
没有区别。方法也是属性。(但是,如果您希望该方法具有隐式的“self”参数,则必须做更多的工作来“绑定”该方法)。
于 2010-10-04T12:04:24.093 回答
8
方法也是属性。__getattr__
对他们同样有效:
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
于 2010-10-04T12:08:28.247 回答
0
你没有返回任何东西。
class A(object):
def __getattr__(self, attr):
return attr
应该管用
于 2013-04-06T04:02:59.690 回答