Skip to content

Commit a1e0482

Browse files
committed
Nonlocal
1 parent 6eec781 commit a1e0482

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,25 @@ from functools import partial
577577
30
578578
```
579579

580+
### Nonlocal
581+
**If variable is assigned to anywhere in the scope, it is regarded as local variable, unless it is declared as global or nonlocal.**
582+
583+
```python
584+
def get_counter():
585+
a = 0
586+
def out():
587+
nonlocal a
588+
a += 1
589+
return a
590+
return out
591+
```
592+
593+
```python
594+
>>> counter = get_counter()
595+
>>> counter(), counter(), counter()
596+
(0, 1, 2)
597+
```
598+
580599

581600
Decorator
582601
---------

0 commit comments

Comments
 (0)