# 代码1 正确
def make_counter(init):
counter = [init]
def inc(): counter[0] += 1
def get(): return counter[0]
return inc, get
increase, gain = make_counter(5)
increase()
print(gain())
# 代码2 报错
def make_counter(init):
counter = init
def inc(): counter += 1
def get(): return counter
return inc, get
increase, gain = make_counter(5)
increase()
print(gain())
为什么代码1没有问题,而代码2却报错?