在 Python 中将列表定义为全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4630543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:37:47  来源:igfitidea点击:

Defining lists as global variables in Python

pythonlistglobal-variables

提问by Hossein

I am using a list on which some functions works in my program. This is a shared list actually and all of my functions can edit it. Is it really necessary to define it as "global" in all the functions?

我正在使用一个列表,其中一些函数在我的程序中起作用。这实际上是一个共享列表,我所有的功能都可以编辑它。是否真的有必要在所有功能中将其定义为“全局”?

I mean putting the global keyword behind it in each function that uses it, or defining it outside of all the functions is enough without using the global word behind its definition?

我的意思是将 global 关键字放在使用它的每个函数中,或者在所有函数之外定义它就足够了,而无需在其定义后面使用 global 词?

采纳答案by Taylor Leese

When you assign a variable (x = ...), you are creating a variable in the current scope (e.g. local to the current function). If it happens to shadow a variable fron an outer (e.g. global) scope, well too bad - Python doesn't care (and that's a good thing). So you can't do this:

当您分配一个变量 ( x = ...) 时,您是在当前范围内创建一个变量(例如,当前函数的局部变量)。如果它碰巧在外部(例如全局)范围内隐藏变量,那就太糟糕了 - Python 不在乎(这是一件好事)。所以你不能这样做:

x = 0
def f():
    x = 1
f()
print x #=>0

and expect 1. Instead, you need do declare that you intend to use the global x:

并期待1。相反,您需要声明您打算使用global x

x = 0
def f():
    global x
    x = 1
f()
print x #=>1

But note that assignment of a variable is very different from method calls. You can always call methods on anything in scope - e.g. on variables that come from an outer (e.g. the global) scope because nothing local shadows them.

但请注意,变量的赋值与方法调用非常不同。你总是可以在范围内的任何东西上调用方法——例如,在来自外部(例如全局)范围的变量上调用方法,因为没有任何局部影响它们。

Also veryimportant: Member assignment (x.name = ...), item assignment (collection[key] = ...), slice assignment (sliceable[start:end] = ...) and propably more are all method calls as well! And therefore you don't need globalto change a global's members or call it methods (even when they mutate the object).

同样非常重要的是:成员赋值 ( x.name = ...)、项赋值 ( collection[key] = ...)、切片赋值 ( sliceable[start:end] = ...) 以及可能更多的都是方法调用!因此,您不需要global更改全局成员或调用它的方法(即使它们改变了对象)。

回答by Taylor Leese

Yes, you need to use global fooif you are going to write to it.

是的,global foo如果要写入,则需要使用。

foo = []

def bar():
    global foo
    ...
    foo = [1]

回答by XORcist

No, you can specify the list as a keyword argument to your function.

不,您可以将列表指定为函数的关键字参数。

alist = []
def fn(alist=alist):
    alist.append(1)
fn()
print alist    # [1]

I'd say it's bad practice though. Kind of too hackish. If you really need to use a globally available singleton-like data structure, I'd use the module level variable approach, i.e. put 'alist' in a module and then in your other modules import that variable:

我会说这是不好的做法。有点太黑客了。如果你真的需要使用一个全局可用的类似单例的数据结构,我会使用模块级变量方法,即将“alist”放在一个模块中,然后在你的其他模块中导入该变量:

In file foomodule.py:

在文件 foomodule.py 中:

alist = []

In file barmodule.py:

在文件 barmodule.py 中:

import foomodule
def fn():
    foomodule.alist.append(1)
print foomodule.alist    # [1]