您应该使用装饰器,以便清楚地说明您打算做什么:
class call_three_times(object): def __init__(self, func): self.func = func self.count = 0 def __call__(self, *args, **kw): self.count += 1 if self.count <= 3: return self.func(*args, **kw)@call_three_timesdef func(): print "Called only three times"func() # prints "Called only three times"func() # prints "Called only three times"func() # prints "Called only three times"func() # does nothing



