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
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -62,7 +62,7 @@ So, here we go...
62
62
-[> Beware of default mutable arguments!/当心默认的可变参数!](#-beware-of-default-mutable-arguments当心默认的可变参数)
63
63
-[> Catching the Exceptions/捕获异常](#-catching-the-exceptions捕获异常)
64
64
-[> Same operands, different story!/同人不同命!](#-same-operands-different-story同人不同命)
65
-
-[> The out of scope variable](#-the-out-of-scope-variable)
65
+
-[> The out of scope variable/外部作用域变量](#-the-out-of-scope-variable外部作用域变量)
66
66
-[> Be careful with chained operations](#-be-careful-with-chained-operations)
67
67
-[> Name resolution ignoring class scope](#-name-resolution-ignoring-class-scope)
68
68
-[> Needle in a Haystack](#-needle-in-a-haystack)
@@ -1717,7 +1717,7 @@ a += [5, 6, 7, 8]
1717
1717
1718
1718
---
1719
1719
1720
-
### > The out of scope variable
1720
+
### > The out of scope variable/外部作用域变量
1721
1721
1722
1722
```py
1723
1723
a = 1
@@ -1737,10 +1737,10 @@ def another_func():
1737
1737
UnboundLocalError: local variable 'a' referenced before assignment
1738
1738
```
1739
1739
1740
-
#### 💡 Explanation:
1741
-
* 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.
1742
-
* 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.
1743
-
* To modify the outer scope variable `a` in `another_func`, use `global` keyword.
0 commit comments