Skip to content

Commit 262035c

Browse files
暮晨leisurelicht
暮晨
authored andcommitted
EX.The out of scope variable
1 parent 1d1fac8 commit 262035c

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ So, here we go...
6262
- [> Beware of default mutable arguments!/当心默认的可变参数!](#-beware-of-default-mutable-arguments当心默认的可变参数)
6363
- [> Catching the Exceptions/捕获异常](#-catching-the-exceptions捕获异常)
6464
- [> 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外部作用域变量)
6666
- [> Be careful with chained operations](#-be-careful-with-chained-operations)
6767
- [> Name resolution ignoring class scope](#-name-resolution-ignoring-class-scope)
6868
- [> Needle in a Haystack](#-needle-in-a-haystack)
@@ -1722,7 +1722,7 @@ a += [5, 6, 7, 8]
17221722
17231723
---
17241724
1725-
### > The out of scope variable
1725+
### > The out of scope variable/外部作用域变量
17261726
17271727
```py
17281728
a = 1
@@ -1742,10 +1742,10 @@ def another_func():
17421742
UnboundLocalError: local variable 'a' referenced before assignment
17431743
```
17441744
1745-
#### 💡 Explanation:
1746-
* 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.
1747-
* 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.
1748-
* To modify the outer scope variable `a` in `another_func`, use `global` keyword.
1745+
#### 💡 说明:
1746+
* 当你在作用域中对变量进行赋值时, 变量会变成该作用域内的局部变量. 因此 `a` 会变成 `another_func` 函数作用域中的局部变量, 但它在函数作用域中并没有被初始化, 所以会引发错误.
1747+
* 可以阅读[这个](http://sebastianraschka.com/Articles/2014_python_scope_and_namespaces.html)简短却很棒的指南, 了解更多关于 Python 中命名空间和作用域的工作原理.
1748+
* 想要在 `another_func` 中修改外部作用域变量 `a` 的话, 可以使用 `global` 关键字.
17491749
```py
17501750
def another_func()
17511751
global a

0 commit comments

Comments
 (0)