# 利用闭包返回一个计数器函数,每次调用它返回递增整数: ''' def createCounter(): x = int(0) def counter(): nonlocal x x += 1 return x 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('测试失败!') ''' # 请用匿名函数改造下面的代码: # def is_odd(n): # return n % 2 == 1 # L = list(filter(is_odd, range(1, 20))) ''' L = list(filter(lambda x: x%2 == 1, range(1, 20))) '''
廖雪峰python3 返回函数 && 匿名函数练习题
猜你喜欢
转载自blog.csdn.net/wangzhuo0978/article/details/79835507
今日推荐
周排行