chrisg
chrisg

Reputation: 41655

Class Inheritance through Multiple Classes

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.

Upvotes: 1

Views: 3542

Answers (2)

batbrat
batbrat

Reputation: 5221

I'd suggest:

File1

class Class3:
    def __init__(self):
        #I'd really like to do self.string1 = "" or something concrete here.
        pass

    def printValue(self):
        #Add exception handling
        #self may not have a `string1`
        print self.string1

File2

from File1 import Class3

class Class2(Class3):
    def __init__(self):
        Class3.__init__(self)
        self.string1 = 'Hello'

    def e(self):
        self.value4 = self.value3 - self.value2
        print self.value4
        self.printValue()

File3

from File2 import Class2

class Class1(Class2):
    def __init__(self):
        Class2.__init__(self)
        self.value1 = 10
        self.value2 = 20

    def add(self):
        self.value3 = self.value1 + self.value2


if __name__ == '__main__':
    obj1 = Class1()
    #The order of calling is important! value3 isn't defined until add() is called
    obj1.add()
    obj1.e()
    print obj1.value3

I have assumed a total absence of multiple inheritance [Use super() if you need cooperative MI + google: Why python's super is considered harmful!]. All in all, truppo's answer might be exactly what you need. My answer just points out, what in my very subjective opinion is a better way to achieve what you need. I'd also suggest using new style classes.

Upvotes: 0

mthurlin
mthurlin

Reputation: 27285

It is kind of hard to understand what you are trying to do as your code does not even run. I think something like this is what you are trying to do:

class Class3():

    def printValue(self):
        print self.string1

class Class2(Class3):

    def e(self):
        self.value4 = self.value3 - self.value2
        print self.value4
        self.string1 = 'Hello'
        self.printValue()

class Class1(Class2):

    def __init__(self):
        self.value1 = 10
        self.value2 = 20

    def add(self):
        self.value3 = self.value1 + self.value2


if __name__ == '__main__':
    instance1 = Class1()
    instance1.add()
    instance1.e() # will print "10" and "Hello"
    print instance1.value3 # will print "30"

Upvotes: 1

Related Questions