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
Copy file name to clipboardExpand all lines: README.md
+9-5Lines changed: 9 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -1987,6 +1987,8 @@ Okay, now it's deleted :confused:
1987
1987
1988
1988
### ▶ The out of scope variable
1989
1989
<!-- Example ID: 75c03015-7be9-4289-9e22-4f5fdda056f7 --->
1990
+
1991
+
1\.
1990
1992
```py
1991
1993
a = 1
1992
1994
def some_func():
@@ -1995,8 +1997,10 @@ def some_func():
1995
1997
def another_func():
1996
1998
a += 1
1997
1999
return a
2000
+
```
1998
2001
1999
-
2002
+
2\.
2003
+
```py
2000
2004
def some_closure_func():
2001
2005
a = 1
2002
2006
def some_inner_func():
@@ -2026,7 +2030,7 @@ UnboundLocalError: local variable 'a' referenced before assignment
2026
2030
2027
2031
#### 💡 Explanation:
2028
2032
* 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.
2030
2034
```py
2031
2035
def another_func()
2032
2036
global a
@@ -2039,8 +2043,8 @@ UnboundLocalError: local variable 'a' referenced before assignment
2039
2043
>>> another_func()
2040
2044
2
2041
2045
```
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.
2044
2048
```py
2045
2049
def another_func():
2046
2050
a = 1
@@ -2056,7 +2060,7 @@ UnboundLocalError: local variable 'a' referenced before assignment
2056
2060
>>> another_func()
2057
2061
2
2058
2062
```
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.
2060
2064
* 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