[Tutor] imported module/global

Cecilia Alm ebbaalm at uiuc.edu
Sun Apr 15 22:51:20 CEST 2007

2007/4/15, Andreas Kostyrka <andreas at kostyrka.org>:
> * Cecilia Alm <ebbaalm at uiuc.edu> [070415 18:21]:
> > If a module "x" imports module "y" with a global variable "z", then
> > this global can be referred or assigned to in "x" with the syntax
> > "y.z" (no "global" keyword preceding) and changes are accessible to
> > class methods in "y" referring to "global z".
> Well, you don't need the global statement for accessing z. You need it
> for modifying it, or Python will assume that you are working with a
> local variable z.

Hm, I'm confused by your post. Within the global's module, the "global
z" syntax works for bothh modifying and accessing, and makes the code
clearer to read in my opinion. (Although, it may not be needed when
accessing or when modifying mutable types.)

But, when importing the module ("y") of the global in another module,
the syntax "y.z" seems adequate both for accessing and modifying, as
confirmed by Keith's post (prepending 'global' results in NameError).

More information about the Tutor mailing list