From a1e04821d1d2cf7c01a42f8a8037b01ad4d7ca78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Mon, 28 Jan 2019 15:52:23 +0100 Subject: [PATCH] Nonlocal --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 4819b43..121b986 100644 --- a/README.md +++ b/README.md @@ -577,6 +577,25 @@ from functools import partial 30 ``` +### Nonlocal +**If variable is assigned to anywhere in the scope, it is regarded as local variable, unless it is declared as global or nonlocal.** + +```python +def get_counter(): + a = 0 + def out(): + nonlocal a + a += 1 + return a + return out +``` + +```python +>>> counter = get_counter() +>>> counter(), counter(), counter() +(0, 1, 2) +``` + Decorator ---------