Hello,
I have 3 classes and I run the first class and declare a variable in the second class and want the 3rd class to be able to print out this variable. I have code below to explain this more clearly.
from class2 import Class2
class Class1(Class2):
def __init__(self):
self.value1 = 10
self.value2 = 20
def add(self):
self.value3 = self.value1 + self.value2
def multiply(self):
Test.subtract()
if __name__ == '__main__':
Class1 = Class1()
Class1.add()
Class1.Multiply()
The above class calls functions in the second class and the values are used by the functions called.
from class3 import Class3
class Class2(Class3):
def e(self):
self.value4 = self.value3 - self.value2
print self.value4
self.string1 = 'Hello'
Class2.printValue()
Class2 = Class2()
The functions of the 3rd class are called by the 2nd class but the values from the 2nd class are not passed into the 3rd class.
class Class3():
def printValue(self):
print self.string1
if __name__ == '__main__':
Class3 = Class3()
So my question is how do I get the variables in the second class to be passed into the 3rd class when I run the the first class as the main script?
Thanks for any help.
This script is for example purposes only, in my script I have 3 files. I need to start with the 1st class in a file and then use the functions of the 2nd class which in turn create a variable which I then use when I am running a function in the 3rd class. But all this need's to run by executing the first class. All classes have to be in separate files.
Sorry for the confusion, Thanks
I can do this when I just use functions by passing the value through a parameter like:
string1 = 'Hello'
printValue(string1)
Then this value can be used by the printValue
function when it is running. I just can't get it working with Classes as passing parameters seems to be a problem because of self
.