Python - 定义多个全局变量的最简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40992931/
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
Python - Easiest way to define multiple global variables
提问by Stephen Gelardi
I am trying to look for an easy way to define multiple global variables from inside a function or class.
我正在尝试寻找一种从函数或类内部定义多个全局变量的简单方法。
The obvious option is to do the following:
显而易见的选择是执行以下操作:
global a,b,c,d,e,f,g......
a=1
b=2
c=3
d=4
.....
This is a simplified example but essentially I need to define dozens of global variables based on the input of a particular function. Ideally I would like to take care of defining the global name and value without having to update both the value and defining the global name independently.
这是一个简化的示例,但本质上我需要根据特定函数的输入定义数十个全局变量。理想情况下,我想负责定义全局名称和值,而不必独立更新值和定义全局名称。
To add some more clarity.
为了增加一些清晰度。
I am looking for the python equivalent of this (javascript):
我正在寻找与此 (javascript) 等效的 python:
var a = 1
, b = 2
, c = 3
, d = 4
, e = 5;
采纳答案by qui
Alright, I'm new myself and this has been a big issue for me as well and I think it has to do with the phrasing of the question (I phrased it the same way when googling and couldn't quite find an answer that was relevant to what I was trying to do). Please, someone correct me if there's a reason I should not do this, but...
好吧,我自己是新人,这对我来说也是一个大问题,我认为这与问题的措辞有关(我在谷歌搜索时用同样的方式措辞,但找不到答案与我正在尝试做的事情相关)。如果有我不应该这样做的原因,请有人纠正我,但是......
The best way to set up a list of global variables would be to set up a class for them in that module.
设置全局变量列表的最佳方法是在该模块中为它们设置一个类。
class globalBS():
bsA = "F"
bsB = "U"
now you can call them, change them, etc. in any other function by referencing them as such:
现在您可以通过引用它们在任何其他函数中调用它们、更改它们等:
print(globalBS.bsA)
would print "F" in any function on that module. The reason for this is that python treats classes as modules, so this works out well for setting up a list of global variables- such as when trying to make an interface with multiple options such as I'm currently doing, without copying and pasting a huge list of globals in every function. I would just keep the class name as short as possible- maybe one or two characters instead of an entire word.
将在该模块的任何函数中打印“F”。这样做的原因是 python 将类视为模块,因此这对于设置全局变量列表很有效 - 例如在尝试创建具有多个选项的接口时,例如我目前正在做的,而无需复制和粘贴每个函数中的大量全局变量列表。我只会让班级名称尽可能短——可能是一两个字符而不是整个单词。
Hope that helps!
希望有帮助!
回答by d3x
You could just unpack the variables:
你可以解压变量:
global x, y, z
x, y, z = 4, 5, 6
回答by Martinbaste
If they are conceptually related one option is to have them in a dictionary:
如果它们在概念上相关,一个选择是将它们放在字典中:
global global_dictionary
global_dictionary = {'a': 1, 'b': 2, 'c': 3}
But as it was commented in your question, this is not the best approach to handle the problem that generated this question.
但是正如您在问题中所评论的那样,这不是处理产生此问题的问题的最佳方法。
回答by blue_note
The globals()
builtin function refers to the global variables in the current module. You can use it just like a normal dictionary
该globals()
内置功能是指当前模块中的全局变量。您可以像使用普通字典一样使用它
globals()['myvariable'] = 5
print(myvariable) # returns 5
Repeat this for all you variables.
对所有变量重复此操作。
However, you most certainly shouldn't do that
但是,您绝对不应该这样做