1

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.

5
  • 1
    Where is "Test" defined? Why are you shadowing your classes with an instance of it: "Class3 = Class3()"? I have a hard time imagining that this code even runs. Commented Feb 16, 2010 at 12:16
  • The examples override class names with instances. Please clarify. Commented Feb 16, 2010 at 12:18
  • Sorry, Test() was left in it from the script I worte and I forgot to change it. Edited now. Commented Feb 16, 2010 at 12:33
  • Inheritance has a direction. You can't inherit methods from your ancestors but inherit data from your decendants. You have a class tree like this: Class3 -> Class2 -> Class1 (??) Commented Feb 16, 2010 at 13:16
  • @kaizer.se -- I don't want to inherit in both directions, I want to inherit in one direction and pass a variable into a function I am calling. Commented Feb 16, 2010 at 13:37

2 Answers 2

1

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"
Sign up to request clarification or add additional context in comments.

5 Comments

@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
Different files doesn't matter. Just stick the code above in different files and import them like you did in your code.
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.
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.
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
0

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.

3 Comments

Cooperative multiple inheritance needs super, while simple inheritance with mixin classes can do without.
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()
@chrissygormely: The string can be defined in e(). Just like in init Watch out: Don't use it before calling e()!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.