【发布时间】:2014-03-13 20:46:31
【问题描述】:
我有以下代码
import numpy as np
class Estimator(object):
name = None
def __init__(self):
self.__call__ = self._call
class Mean(Estimator):
name = 'mean'
def _call(self, data):
return np.mean(data)
data = np.arange(10)
现在,为什么我不能将第二个类用作函子作为第一个类?
它似乎有效:
M = Mean()
print M.__call__(data) # -> 4.5
M 有方法__call__:
print '__call__' in dir(M) # -> True
但它不起作用
print M(data)
我明白了:
TypeError: 'Mean' object is not callable
【问题讨论】:
-
特殊方法在类而不是实例中查找。
标签: python inheritance callable