Python Forum
Calling a class inside other class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling a class inside other class
#1
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.
Reply
#2
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(...)
Reply
#3
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).
Reply
#4
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 "???" ?
Reply
#5
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.
Reply
#6
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
Reply
#7
@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.
Reply
#8
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)
Reply
#9
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__()
Reply
#10
Thank you, works now and I finally understand how. :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class and runtime akbarza 4 208 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 393 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 526 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 580 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Class test : good way to split methods into several files paul18fr 4 380 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Good class design - with a Snake game as an example bear 1 1,703 Jan-24-2024, 08:36 AM
Last Post: annakenna
  question about __repr__ in a class akbarza 4 504 Jan-12-2024, 11:22 AM
Last Post: DeaD_EyE
  error in class: TypeError: 'str' object is not callable akbarza 2 418 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  super() in class akbarza 1 386 Dec-19-2023, 12:55 PM
Last Post: menator01
  error occuring in definition a class akbarza 3 599 Nov-26-2023, 09:28 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020