What does the keyword 'global' really mean

Michael Peuser mpeuser at web.de
Tue Sep 9 07:41:48 EDT 2003


"John Dean" <john at rygannon.com> schrieb im Newsbeitrag
news:3f5db3b6$0$252$fa0fcedb at lovejoy.zen.co.uk...
> Hi
> I have been looking through some Python code and I came across the keyword
> 'global'. I have looked through the docs and two or three Python
programming
> books for a full explanation of what 'global' really means. Would I be
> correct in assuming that any variable prefixed with the keyword global
would
> allow that variable to be accessible across translation units, in other
> words global is equivalent to the 'C' keyword 'extern' ?

(1) No! When you 'import' a modul all variables (except __...) will be
visible to you.
(2) You as well have visiblity inside a modul function to variables used in
the modul scope, i.e. you do have to declare them 'global' if you only want
to 'read' them.
(3) It does not work the other way round. It you assign something ('bind' )
to avariable ('name') (what you would call 'local definition' in some other
languages, this variable will not be visible to the outside wortld (thus (1)
is not quite correct ;-) )
(4) This is where 'global' enteres the scene. Declaring a name in a local
(function) context  as 'global' makes is visible to all the modul.
(5) You as well can declare a variable as 'global' in the modul context; I
am not aware of any use there, except purely informative.


> Also, does Python have the equivalent of the 'C' keyword 'static'?

Not as a special construct, but you can use real 'dummy keyword parameters'
for that:

def p(p1,p2,....myown={}):
myown[....] =

This trick will emulate something similar to 'static'


Kindly
MichaelP








More information about the Python-list mailing list