Skip to content

Commit 7525e80

Browse files
authored
satwikkansal#193 The out of scope variable, nonlocal (satwikkansal#228)
* fixed link to CONTRIBUTORS.md * The out of scope variable, nonlocal, satwikkansal#193 * added myself to CONTRIBUTORS.md :D * Update CONTRIBUTORS.md * added entry in index * merged nonlocal to `the out of scope variable` example Also removed the extra entry from the main index.
1 parent 9b3f869 commit 7525e80

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

CONTRIBUTING.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,4 @@ Few things that you can consider while writing an example,
3939
- If you are choosing to submit a new example without creating an issue and discussing, please check the project to make sure there aren't similar examples already.
4040
- Try to be consistent with the namings and the values you use with the variables. For instance, most variable names in the project are along the lines of `some_string`, `some_list`, `some_dict`, etc. You'd see a lot of `x`s for single letter variable names, and `"wtf"` as values for strings. There's no strictly enforced scheme in the project as such, but you can take a glance at other examples to get a gist.
4141
- Try to be as creative as possible to add that element of "surprise" in the setting up part of an example. Sometimes this may mean writing a snippet a sane programmer would never write.
42-
- Also, feel free to add your name to the [contributors list](/CONTRIBUTING.md).
43-
42+
- Also, feel free to add your name to the [contributors list](/CONTRIBUTORS.md).

CONTRIBUTORS.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Following are the wonderful people (in no specific order) who have contributed t
2222
| koddo | [koddo](https://github.com/koddo) | [#80](https://github.com/satwikkansal/wtfpython/issues/80), [#73](https://github.com/satwikkansal/wtfpython/issues/73) |
2323
| jab | [jab](https://github.com/jab) | [#77](https://github.com/satwikkansal/wtfpython/issues/77) |
2424
| Jongy | [Jongy](https://github.com/Jongy) | [#208](https://github.com/satwikkansal/wtfpython/issues/208), [#210](https://github.com/satwikkansal/wtfpython/issues/210) |
25+
| Diptangsu Goswami | [diptangsu](https://github.com/diptangsu) | [#193](https://github.com/satwikkansal/wtfpython/issues/193) |
26+
2527
---
2628

2729
**Translations**

README.md

+41-3
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,8 @@ Even when the values of `x` were different in every iteration prior to appending
10281028
10291029
- When defining a function inside a loop that uses the loop variable in its body, the loop function's closure is bound to the variable, not its value. So all of the functions use the latest value assigned to the variable for computation.
10301030
1031-
- To get the desired behavior you can pass in the loop variable as a named variable to the function. **Why does this work?** Because this will define the variable again within the function's scope.
1031+
- To get the desired behavior you can pass in the loop variable as a named variable to the function. **Why does this work?** Because this will define the variable
1032+
within the function's scope.
10321033
10331034
```py
10341035
funcs = []
@@ -1994,6 +1995,20 @@ def some_func():
19941995
def another_func():
19951996
a += 1
19961997
return a
1998+
1999+
2000+
def some_closure_func():
2001+
a = 1
2002+
def some_inner_func():
2003+
return a
2004+
return some_inner_func()
2005+
2006+
def another_closure_func():
2007+
a = 1
2008+
def another_inner_func():
2009+
a += 1
2010+
return a
2011+
return another_inner_func()
19972012
```
19982013
19992014
**Output:**
@@ -2002,11 +2017,15 @@ def another_func():
20022017
1
20032018
>>> another_func()
20042019
UnboundLocalError: local variable 'a' referenced before assignment
2020+
2021+
>>> some_closure_func()
2022+
1
2023+
>>> another_closure_func()
2024+
UnboundLocalError: local variable 'a' referenced before assignment
20052025
```
20062026
20072027
#### 💡 Explanation:
2008-
* 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.
2009-
* 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.
2028+
* 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.
20102029
* To modify the outer scope variable `a` in `another_func`, use `global` keyword.
20112030
```py
20122031
def another_func()
@@ -2020,6 +2039,25 @@ UnboundLocalError: local variable 'a' referenced before assignment
20202039
>>> another_func()
20212040
2
20222041
```
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.
2044+
```py
2045+
def another_func():
2046+
a = 1
2047+
def another_inner_func():
2048+
nonlocal a
2049+
a += 1
2050+
return a
2051+
return another_inner_func()
2052+
```
2053+
2054+
**Output:**
2055+
```py
2056+
>>> another_func()
2057+
2
2058+
```
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.
2060+
* 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.
20232061
20242062
---
20252063

0 commit comments

Comments
 (0)