You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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.
Copy file name to clipboardExpand all lines: CONTRIBUTING.md
+1-2
Original file line number
Diff line number
Diff line change
@@ -39,5 +39,4 @@ Few things that you can consider while writing an example,
39
39
- 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.
40
40
- 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.
41
41
- 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).
Copy file name to clipboardExpand all lines: README.md
+41-3
Original file line number
Diff line number
Diff line change
@@ -1028,7 +1028,8 @@ Even when the values of `x` were different in every iteration prior to appending
1028
1028
1029
1029
- 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.
1030
1030
1031
-
- To get the desired behavior you can passin 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 passin 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.
1032
1033
1033
1034
```py
1034
1035
funcs = []
@@ -1994,6 +1995,20 @@ def some_func():
1994
1995
def another_func():
1995
1996
a += 1
1996
1997
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()
1997
2012
```
1998
2013
1999
2014
**Output:**
@@ -2002,11 +2017,15 @@ def another_func():
2002
2017
1
2003
2018
>>> another_func()
2004
2019
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
2005
2025
```
2006
2026
2007
2027
#### 💡 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.
2010
2029
* To modify the outer scope variable `a` in `another_func`, use `global` keyword.
2011
2030
```py
2012
2031
def another_func()
@@ -2020,6 +2039,25 @@ UnboundLocalError: local variable 'a' referenced before assignment
2020
2039
>>> another_func()
2021
2040
2
2022
2041
```
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.
0 commit comments