What does the keyword 'global' really mean

J

John Dean

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' ?

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

John Roth

John Dean said:
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' ?

No. What global does is tell the compiler that assignments to that
identifier are at the module level, they are not local to the function or
method.

There is no equivalent to 'extern.' If you want to reference something
in another module, you import the module, then all the identifiers are
availible.
Also, does Python have the equivalent of the 'C' keyword 'static'?

IIRC, static means that it's global to the compilation unit. Python does
not need such a keyword, just bind something at the module or class
level.

John Roth
 
M

Michael Peuser

John Dean said:
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
 
P

Peter Otten

Michael said:
(1) No! When you 'import' a modul all variables (except __...) will be
visible to you.

As far as I know, there is no way to hide a module level variable:

<hidden.py>
x = "alpha"
_y = "beta"
__z = "gamma"
import hidden
dir(hidden) ['__builtins__', '__doc__', '__file__', '__name__', '__z', '_y', 'x']
(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.

Oops! You need not declare them when you do not want to bind them to another
object instance.
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'

If you want to to emulate a "static" variable in a function, as in:

int contrived(void) { static int i=-1; i += 1; return i; }

use

def contrived():
contrived.i += 1
return contrived.i
contrived.i = -1

or (better, but will accept parameters only once)

def contrived():
i = 0
while True:
yield i
i += 1

or a class with a __call__() method
and live happily ever after.

Peter
 
M

Michael Peuser

"Peter Otten" <[email protected]
As far as I know, there is no way to hide a module level variable:

Funny, so it seems....
Oops as well! But this typo should be evident from the context...
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'
If you want to to emulate a "static" variable in a function, as in:

int contrived(void) { static int i=-1; i += 1; return i; }

use

def contrived():
contrived.i += 1
return contrived.i
contrived.i = -1

This has the drawback that 'i' is not so well encapsulated (initialisation
outside) and - thus -visible outside of contrived. But it is certainly
somwhat simpler.

Kindly
Michael P
 
H

Hans Nowak

John said:
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' ?

It's really quite simple. Within a function, assignments are local (by default):
x = 3
print x
42

When executing f(), it creates a local variable x with value 3, and prints it.
The global variable x is unaffected.

You can change this behavior by using the 'global' statement:
global x
x = 6
print x
6

'global x' tells Python that the x in this function is global rather than
local, so 'x = 6' refers to the global variable x we already created. (As a
side note, it may seem like you change an existing variable, but what it
actually does it rebind the name 'x' that already existed in the global namespace.)

As you can see, the global variable x is overwritten and how has a value of 6.

To make matters a bit more confusing, you don't need 'global' to *access* a
global variable from within a function, only when you assign to it. This works:
print x
6

There's no local variable x this time, but 'x' obviously refers to the global one.
Also, does Python have the equivalent of the 'C' keyword 'static'?

No. There are (clumsy) ways to fake it, but you'll probably be better off
writing code in a Pythonic way, rather than trying to emulate a C style.

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top