Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   What does the keyword 'global' really mean (http://www.velocityreviews.com/forums/t322238-what-does-the-keyword-global-really-mean.html)

John Dean 09-09-2003 11:03 AM

What does the keyword 'global' really mean
 
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'?

--
Best Regards
John

John Roth 09-09-2003 11:41 AM

Re: What does the keyword 'global' really mean
 

"John Dean" <john@rygannon.com> wrote in message
news:3f5db3b6$0$252$fa0fcedb@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' ?


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
>
> --
> Best Regards
> John




Michael Peuser 09-09-2003 11:41 AM

Re: What does the keyword 'global' really mean
 

"John Dean" <john@rygannon.com> schrieb im Newsbeitrag
news:3f5db3b6$0$252$fa0fcedb@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





Peter Otten 09-09-2003 12:28 PM

Re: What does the keyword 'global' really mean
 
Michael Peuser wrote:

> (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"
</hidden.py>

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

Michael Peuser 09-09-2003 01:43 PM

Re: What does the keyword 'global' really mean
 

"Peter Otten" <__peter__@web.de
> Michael Peuser wrote:
>
> > (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:


Funny, so it seems....

>
> > (2) You as well have visiblity inside a modul function to variables used
> > in the modul scope, i.e. you do

NOT!!!
>> have to declare them 'global' if you only
> > want to 'read' them.


> Oops!

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






Hans Nowak 09-09-2003 02:58 PM

Re: What does the keyword 'global' really mean
 
John Dean wrote:

> 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 = 42
>>> def f():

x = 3
print x

>>> f()

3
>>> 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:

>>> def g():

global x
x = 6
print x

>>> g()

6
>>> 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:

>>> def h():

print x

>>> h()

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,

--
Hans (hans@zephyrfalcon.org)
http://zephyrfalcon.org/





All times are GMT. The time now is 09:52 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.