views:

116

answers:

2

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.

+1  A: 

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"
truppo
@truppo -- The classes are in different files. This code will run if I remove `self.string1` from `class3`. I'm trying to get the 3rd class to be able to print out `string1`. If I was to do this between `class1` and `class2`
chrissygormley
Different files doesn't matter. Just stick the code above in different files and import them like you did in your code.
truppo
@truppo -- I have tried this code in different files and this doesn't work. I get the error `self.printValue() TypeError: 'int' object is not callable` Calling `printValue()` doesn't work with `self`.
chrissygormley
The code above works (in python 2.6 atleast), even if split to multiple files. If it doesnt, you have not replicated it exactly. Perhaps you have old "class2/3.py" lying around.
truppo
@truppo -- Sorry about that I had the class named `r` and when I changed the name it worked. I had a variable in my program with the same name. Thanks for the solution
chrissygormley
A: 

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.

batbrat
*Cooperative* multiple inheritance needs super, while simple inheritance with mixin classes can do without.
kaizer.se
+1 for the comment. I'm editing to fix my answer.
batbrat
@batbrat -- I give you plus one as this work's, but the variable `string1` needs to be defined in the function `e()` not in `__init__`. Is there any way of doing that as the variable in my code is created further down the code in the function `e()`
chrissygormley
@chrissygormely: The string can be defined in e(). Just like in __init__ Watch out: Don't use it before calling e()!
batbrat