def createCounter():
n = 0
def counter():
nonlocal n
n = n + 1
return n
return counter
# 测试:
counterA = createCounter()
print(counterA(), counterA(), counterA(), counterA(), counterA()) # 1 2 3 4 5
counterB = createCounter()
if [counterB(), counterB(), counterB(), counterB()] == [1, 2, 3, 4]:
print('测试通过!')
else:
print('测试失败!')
python返回函数
最新推荐文章于 2024-07-27 23:02:02 发布