简体   繁体   English

如何在 python 中的类之间共享非全局变量

[英]how to share a non global variables between classes in python

I need to share a variable between 2 classes that are called in the main file.我需要在主文件中调用的 2 个类之间共享一个变量。 in the following replicable code, variable sharedVar is defined in main file and passed to both class child1 and child 2. I am expecting the variable to be equal to 2 in child2 since it has been changed from 1 to 2 in child1.在下面的可复制代码中,变量 sharedVar 在主文件中定义并传递给 class child1 和 child 2。我希望 child2 中的变量等于 2,因为它在 child1 中已从 1 更改为 2。 but it still prints it as 1.但它仍然将其打印为 1。

file mainClass:文件主类:

from class1 import Class1
from class2 import Class2

class mainClass():
    sharedVar = 1
    my_child1 = Class1(sharedVar)  # expecting to print 1
    my_child2 = Class2(sharedVar)  # expecting to print 2

file class1文件类 1

class Class1():
    def __init__(self,sharedVar):
        sharedVar += 1
        print("Class1",sharedVar) # output is 2

file class2文件类2

class Class2():
    def __init__(self,sharedVar):
        print("Class2",sharedVar) # output is 1 but expected to be 2

Note: These classes are in 3 separate files, so defining a global variable outside the classes will not work!注意:这些类位于 3 个单独的文件中,因此在类之外定义全局变量将不起作用!

If you have an answer, please be specific and provide the code.如果您有答案,请具体并提供代码。 writing an explanation without codes is not going to help me nor the future readers of this question.写一个没有代码的解释对我和这个问题的未来读者都没有帮助。

Like this:像这样:

file mainClass:文件主类:

from class1 import Class1
from class2 import Class2

class mainClass():
    def __init__(self):
        self.sharedVar = 1
        self.my_child1 = Class1(self)  # expecting to print 1
        self.my_child2 = Class2(self)  # expecting to print 2

file class1文件类 1

class Class1():
    def __init__(self,sharedObj):
        sharedObj.sharedVar += 1
        print("Class1",sharedObj.sharedVar)

file class2文件类2

class Class2():
    def __init__(self,sharedObj):
        print("Class2",sharedObj.sharedVar)

if you came from a c++ or java background, of course, this is what you expect to happen(If they are passed by reference), however, it's not like that in python.如果您来自 c++ 或 java 背景,当然,这是您期望发生的(如果它们通过引用传递),但是,在 python 中却不是这样。 variables here are a bit different, it's called identifier;这里的变量有点不同,叫做标识符; when I say that x=5 this means python will create an object in memory of type int(let's call b1) and with value 5, and will let x point to that variable, so when I make x +=1 this mean that python will create a new object in memory (call it b2)of type int with value 6 and will let x now point to b2. when I say that x=5 this means python will create an object in memory of type int(let's call b1) and with value 5, and will let x point to that variable, so when I make x +=1 this mean that python将在 int 类型的 memory(称为 b2)中创建一个新的 object,其值为 6,并让 x 现在指向 b2。 so now b1 if left unpointed.所以现在 b1 如果没有指出。

Another example: if we say x=5, y=x--> then we have an object in memory of type int with value 5, and x and y point to it;另一个例子:如果我们说 x=5, y=x--> 那么我们在 memory 中有一个 object 类型为 int ,值为 5,x 和 y 指向它; so when I say x=9 I let x point to another object which is equal to 9, but still y points to 5所以当我说 x=9 我让 x 指向另一个 object 等于 9,但仍然 y 指向 5

so when you send it to class1 it let x point just to a different object and in class2 it send the original object which is equal to 1. I understood very deeply from Data structure and algorithms in python book I encourage you to read that section.因此,当您将其发送到 class1 时,它让 x 仅指向不同的 object,而在 class2 中,它发送原始的 object 等于 1。我从 Z23EEEB4347BDD26BFC6B7EE9A3B755 中的数据结构和算法中非常深入地了解我鼓励您阅读该部分的书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM