Posts: 66
Threads: 19
Joined: Nov 2018
Jan-05-2019, 12:50 PM
(This post was last modified: Jan-05-2019, 12:50 PM by dan789.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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.
Posts: 12,030
Threads: 485
Joined: Sep 2016
to call class2 from class1,
- Instantiate class 2 in the class1 __init__ method:
- when you want to call Class2 method_2 from class1:
Posts: 443
Threads: 1
Joined: Sep 2018
To do that, you need to instantiate Class2 an attribute in Class1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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).
Posts: 66
Threads: 19
Joined: Nov 2018
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?
1 2 3 4 5 6 7 8 9 10 11 |
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 "???" ?
Posts: 443
Threads: 1
Joined: Sep 2018
You have two options:
- Pass the two attributes through as arguments to Class2.method_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.
Posts: 4,787
Threads: 76
Joined: Jan 2018
Use instances, not classes, and keep a pointer to the Class1 instance in Class2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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
Posts: 66
Threads: 19
Joined: Nov 2018
Jan-05-2019, 09:00 PM
(This post was last modified: Jan-05-2019, 09:00 PM by dan789.)
@ Gribouillis :
1 2 3 |
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:
1 2 3 4 5 6 7 8 9 |
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.
Posts: 443
Threads: 1
Joined: Sep 2018
Like Gribouilis said, you need to use class instances, not the class itself. In his example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Class1:
def __init__( self ):
pass
def add_class2( self , c2):
self .c2 = c2
class Class2:
def __init__( self , c1):
self .c1 = c1
c1 = Class1()
c2 = Class2(c1)
c1.add_class2(c2)
|
Posts: 4,787
Threads: 76
Joined: Jan 2018
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__()
Posts: 66
Threads: 19
Joined: Nov 2018
Thank you, works now and I finally understand how. :)
|