So we have two classes: Test and TestManager. Every Test object should have its manager, instantiated on its __init__ function. Also, in order to create a TestManager object, you need to pass a Test instance to it.
How can we do that? I have this, but doesn't work:
class SubTest:
def __init__(self, test):
print(test.getText())
class Test:
def __init__(self):
self.text = "Hello world!"
SubTest(Test) # In Java this would be something like 'new SubTest(this);'
def getText(self):
return self.text
Test()
Thanks!
CodePudding user response:
When passing a Test to SubTest, refer to the class as 'self', meaning your code should look a little something like this:
class Test:
def __init__(self):
self.text = "Hello world!"
SubTest(self) # Passing self, instead of 'Test'
And your full code like this:
class SubTest:
def __init__(self, test):
print(test.getText())
class Test:
def __init__(self):
self.text = "Hello world!"
SubTest(self)
def getText(self):
return self.text
Test()
