What does the keyword 'global' really mean

Discussion in 'Python' started by John Dean, Sep 9, 2003.

  1. John Dean

    John Dean Guest

    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 Dean, Sep 9, 2003
    #1
    1. Advertising

  2. John Dean

    John Roth Guest

    "John Dean" <> wrote in message
    news:3f5db3b6$0$252$...
    > 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
     
    John Roth, Sep 9, 2003
    #2
    1. Advertising

  3. "John Dean" <> schrieb im Newsbeitrag
    news:3f5db3b6$0$252$...
    > 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
     
    Michael Peuser, Sep 9, 2003
    #3
  4. John Dean

    Peter Otten Guest

    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
     
    Peter Otten, Sep 9, 2003
    #4
  5. "Peter Otten" <
    > 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
     
    Michael Peuser, Sep 9, 2003
    #5
  6. John Dean

    Hans Nowak Guest

    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 ()
    http://zephyrfalcon.org/
     
    Hans Nowak, Sep 9, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Niu Xiao

    Re: what does global char x[][444] mean?

    Niu Xiao, Oct 21, 2005, in forum: C Programming
    Replies:
    13
    Views:
    561
    Tim Rentsch
    Nov 1, 2005
  2. Steven T. Hatton

    What does "formatted" I/O really mean?

    Steven T. Hatton, Aug 1, 2005, in forum: C++
    Replies:
    2
    Views:
    362
    Dietmar Kuehl
    Aug 1, 2005
  3. Casey
    Replies:
    9
    Views:
    274
    Robert Kern
    Oct 4, 2007
  4. Sébastien de Mapias

    What does "persist" really mean ?

    Sébastien de Mapias, Jul 30, 2008, in forum: Java
    Replies:
    11
    Views:
    2,572
    Arne Vajhøj
    Aug 2, 2008
  5. Hongyu
    Replies:
    2
    Views:
    298
    Hongyu
    Aug 22, 2008
Loading...

Share This Page