[Tutor] imported module/global
alan.gauld at btinternet.com
Mon Apr 16 00:22:45 CEST 2007
"Cecilia Alm" <ebbaalm at uiuc.edu> wrote
> Hm, I'm confused by your post. Within the global's module, the
> z" syntax works for bothh modifying and accessing, and makes the
> clearer to read in my opinion.
You seem to be confused about the role of the global keyword.
It is purely used to define a name within a local namespace
(ie inside a function) as being proper to the module's namespace.
It has no role whatsoever in assignment or in access, it
purely controls the namespace. In practice of course
this means that assignments in the local namespace to
a name defined to be global do not create a local version, it
has no affect whatsoever on access.
global in Python is completely different to the type definition
usage found in other languages (eg static in C)
Does using it when access only in required do any harm?
Does using it make the code more readable?
Only very marginally to those familiar with Puython,
but possibly more so to those from other languages.
But using it for access only is non Pythonic and therefore
out of idiom to experienced Python profgrammers which
might lead to confusion rather than clarity. Personally I'd
always argue for using the local languages idioms rather
than changing the idiom to suit new users temporary
disorientation. And it could cause such users to take
away the erroneous idea that global somwhow affected
access, which could cause cognitive issues when dealing
with more convemntional usage..
> (Although, it may not be needed when
> accessing or when modifying mutable types.)
Again it has nothing whatsoever to do with types whether
mutable or not. It is purely about defining where a name
> But, when importing the module ("y") of the global in another
> the syntax "y.z" seems adequate both for accessing and modifying, as
> confirmed by Keith's post (prepending 'global' results in
Because global only applies in the local module.
As Kent suggested global is an unfortunate choice of a name and it
should really be "module", since that is as far as the scope extends.
Author of the Learn to Program web site
More information about the Tutor