[Tutor] Modules - problem
Emil Styrke
emil@lysator.liu.se
18 Oct 2002 22:21:00 +0200
"A" <printers@sendme.cz> writes:
> Hi,
> I would like to access a variable in one module from another module.For example
>
> The main module is like this
> #############
> #Main modul z1.py
> import z3
> print "x variable=", z3.x
> ###########
>
> where z3.py module is like
>
> ########
> #My module z3.py
> global x
> x=11
> #########
> Now if I run z1.py for the first time it prints properly
>
> x variable= 11
>
> but if I do any change in z3 module , it has no effect
> For example If I change in z3
> x=12
> I still get
> x variable= 11
> Why?
If you are changing the x value from a function inside z3, have you remembered to declare x global inside that function? Otherwise you would just change the local variable x.
For example, in z3:
def change_x():
global x # You need this declaration, else x will be local.
x = 12
Hope that helps!
/Emil
> Thank you for help.
> Is it possible to name module with a different extension from py? For example is it possible
> to import z3.cod file?
> Ladislav
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor