请实现函数 new_counter ,使得调用结果如下:
c1 = new_counter(10)
c2 = new_counter(20)
print c1(), c2(), c1(), c2()
outputs :
11 21 12 22
我的想法:
def new_counter(n):
class Counter(object):
def __init__(self, num):
self.num =num
def __call__(self):
self.num += 1
return self.num
return Counter(n)