[Tutor] Mutable data type in python
Laura Creighton
lac at openend.se
Sat Oct 3 14:59:14 CEST 2015
One thing you can do is to make your variable live in another
module. This is a common way to make config files, for instance.
say you have a file called config.py
which contains the single line
my_int = 0
then in your main program you can do:
import config
and start using
config.my_int
wherever you would like to.
of course, you can also do
from config import my_int
and start using my_int
At this point, you aren't any better off that just
using a global, though.
Note that if you do:
config.my_int = 1
or
my_int = 1
the file config.py is not changed. The next time anybody imports it,
they will get the 0 value for my_int.
Laura
More information about the Tutor
mailing list