[Tutor] Calling variable from two different classes

Alan Gauld alan.gauld at btinternet.com
Fri Feb 13 20:18:50 CET 2009


"Hi" <haztang17 at gmail.com> wrote

>I want to be able to call a variable from one of my classes to my 
>main class
> (interface layout) and have it update every minute.

Sp provuide an update method in the class that has the variable.
Do not pass the variable to another class to update it, Objects
should always do it to themselves.

> wxPython. Is it better to be in the main class and read the variable 
> or
> should I have the class the variable resides in to send the variable 
> along
> to the main class?

Neither its better for the mainclass to update the variable by
sending a message to the class with the variable. Now the
next question is what determines when it needs to be updated?

In Python reading the data from a class is considered OK - in most
OOP languages its considered better to write a "getter" method.

But if you need to display and set it in your main class are you
sure the variable shouldn't be stored in the main class? Is it part
of the display or part of the underlying logic that the GUI is 
controlling?
If the latter I'd go for using a getter and an update method but if 
its
just a display feature move it to the main class.

What you really want to eliminate is any knowledge of the GUI in the
application classes. A little bit of knowledge of the app classes by
the GUI is not so bad.

HTH,
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list