|
发表于 2022-6-16 02:25:22
|
显示全部楼层
这里有视频讲解 -> https://fishc.com.cn/thread-207567-1-1.html
所谓闭包(closure),也有人称之为工厂函数(factory function)。
举个例子:
- >>> def power(exp):
- ... def exp_of(base):
- ... return base ** exp
- ... return exp_of
- ...
- >>> square = power(2)
- >>> cube = power(3)
- >>> square
- <function power.<locals>.exp_of at 0x000001CF6A1FAF70>
- >>> square(2)
- 4
- >>> square(5)
- 25
- >>> cube(2)
- 8
- >>> cube(5)
- 125
复制代码
这里 power() 函数就像是一个工厂,由于参数不同,得到了两个不同的 “生产线”,一个是 square(),一个是 cube(),前者是返回参数的平方,后者是返回参数的立方。
在游戏开发中,我们需要将游戏中角色的移动位置保护起来,不希望被其他函数轻易就能够修改,所以我们就可以利用闭包:
- origin = (0, 0) # 这个是原点
- legal_x = [-100, 100] # 限定x轴的移动范围
- legal_y = [-100, 100] # 限定y轴的移动范围
- # 好,接着我们定义一个create()函数
- # 初始化位置是原点
- def create(pos_x=0, pos_y=0):
- # 然后我们定义一个实现角色移动的函数moving()
- def moving(direction, step):
- # direction参数设置方向,1为向右或向上,-1为向左或向下,如果是0则不移动
- # step参数是设置移动的距离
- # 为了修改外层作用域的变量
- nonlocal pos_x, pos_y
- # 然后我们真的就去修改它们
- new_x = pos_x + direction[0] * step
- new_y = pos_y + direction[1] * step
- # 检查移动后是否超出x轴的边界
- if new_x < legal_x[0]:
- # 制造一个撞墙反弹的效果
- pos_x = legal_x[0] - (new_x - legal_x[0])
- elif new_x > legal_x[1]:
- pos_x = legal_x[1] - (new_x - legal_x[1])
- else:
- pos_x = new_x
- # 检查移动后是否超出y轴边界
- if new_y < legal_y[0]:
- pos_y = legal_y[0] - (new_y - legal_y[0])
- elif new_y > legal_y[1]:
- pos_y = legal_y[1] - (new_y - legal_y[1])
- else:
- pos_y = new_y
- # 将最终修改后的位置作为结果返回
- return pos_x, pos_y
- # 外层函数返回内层函数的引用
- return moving
复制代码
程序实现如下:
- >>> move = create()
- >>> print("向右移动20步后,位置是:", move([1, 0], 20))
- 向右移动20步后,位置是: (20, 0)
- >>> print("向上移动120步后,位置是:", move([0, 1], 120))
- 向上移动120步后,位置是: (20, 80)
- >>> print("向左移动66步后,位置是:", move([-1, 0], 66))
- 向左移动66步后,位置是: (-46, 80)
- >>> print("向右下角移动88步后,位置是:", move([1, -1]), 88)
- Traceback (most recent call last):
- File "<pyshell#28>", line 1, in <module>
- print("向右下角移动88步后,位置是:", move([1, -1]), 88)
- TypeError: moving() missing 1 required positional argument: 'step'
- >>> print("向右下角移动88步后,位置是:", move([1, -1], 88))
- 向右下角移动88步后,位置是: (42, -8)
复制代码 |
|