[Tutor] imported module/global

Cecilia Alm ebbaalm at uiuc.edu
Mon Apr 16 01:36:16 CEST 2007


OK, apologies if my terminology and wording was unfortunate and unclear.

(Setting aside for now if this is preferred practice or not) In
regards to modifying a global mutable list/dictionary (for example
appending to a global list), the name does not need defined within the
local namespace, right? However, for example for a global integer
counter, the name would need to be defined in the local name space, as
shown by Andreas' previous example.

2007/4/15, Alan Gauld <alan.gauld at btinternet.com>:
> "Cecilia Alm" <ebbaalm at uiuc.edu> wrote
>
> > 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.
>
> 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?
> No.
> 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
> is located.
>
> > 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).
>
> 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.
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
E. Cecilia Alm
Graduate student, Dept. of Linguistics, UIUC
Office: 2013 Beckman Institute


More information about the Tutor mailing list