在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