Python Forum

Full Version: Calling a class inside other class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
class Class1:

    def __init__(self):
        self.atr1 = value1
        self.atr2 = value2

    def method_1(self):
        ???

class Class2:

    def __init__(self):
        self.atr_new = ???

    def method_2(self):
        ...
Hi, I want these 2 classes to work together. The Class1 has a lot of methods and is doing an important part of my program. The Class2 is just as an addition to Class1. What I want to use this Class2 for is:
After several code lines in Class1 are realised, it has to call Class2 with some of its atributes. Let´s give an example - Class1 creates a tkinter canvas and doing something there, method_1 should call Class2 with self.atr1 and self.atr2 atributes and that method Class2 should according to these 2 atributes do something in that tkinter canvas.

Can you help me with that? How to connect these 2 classes to work so?

Thank you in advance.
to call class2 from class1,
  1. Instantiate class 2 in the class1 __init__ method:
        self.c2 = Class2()
  2. when you want to call Class2 method_2 from class1:
        self.c2.method_2(...)
To do that, you need to instantiate Class2 an attribute in Class1:

class Class1:
 
    def __init__(self, value1, value2, class2):
        self.atr1 = value1
        self.atr2 = value2
        self.class2 = class2
 
    def method_1(self):
        ???
 
class Class2:
 
    def __init__(self):
        self.atr_new = ???
 
    def method_2(self):
        ...

x = Class2()
y = Class1(1, 2, x)
As needed, you can then access the methods and attributes of class2 by calling self.class2 (e.g. self.class2.method_2() would call method_2() of Class2).
Well, thank you. Until here it works, but I came across one another problem. In Class1 and method_1 I create few variables, let´s call them self.var_1, self.var_2. After their creating I want to call Class2 (method_2) with these two variables. But how to call it with something, what is self to the Class1?

class Class1:

def method_1(self):
self.var_1 = value1
self.var_2 = value2
self.class2.method_2(???)

class Class2:

def method_2(???):
...
What to put instead of "???" ?
You have two options:
  1. Pass the two attributes through as arguments to Class2.method_2().
  2. Add an attribute to Class2 to store Class1. This way, each of them exists inside the other and can access attributes and methods from the other at any time.
Use instances, not classes, and keep a pointer to the Class1 instance in Class2
class Class1:
    def __init__(self):
        self.c2 = Class2(self)

    def method1(self):
        self.var1 = 'spam'
        self.var2 = 'eggs'
        self.c2.method2()



class Class2:
    def __init__(self, c1):
        self.c1 = c1

    def method2(self):
        print(self.c1.var1, self.c1.var2)

if __name__ == '__main__':
    guido = Class1()
    guido.method1()
Output:
spam eggs
@Gribouillis :

class Class2:
    def __init__(self, c1):
        self.c1 = c1
What is c1 here? It doesn´t work for me.

@stullis :

When I make it like this:

class Class1:

def __init__(self):
self.c2 = Class2()

class Class2:

def __init__(self):
self.c1 = Class1()
it gets into endless calling each other, without any result.
Like Gribouilis said, you need to use class instances, not the class itself. In his example:

class Class1:
    def __init__(self):
        pass

    def add_class2(self, c2):
        self.c2 = c2

class Class2:
    def __init__(self, c1):
        self.c1 = c1

# Instantiate them
c1 = Class1()
c2 = Class2(c1)
c1.add_class2(c2)
dan789 Wrote:What is c1 here? It doesn´t work for me.
c1 is a Class1 instance that needs to be passed to Class2's constructor. If you run my code above, it necessarily works for you. At line 3 in the call self.c2 = Class2(self), the Class1 instance 'self' is passed as the c1 argument in Class2.__init__()
Thank you, works now and I finally understand how. :)