在Python中,为什么list[]自动是全局的?
这是一种奇怪的行为。
试试这个:
rep_i=0
print "rep_i is" , rep_i
def test():
global rep_i #without Global this gives error but list , dict , and others don't
if rep_i==0:
print "Testing Integer %s" % rep_i
rep_i=1
return "Done"
rep_lst=[1,2,3]
def test2():
if rep_lst[0]==1:
print "Testing List %s" % rep_lst
return "Done"
if __name__=="__main__":
test()
test2()
为什么列表不需要声明全局?它们自动是全局的吗?
我觉得这很奇怪,我大部分时间都使用列表,我什至根本不使用全局来将它们用作全局......
This is a weird behavior.
Try this :
rep_i=0
print "rep_i is" , rep_i
def test():
global rep_i #without Global this gives error but list , dict , and others don't
if rep_i==0:
print "Testing Integer %s" % rep_i
rep_i=1
return "Done"
rep_lst=[1,2,3]
def test2():
if rep_lst[0]==1:
print "Testing List %s" % rep_lst
return "Done"
if __name__=="__main__":
test()
test2()
Why list do not need to declare global? are they automatically global?
I find it really weird, I use list most of the time and I don't even use global at all to us them as global...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它不会自动成为全球性的。
但是,
rep_i=1
和rep_lst[0]=1
之间存在差异 - 前者重新绑定名称rep_i
,因此global需要
来防止创建同名的本地槽。在后一种情况下,您只是修改一个现有的全局对象,该对象是通过常规名称查找找到的(更改列表条目就像调用列表上的成员函数,它不是名称重新绑定)。要对其进行测试,请尝试在
test2
中分配rep_lst=[]
(即将其设置为新列表)。除非您声明rep_lst
global
,否则效果在test2
外部将不可见,因为创建了同名的本地插槽并遮蔽了全局插槽投币口。It isn't automatically global.
However, there's a difference between
rep_i=1
andrep_lst[0]=1
- the former rebinds the namerep_i
, soglobal
is needed to prevent creation of a local slot of the same name. In the latter case, you're just modifying an existing, global object, which is found by regular name lookup (changing a list entry is like calling a member function on the list, it's not a name rebinding).To test it out, try assigning
rep_lst=[]
intest2
(i.e. set it to a fresh list). Unless you declarerep_lst
global
, the effects won't be visible outsidetest2
because a local slot of the same name is created and shadows the global slot.如果要分配全局名称,则只需使用
global
。如果没有global
,赋值会创建一个新的局部变量。global
如何应用于列表没有什么特别之处 -global
只是影响范围和名称解析。You only need to use
global
if you are assigning to the global name. Withoutglobal
, an assignment creates a new local.There's nothing special about how
global
applies to a list—global
simply influences scope and name resolution.python 中有一个名为
UnboundLocalError
的错误,它经常让新手感到困惑。令人困惑的是:未来赋值确实会改变变量的查找方式。当解释器第一次看到变量名时,它会向前查找当前代码块的末尾,如果在同一代码块内的任何位置都没有对其进行赋值,则解释器会将其视为全局变量。但是,如果这样做,则它被视为本地,并且在赋值之前对它的任何引用都会生成
UnboundLocalError
。这就是你得到的错误。这就是为什么您需要声明全局rep_i
。如果您没有分配rep_i
,则不需要此行。此外,这与变量类型无关。此外,向列表分配或附加一个项目(您可能打算这样做,但没有这样做)并不是列表本身的分配,它本质上是调用列表对象上的方法,这与分配不同:分配创建一个新对象(可能使用已存在的名称),而操作列表时只会更改现有列表。
您可以尝试:
There is an error in python called
UnboundLocalError
which often confuses newcomers. The confusing thing is: future assignment does change the way a variable is looked up.When the interpreter sees a variable name for the first time, it looks ahead to the end of current code block, and if you don't have an assignment to it anywhere within the same block of code, the interpreter considers it global. If you do, however, then it is considered local, and any reference to it before assignment generates an
UnboundLocalError
. That's the error you got. That's why you need to declareglobal rep_i
. If you did not assignrep_i
, you wouldn't need this line.Also, this has nothing to do with variable type. Also, assigning or appending an item to the list (which you probably meant to do, but did not) is not assignment of the list itself, it is essentially calling a method on a list object, which is different from assignment: assignment creates a new object (possibly under a name that already exists), while manipulating a list just changes an existing list.
You can try:
这是一个示例,演示了子例程中可以使用非
list
/dict
变量,正如大家所说,问题在于重新绑定
行为 code> 在原始代码示例中:您将看到打印出
2
,尽管x
未声明为全局。Here's an example that demonstrates that a non
list
/dict
variable is available in a subroutine, and the problem is, as everyone says, the act ofrebinding
in your original code sample:You'll see this prints out
2
, despitex
not being declared global.如果您为
test2
内部的rep_lst
分配了一个新值(而不是像您那样仅分配给其元素之一),那么如果没有global<,它将无法工作/代码> 标志。在Python中,如果你没有给函数内的变量赋值,它会在更全局的范围内查找该变量,直到找到为止。
例如,在此代码段中,我在全局和
example()
内部定义了列表。由于example()
中的变量在范围上比全局变量更接近example2()
,因此将使用它。这与列表无关,而是 Python 中任何变量的行为。
If you had assigned a new value to
rep_lst
inside oftest2
(not just to one of its elements, as you did) it would not work without theglobal
flag. In Python, if you do not assign to a variable inside a function it will look for that variable in in more global scopes until it finds it.For example, in this code segment I define the list both globally and inside of
example()
. Since the variable inexample()
is closer in scope toexample2()
than the global one is, it is what will be used.This has nothing to do with lists, but is the behaviour of any variable in Python.