考虑以下代码:
ff = lambda x : x
gg = lambda x : x*2
class FunctionCollection(object):
f = ff
g = gg
def FunctionCaller(x, Type = FunctionCollection.f):
return Type(x)
y = FunctionCaller(x)
它返回一个
unbound method () must be called with FunctionCollection instance as first argument (got ndarray instance instead)
我不明白的信息。 一个显而易见的解决方案是在FunctionCollection中定义ff和gg,但是我想知道是否不能在模块中定义ff和gg,然后创建一个包含指向这些函数的“指针”的枚举,最后将这些“指针”作为参数传递。抱歉给你起了个C风格的名字。你知道吗
怎么了?你知道吗
谢谢你
迈克
您的代码实际上在python3.x上工作。如果您需要支持python2.x,可以使用staticmethod:
另一种选择是使用
FunctionCollection
作为“单例”,并将函数绑定到单个实例。。。你知道吗我认为这是一个显而易见的问题。。。为什么你需要这种额外的间接层次?对我来说似乎没有必要。你知道吗
当然,这并不是说没有必要,只是因为我还不明白目的……
相关问题 更多 >
编程相关推荐