global variables or inheritence
pointal at lure.u-psud.fr
Thu Apr 27 17:04:37 CEST 2000
On Wed, 26 Apr 2000 19:37:40 GMT, Thomas Rasmussen
<simpsons at kom.auc.dk> wrote:
>: I have quite a big problem. I have two different files, where I in one
>: file want to declare a variable and then in the other file be able to
>: access and use this variable (no need to change it), but I just can't
>: seem to get it to work... Is there any nice way of doing this easily?
>: I have tried the global var but this doesn't seem to work.
>: Anyone got a good idea?
Be careful that when you write "from module import *", you get
duplication of the imported module public names into your namespace,
with their current referenced objects.
If one module change(*) the object which is referenced in its
namespace, the other module dont see the modification.
var = 12
from mod1 import *
# Here you get var with value 12.
var = 18
# Here you have var with value 18 and mod1 has var with value 12.
To avoid this problem, you must access globals by their modules names.
mod1.var = 18
# And its ok.
Here, to force the use of module prefix, we have adopted the rule to
all times use a _ before globals names, so that they MUST be accessed
by their modules names.
(*) Change the object instance!!! If you have a 'shared' list between
modules, you can do operations on it and all modules ill see them...
except the bad module which create a new list instance and assign it
to its namespace'name.
Laurent POINTAL - CNRS/LURE - Service Informatique Experiences
Tel/fax: 01 64 46 82 80 / 01 64 46 41 48
email : pointal at lure.u-psud.fr ou lpointal at planete.net
More information about the Python-list