This question already has an answer here:
这个问题在这里已有答案:
- Referencing global primitives vs. objects 1 answer
引用全局原语与对象1回答
I have 2 example below, and my question is: why List L
don't need global
to change value like variable V
, I think, more Safe if L
need global
to change
我下面有两个例子,我的问题是:为什么List L不需要全局来改变变量V这样的值,我认为,如果L需要全局改变则更安全
Example 1:
L=[1,2,3,4]
V=5
def U():
L[1]=22
V=55
U()
print L[1]
print V
Result 1:
22
5
Example 2:
L=[1,2,3,4]
V=5
def U():
L[1]=22
global V #Now V will change like L[1]
V=55
U()
print L[1]
print V
Result 2:
22
55
1 个解决方案
#1
Perhaps someone can explain it better than me, but when you do the following:
也许有人可以比我更好地解释它,但当你做以下事情时:
print V
Python knows this variable isn't defined in your functions scope, and so it checks the global scope, and prints it.
Python知道这个变量没有在你的函数范围中定义,因此它会检查全局范围并打印它。
If you were to do instead
如果你要这样做
V=5
Python knows this variable isn't defined in your functions scope, so it goes ahead and creates it (now it over-shadows V
at the global scope). There is different behaviour between writing a variable and reading it.
Python知道这个变量没有在你的函数范围中定义,因此它继续并创建它(现在它在全局范围内覆盖了V)。在编写变量和读取变量之间存在不同的行为。
Now compare this to a list:
现在将其与列表进行比较:
L[1]=22
This is similar to print V
. Why? Because Python needs to find where L is defined in memory, and then modifies its second index. This is a read
followed by a write
, whereas print V
is simply a read
from memory.
这与print V类似。为什么?因为Python需要找到L在内存中定义的位置,然后修改其第二个索引。这是一个读取后写入,而打印V只是从内存中读取。
When your operation needs to do a read
first, it will find the reference that was defined at the outer scope, and that is why you see a difference between V=55
and L[1]=22
.
当您的操作需要先读取时,它将找到在外部范围定义的引用,这就是您看到V = 55和L [1] = 22之间的差异的原因。
When your operation is a write
first, it will create a new variable in the function scope unless you have that variable marked as a global
. Then it will modify that global
instead.
当您的操作首先写入时,它将在函数范围中创建一个新变量,除非您将该变量标记为全局变量。然后它将修改该全局。
#1
Perhaps someone can explain it better than me, but when you do the following:
也许有人可以比我更好地解释它,但当你做以下事情时:
print V
Python knows this variable isn't defined in your functions scope, and so it checks the global scope, and prints it.
Python知道这个变量没有在你的函数范围中定义,因此它会检查全局范围并打印它。
If you were to do instead
如果你要这样做
V=5
Python knows this variable isn't defined in your functions scope, so it goes ahead and creates it (now it over-shadows V
at the global scope). There is different behaviour between writing a variable and reading it.
Python知道这个变量没有在你的函数范围中定义,因此它继续并创建它(现在它在全局范围内覆盖了V)。在编写变量和读取变量之间存在不同的行为。
Now compare this to a list:
现在将其与列表进行比较:
L[1]=22
This is similar to print V
. Why? Because Python needs to find where L is defined in memory, and then modifies its second index. This is a read
followed by a write
, whereas print V
is simply a read
from memory.
这与print V类似。为什么?因为Python需要找到L在内存中定义的位置,然后修改其第二个索引。这是一个读取后写入,而打印V只是从内存中读取。
When your operation needs to do a read
first, it will find the reference that was defined at the outer scope, and that is why you see a difference between V=55
and L[1]=22
.
当您的操作需要先读取时,它将找到在外部范围定义的引用,这就是您看到V = 55和L [1] = 22之间的差异的原因。
When your operation is a write
first, it will create a new variable in the function scope unless you have that variable marked as a global
. Then it will modify that global
instead.
当您的操作首先写入时,它将在函数范围中创建一个新变量,除非您将该变量标记为全局变量。然后它将修改该全局。