1
0
mirror of https://github.com/satwikkansal/wtfpython synced 2024-11-22 11:04:25 +01:00

Minor changes to nonlocal explanation

Closes https://github.com/satwikkansal/wtfpython/issues/193
This commit is contained in:
Satwik Kansal 2020-10-05 20:42:16 +05:30 committed by GitHub
parent 7525e800b8
commit 33bcfa744a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

12
README.md vendored
View File

@ -1987,6 +1987,8 @@ Okay, now it's deleted :confused:
### ▶ The out of scope variable ### ▶ The out of scope variable
<!-- Example ID: 75c03015-7be9-4289-9e22-4f5fdda056f7 ---> <!-- Example ID: 75c03015-7be9-4289-9e22-4f5fdda056f7 --->
1\.
```py ```py
a = 1 a = 1
def some_func(): def some_func():
@ -1995,8 +1997,10 @@ def some_func():
def another_func(): def another_func():
a += 1 a += 1
return a return a
```
2\.
```py
def some_closure_func(): def some_closure_func():
a = 1 a = 1
def some_inner_func(): def some_inner_func():
@ -2026,7 +2030,7 @@ UnboundLocalError: local variable 'a' referenced before assignment
#### 💡 Explanation: #### 💡 Explanation:
* 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. * 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.
* To modify the outer scope variable `a` in `another_func`, use `global` keyword. * To modify the outer scope variable `a` in `another_func`, we have to use the `global` keyword.
```py ```py
def another_func() def another_func()
global a global a
@ -2040,7 +2044,7 @@ UnboundLocalError: local variable 'a' referenced before assignment
2 2
``` ```
* 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. * 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.
* To modify the outer scope variable `a` in `another_inner_func`, use the `nonlocal` keyword. * 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.
```py ```py
def another_func(): def another_func():
a = 1 a = 1
@ -2056,7 +2060,7 @@ UnboundLocalError: local variable 'a' referenced before assignment
>>> another_func() >>> another_func()
2 2
``` ```
* 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. * The keywords `global` and `nonlocal` tell the python interpreter to not delcare new variables and look them up in the corresponding outer scopes.
* 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. * 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.
--- ---