【发布时间】:2015-06-26 23:14:11
【问题描述】:
根据这个答案Change attribute after each object calling,我认为调用方法应该在每次调用时对其实例做一些事情。我试图在我的代码中使用它,但我在某个地方错了。
class switcher():
current_index = -1
def __call__(self):
self.current_index +=1
sw = switcher()
print sw.current_index
print sw.current_index
输出: -1
输出: -1
我认为它应该返回这个:
输出: 0
输出: 1
因为每次调用 sw 实例时它都会增加current_index 的值。
显然我错了,请你告诉我问题出在哪里?
【问题讨论】:
-
你永远不会在这段代码中调用任何切换器实例。
标签: python class oop call init