代码运行位置 | 代码 | 新增/更改的变量 | 该框架中的所有变量 | 备注 |
1 | def power(exp): | power = <function power> | |
6 | cube = power(3) | | |
6>2 | def exp_of(base): | exp_of = <function power.<locals>.exp_of> | - exp = 3
- exp_of = <function power.<locals>.exp_of>
| 调用函数体 |
6>4 | return exp_of | | - exp = 3
- exp_of = <function power.<locals>.exp_of>
| 返回闭包函数,退出函数体 |
6 | cube = power(3) | cube = <function power.<locals>.exp_of> | - cube = <function power.<locals>.exp_of>
- power = <function power>
| 此时cube中是有exp变量的 |
7 | print(cube(4)) | | - cube = <function power.<locals>.exp_of>
- power = <function power>
|
7>3 | return base ** exp | | | 调用函数体,cube函数体内是有power函数体内的所有变量的,计算后退出函数体 |
7 | print(cube(4)) | | - cube = <function power.<locals>.exp_of>
- power = <function power>
| cube(4)返回了64,输出64 |