[Tutor] Modules - problem

Emil Styrke emil at lysator.liu.se
Fri Oct 18 16:21:00 EDT 2002


"A" <printers at 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 at python.org
> http://mail.python.org/mailman/listinfo/tutor





More information about the Python-list mailing list