在 Python 中的类之间共享全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20181830/
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 19:47:41  来源:igfitidea点击:

Sharing global variables between classes in Python

pythonclassooppython-2.7

提问by user22861

I'm relatively new to thinking of python in terms of object-oriented programming, and it's coming relatively slowly to me.

我对从面向对象编程的角度考虑 Python 比较陌生,而且对我来说它的发展速度相对较慢。

Is it possible to pass global variables between classes with multiple functions in them? I've read extensively about them here and in other sources, but I'm still a bit confused.

是否可以在具有多个函数的类之间传递全局变量?我已经在这里和其他来源广泛阅读了它们,但我仍然有点困惑。

My ultimate goal is to define a variable globally, assign it a value in a function of one class, and then USE it in another class's function. Is that possible? I'm building a pythonaddin for ArcMap, which requires a class for each button. I want to have a button to assign a value to a variable, and then use that variable in another class in the script.

我的最终目标是全局定义一个变量,在一个类的函数中给它赋值,然后在另一个类的函数中使用它。那可能吗?我正在为 ArcMap 构建一个 pythonaddin,每个按钮都需要一个类。我想要一个按钮来为变量赋值,然后在脚本的另一个类中使用该变量。

(Before I get the flak, I know it's relatively bad form to use global variables in the first place, I'm trying to learn)

(在我受到抨击之前,我知道首先使用全局变量是一种相对糟糕的形式,我正在努力学习)

For instance, as a highly simplified example:

例如,作为一个高度简化的例子:

x = []

class A():

    def func_1(self): 
        #populate the x variable
        global x 
        x = 1


class B():

    def func_2(self):
        global x
        #do stuff with x

Is this acceptable (even if not pythonic)?

这是可以接受的(即使不是pythonic)?

回答by José Tomás Tocino

Yes, it is possible. Your code is almost right, just missing a couple of parenthesis in the function definitions. Other than that, it works.

是的,这是可能的。您的代码几乎是正确的,只是在函数定义中缺少几个括号。除此之外,它的工作原理。

Now, is that acceptable? In 99% of cases, no, it's not. There're many ways of passing variables and sharing data between classes, and using global variables is one of the worse ones, if not the worst.

现在,这是可以接受的吗?在 99% 的情况下,不,不是。有很多方法可以在类之间传递变量和共享数据,使用全局变量是最糟糕的方法之一,如果不是最糟糕的

回答by Old Panda

Yes, it can work. Here is the modified code:

是的,它可以工作。这是修改后的代码:

x = []

class A:
    def func_1(self):
        #populate the x variable
        global x 
        x.append(1)
class B:
    def func_2(self):
        global x
        print x

a = A()
a.func_1()
b = B()
b.func_2()

It can print the list xcorrectly. When it's possible, you should try to avoid global variables. You can use many other ways to pass variables between classes more safely and efficiently.

它可以x正确打印列表。如果可能,您应该尽量避免使用全局变量。您可以使用许多其他方式在类之间更安全有效地传递变量。

PS: Notice the parameters in the functions.

PS:注意函数中的参数。

回答by Roland Smith

A more Pythonic solution would be to have the button objects (in their method that handles the "press" event) set an attribute or call a method of whatever object needs the information.

一个更 Pythonic 的解决方案是让按钮对象(在它们处理“按下”事件的方法中)设置一个属性或调用任何需要信息的对象的方法。