2

Ok I have two classes and one is dependent on the other. So I need to get a variable from the first class and use it in the second. But the second variable is constantly changing. Like this:

class class1 :
    var1 = 0
    def meth1 (self):
        self.var1 += 1
class class2:
    var2 = class1.var1
    def see (self):
        return self.var2
obj1 = class1()
obj2 = class2()

obj1.meth1()
obj2.see()

This would return 0 not 1. If I say print var1 in class one it prints that changed var. But when class2 gets it it is still 0... I guess it is still referring to the old var1. What am I doing wrong and what should I be doing?

Thanks

3
  • 1
    First. Please Capitalize class names. Second. Why are you not using class-level variables in the first place? Your var1 is not an instance variable. Why are you avoiding ordinary instance variables? Commented Sep 7, 2011 at 19:11
  • What is an instance variable? Commented Sep 7, 2011 at 19:25
  • That's a whole new question. 1. Actually do the entire Python tutorial. 2. Search for "Python Instance Variable". 3. Ask the question again, after doing the entire tutorial and reading the hundreds of web pages on Python instance variables. It's a fundamental concept, and hard to cover in a comment on an unrelated question. Commented Sep 7, 2011 at 19:28

2 Answers 2

6
class class2:
    var2 = class1.var1

This is a COPY of the current value of class1.var1.

"I need to get a variable from the first class and use it in the second"

Use class1.var1 instead of making a copy of the current value in var2.

Sign up to request clarification or add additional context in comments.

2 Comments

OH so when i change var1, var2 is still pointing to what var1 unsed to be? Now that makes sense. Thanks for the help!
Very useful article which will help to understand variables in python and how to make correct copies of them: henry.precheur.org/python/copy_list
0

All information on how variables are coppied is true, and the recommended article is well explained. But if you use class1.var1 you will never get the value og the self.var1 from class1. You can check why with the following code:

class Class1 :
var1 = 0
def meth1 (self):
    self.var1 += 1
    print(id(self.var1))

class Class2:
def meth2 (self):
    print(id(Class1.var1))

obj1 = Class1()
obj2 = Class2()

obj1.meth1()
obj2.meth2()

Where you get different ids for Class1.var1 ( which is var1 in line 2) and self.var1.

If you want to get the value of self.var1 from Class2, you must initialize var1 outside the class, and make it global inside the function where you add it a number:

var1 = 0
class class1 :

    def meth1 (self):
        global var1
        var1 += 1

class class2:

    def meth2 (self):
        print(var1)

obj1 = class1()
obj2 = class2()

obj1.meth1()
obj2.meth2()

Comments

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.