|
@ -536,6 +536,10 @@ creature = Creature() |
|
|
|
|
|
|
|
|
Closure |
|
|
Closure |
|
|
------- |
|
|
------- |
|
|
|
|
|
**We have a closure in Python when:** |
|
|
|
|
|
* **A nested function references a value of its enclosing function, and then** |
|
|
|
|
|
* **the enclosing function returns the nested function. |
|
|
|
|
|
|
|
|
```python |
|
|
```python |
|
|
def get_multiplier(a): |
|
|
def get_multiplier(a): |
|
|
def out(b): |
|
|
def out(b): |
|
@ -549,6 +553,8 @@ def get_multiplier(a): |
|
|
30 |
|
|
30 |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
* **If multiple nested functions within enclosing function reference the same value, that value gets shared.** |
|
|
|
|
|
|
|
|
#### Or: |
|
|
#### Or: |
|
|
```python |
|
|
```python |
|
|
from functools import partial |
|
|
from functools import partial |
|
|