相关疑难解决方法(0)

在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 dont
  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()
Run Code Online (Sandbox Code Playgroud)

为什么列表不需要声明全局?他们是自动全球的?

我发现它真的很奇怪,我大部分时间都使用列表,我甚至根本不使用全球它们作为全球.....

python global list python-2.7

9
推荐指数
3
解决办法
1万
查看次数

标签 统计

global ×1

list ×1

python ×1

python-2.7 ×1