4

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?

0

1 Answer 1

5

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.

2
  • Hi! @abarnert thank you for the fast answer. I went for the third approach and works soft!. What do you think is the best practice? Thank you!
    – CristoJV
    Aug 23, 2018 at 19:21
  • @Cristo It really depends on what you’re doing. Personally, I’ve rarely had a use for something as specific as an “int holder” that wasn’t even more specific, like a “user object” (that happens to have no fields but an int user id), but that doesn’t mean you don’t have a good use for it. As long as you’re thinking of terms of “how do I model this data in Python” rather than “how do I translate this C++ model to a Python model”, your intuitions are probably going to be fine.
    – abarnert
    Aug 23, 2018 at 20:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.