4

I read some posts regarding how we can create a shared variable among class instances in python. One way to do it is to define this variable before class initialization. While this works for variables, it seems it doesn't work for what I want.

class C1:
    def __init__(self, a, b):
        self.c = a + b

class C2:
    obj1 = C1(x,y)
    def __init__(self):
        self.d = 1

obj2 = C2

How can we set variables x and y for the instance obj1 of class C1? If we define this instance inside C2 initialization, then we could write

obj2 = C2(var1,var2)

But, now that it is outside C2 initialization, I don't know how to do it. Any idea?

3
  • Can you describe your "what I want" in practice? Not in programming terms but what your program must do? It's unclear why do you want classes and objects in the first place Commented Jun 13, 2018 at 18:14
  • @Ingaz Assume you have two different classes A and B. you want to have instances of class B where all an instance of class A is shared among them. Commented Jun 13, 2018 at 18:21
  • First: "all an instance of class A" - forgive me, I don't understand this phrase. You have only one instance of class A or multiple? Second: properties of instance of A are mutable or immutable? Third: if instance of A is mutable what will become with created instances of B? Create instances of B change their behaviour after mutation of object A or only new created B-instances are affected? Commented Jun 13, 2018 at 18:34

2 Answers 2

3

I am not sure if I am getting this right, what is wrong with this approach?

class C1:
    def __init__(self, a, b):
        self.c = a + b

x = 5
y = 3

class C2:
    obj1 = C1(x,y)
    def __init__(self):
        self.d = 1
        print(self.obj1.c)

obj2 = C2()

Another approach,

class C2:
    obj1 = None
    def __init__(self,ob):
        self.d = 1
        self.obj1 = ob

    def show(self):
        print(self.obj1.c)

x = 5
y = 3
obj1 = C1(x,y)
obj2 = C2(obj1)
obj2.show()
Sign up to request clarification or add additional context in comments.

5 Comments

But in that case, I have to define x and y before defining class C2? Doesn't seem a very systematic solution. No other way that I can give it to this class as an input?
Updated the answer
Thanks, this one is exactly what I wanted.
Welcome, please mark this as correct if it was helpful.
Oh, one issue here is that you defined an instance of class C1 and then use it for class C2. I cannot do that for my code. But, it was a nice solution. The other solution addresses this issue as well.
2

I think you should look at using classmethod decorator. You have to call the class method once by class/instance object and variable will be shared across instances until further modified.

class C1:
   def __init__(self, a, b):
       self.c = a + b

class C2:
   obj1 = None
   def __init__(self):
      self.d = 1

   @classmethod
   def class1(cls, x, y):
       cls.obj1 = C1(x, y)

C2.class1(2, 3)

obj2 = C2()
print(obj2.obj1.c)  # Output would be 5
obj22 = C2()
print(obj22.obj1.c) # Output would be same as above i.e 5

Hope it will help!

2 Comments

Thanks, this one is exactly what I wanted.
There was another correct answer before your solution. I guess I had to mark that one as correct. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.