Problem with imported variable

Shankar SNeelakantan_C at zaplet.com
Fri Nov 2 03:08:17 EST 2001


Hello

> > I have a little question:
> > I have 2 classes "Class1" and "Class2" and several
> > functions in each class. The Class1 is in the file
> > "File1" and the Class2 is in another file "File2". At
> > the beginning of the File1, I import the File2:
> > from File2 import *
> > 
> > In the Class2, I create a variable with a specific
> > value. When the program is running, I modify the value
> > of this variable in the other class (Class1) and then
> > I want to read the new value of the variable farther
> > in the code of the Class2.
> > The problem is that the value does not change in the
> > File2 (Class2). Moreover, as the 2 classes are not in
> > the same files, the global statement does not change
> > the problem.
> > Does someone have an idea?
> > Thanks for your help!
> 
> Someone else may be able to figure out what you want to do, but I'm
> lost. Try posting a very small version of your attempt to do what you
> want, and we'll probably be able to help you.
> 

Actually, I too faced the same problem today. I struggled for nearly
an hour.
If I all code in the same file, everything seems to work. But, in the
name of modularity, when I tried to split my code neatly into 2
different classes, variables of class2 set by methods of class2, but
accessed from methods of class1 seem not to get updated.

> One question: when you say "I modify the value of this variable in the
> other class (Class1) and then I want to read the new value of the
> variable farther in the code of the Class2" are you talking about
> changing a class variable or an instance variable?
> 

Could someone clarify if my notion of class and instance variables is
correct?

Myclass.a                 --> class variable
Myclass.f()               --> class method

h = Myclass.Myclass()
h.a                       --> instance variable
h.f()                     --> instance method

Also, if this differences were correct, how do they change behaviour
of scope of variables and methods ??

-Shankar



More information about the Python-list mailing list