在Python中你可以调用实例通过实现__call__
方法。例如当我应该实现__call__
class Blah:
def __call__(self):
print "hello"
obj = Blah()
obj()
但我也能实现自己的方法,说“跑”:
class Blah:
def run(self):
print "hello"
obj = Blah()
obj.run()
我什么时候应该实现__call__
?
当它是有道理的。 – 2012-04-23 20:39:50
如果您不确定是否需要'__call__',那么您不需要'__call__'。 – 2012-04-23 20:47:08