Python - 在不同类的不同实例之间共享变量 [英] Python - Sharing variables between different instances of different classes

查看:57
本文介绍了Python - 在不同类的不同实例之间共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找下一个答案,但可以肯定的是,我一直在寻找错误的关键字.我曾经用 C++ 开发,在对象之间传递指针作为引用.情况是,现在我正在尝试用 Python 构建一个程序,其中General"类的一个实例使用相同的共享变量初始化Specific"类的不同实例.

class General():def __init__(self):self._shared_variable = 0self._specific1 = 具体(self._shared_variable)self._specific2 = 具体(self._shared_variable)特定类():def __init__(self, shared):self._shared = 共享def modify_shared_variable(self):self._shared_variable +=1

所以我想要做的是在一般范围内共享这个 'shared_variable',所以当一个 'Specific' 实例修改他的内部变量时,这个变化会被另一个实例看到或反映实例.但是在python中情况并非如此.因此,每个特定实例都有自己的变量.我怎样才能做到这一点?

解决方案

您不能在 Python 中引用变量.变量只是在某个命名空间(通常是模块、类或实例对象的 __dict__ 或函数调用框架内的特殊本地命名空间)中的一个名称.>

您可以引用,但当然数字是不可变的值,因此您不能将数字 1 更改为数字 2.

因此,您可以做的是创建某种保存数字的可变值,并共享对那个的引用.

<小时>

一个明显的可能性是给每个 Specific 实例一个对创建它的 General 实例的引用:

class General():def __init__(self):self._shared_variable = 0self._specific1 = 具体(自我)self._specific2 = 具体(自我)特定类():def __init__(self, shared_general):self._shared_general = shared_generaldef modify_shared_variable(self):self._shared_general._shared_variable +=1

<小时>

另一种可能是存储一个单元素列表:

class General():def __init__(self):self._shared_variable = [0]self._specific1 = 具体(self._shared_variable)self._specific2 = 具体(self._shared_variable)特定类():def __init__(self, shared):self._shared = 共享def modify_shared_variable(self):self._shared[0] += 1

(这实际上与您在 C++ 中所做的相同,但没有数组和指针的语法糖几乎是相同的......)

<小时>

或者您可以创建一个简单的 MutableInteger 类,其中包含一个 int,代理非变异方法,添加一个 set 方法到替换它,并通过调用 set 并返回 self 而不是返回新值来处理 += 和其他变异方法.

I have been searching for the next answer but for sure I have been searching the wrong keywords. I used to develop with C++, passing pointers as references between objects. The case is, now I'm trying to build a program in Python where one instance of a class 'General' initializes different instances of a class 'Specific' with the same shared variable.

class General():
    def __init__(self):
        self._shared_variable = 0
        self._specific1 = Specific(self._shared_variable)
        self._specific2 = Specific(self._shared_variable)

class Specific():
    def __init__(self, shared):
        self._shared = shared
    def modify_shared_variable(self):
        self._shared_variable +=1

So what I'm trying to do is shared this 'shared_variable' within de General scope, so when a 'Specific' instance modifies his internal variable, this change is seeing or mirrored by the other instance. But this is not the case in python. So, every specific instance has its own variable. How can I achieve this?

解决方案

You can't have references to variables in Python. A variable is just a name, in some namespace (usually the __dict__ of a module, class, or instance object, or the special local namespace inside a function-call frame), for a value.

You can have references to values, but of course numbers are immutable values, so you can't change the number 1 into the number 2.

So, what you can do is create some kind of mutable value that holds the number, and share references to that.


One obvious possibility is to just give each Specific instance a reference to the General instance that created it:

class General():
    def __init__(self):
        self._shared_variable = 0
        self._specific1 = Specific(self)
        self._specific2 = Specific(self)

class Specific():
    def __init__(self, shared_general):
        self._shared_general = shared_general
    def modify_shared_variable(self):
        self._shared_general._shared_variable +=1


Another possibility is to store a single-element list:

class General():
    def __init__(self):
        self._shared_variable = [0]
        self._specific1 = Specific(self._shared_variable)
        self._specific2 = Specific(self._shared_variable)

class Specific():
    def __init__(self, shared):
        self._shared = shared
    def modify_shared_variable(self):
        self._shared[0] += 1

(This is really the same thing you're doing in C++, but without the syntactic sugar of arrays and pointers being nearly the same thing…)


Or you can create a simple MutableInteger class that holds an int, proxies non-mutating methods to it, adds a set method to replace it, and handles += and other mutating methods by calling set and returning self, instead of returning a new value.

这篇关于Python - 在不同类的不同实例之间共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆