Skip to content

Commit 33bcfa7

Browse files
authored
Minor changes to nonlocal explanation
Closes satwikkansal#193
1 parent 7525e80 commit 33bcfa7

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,8 @@ Okay, now it's deleted :confused:
19871987
19881988
### ▶ The out of scope variable
19891989
<!-- Example ID: 75c03015-7be9-4289-9e22-4f5fdda056f7 --->
1990+
1991+
1\.
19901992
```py
19911993
a = 1
19921994
def some_func():
@@ -1995,8 +1997,10 @@ def some_func():
19951997
def another_func():
19961998
a += 1
19971999
return a
2000+
```
19982001
1999-
2002+
2\.
2003+
```py
20002004
def some_closure_func():
20012005
a = 1
20022006
def some_inner_func():
@@ -2026,7 +2030,7 @@ UnboundLocalError: local variable 'a' referenced before assignment
20262030
20272031
#### 💡 Explanation:
20282032
* When you make an assignment to a variable in scope, it becomes local to that scope. So `a` becomes local to the scope of `another_func`, but it has not been initialized previously in the same scope, which throws an error.
2029-
* To modify the outer scope variable `a` in `another_func`, use `global` keyword.
2033+
* To modify the outer scope variable `a` in `another_func`, we have to use the `global` keyword.
20302034
```py
20312035
def another_func()
20322036
global a
@@ -2039,8 +2043,8 @@ UnboundLocalError: local variable 'a' referenced before assignment
20392043
>>> another_func()
20402044
2
20412045
```
2042-
* In `another_closure_func`, `a` becomes local to the scope of `another_inner_func`, but it has not been initialized previously in the same scope, which is why it throws an error.
2043-
* To modify the outer scope variable `a` in `another_inner_func`, use the `nonlocal` keyword.
2046+
* In `another_closure_func`, `a` becomes local to the scope of `another_inner_func`, but it has not been initialized previously in the same scope, which is why it throws an error.
2047+
* To modify the outer scope variable `a` in `another_inner_func`, use the `nonlocal` keyword. The nonlocal statement is used to refer to variables defined in the nearest outer (excluding the global) scope.
20442048
```py
20452049
def another_func():
20462050
a = 1
@@ -2056,7 +2060,7 @@ UnboundLocalError: local variable 'a' referenced before assignment
20562060
>>> another_func()
20572061
2
20582062
```
2059-
* The keywords `global` and `nonlocal` are ways to simply tell the python interpreter to not delcare new variables, but to just look them up from the corresponding scope.
2063+
* The keywords `global` and `nonlocal` tell the python interpreter to not delcare new variables and look them up in the corresponding outer scopes.
20602064
* Read [this](http://sebastianraschka.com/Articles/2014_python_scope_and_namespaces.html) short but an awesome guide to learn more about how namespaces and scope resolution works in Python.
20612065
20622066
---

0 commit comments

Comments
 (0)