Global surprise

Discussion in 'Python' started by Nick, Nov 24, 2004.

  1. Nick

    Nick Guest

    Hello,

    It must be simple but it seems I misunderstand scopes in Python... :(

    Could someone out there please explain to me why this is printed?
    2 {0: 1, 1: 1}
    instead of
    2 {}

    Thanks.
    N.

    ---- test.py ----

    g = 0
    di = {}

    def test():
    global g
    di[g] = 1
    g += 1

    test()
    test()

    print g, di
     
    Nick, Nov 24, 2004
    #1
    1. Advertising

  2. Nick

    Paul Watson Guest

    "Nick" <> wrote in message
    news:co2vp1$ec9$...
    > Hello,
    >
    > It must be simple but it seems I misunderstand scopes in Python... :(
    >
    > Could someone out there please explain to me why this is printed?
    > 2 {0: 1, 1: 1}
    > instead of
    > 2 {}
    >
    > Thanks.
    > N.
    >
    > ---- test.py ----
    >
    > g = 0
    > di = {}
    >
    > def test():
    > global g
    > di[g] = 1
    > g += 1
    >
    > test()
    > test()
    >
    > print g, di


    There is no variable di defined in the test() scope. Therefore, references
    to di use the di defined in the next outer scope, the module di.

    The interactive mode of Python can be very helpful.

    $ python
    Python 2.1 (#15, May 4 2004, 21:22:34) [MSC 32 bit (Intel)] on win32
    Type "copyright", "credits" or "license" for more information.
    >>> g = 0
    >>> di = {}
    >>> def test():

    .... global g
    .... di[g] = 1
    .... g += 1
    ....
    >>> g

    0
    >>> di

    {}
    >>> test

    <function test at 0083844C>
    >>> test()
    >>> g

    1
    >>> di

    {0: 1}
    >>> test()
    >>> g

    2
    >>> di

    {1: 1, 0: 1}
    >>> print g, di

    2 {1: 1, 0: 1}
     
    Paul Watson, Nov 24, 2004
    #2
    1. Advertising

  3. Nick a écrit :
    > Hello,
    >
    > It must be simple but it seems I misunderstand scopes in Python... :(
    >
    > Could someone out there please explain to me why this is printed?
    > 2 {0: 1, 1: 1}
    > instead of
    > 2 {}
    >
    > Thanks.
    > N.
    >
    > ---- test.py ----
    >
    > g = 0
    > di = {}
    >
    > def test():
    > global g
    > di[g] = 1
    > g += 1
    >
    > test()
    > test()
    >
    > print g, di
    >
    >


    Because di is a global variable, modified by the test() function (which
    is a Bad Thing(tm) BTW).

    Try this :
    g = 0
    di = {}

    def test():
    global g
    di = {g : 1}
    g += 1

    Here you create a local di variable, and bind a new dict to it, so it
    doesnt look for another di variable in the enclosing (here the global)
    scope.

    Keep in mind that modifying the state of an object (your code) is not
    the same thing as binding an object to a variable (my code).

    HTH
    Bruno
     
    bruno modulix, Nov 25, 2004
    #3
  4. On Wed, 24 Nov 2004 22:56:14 +0100, "Nick" <> declaimed
    the following in comp.lang.python:

    Let's try some words other than the formal explanations already
    given...
    > ---- test.py ----
    >
    > g = 0
    > di = {}
    >
    > def test():
    > global g


    This essentially means you can rebind the global g, and have it
    take effect in the outer scope.

    > di[g] = 1


    This is not a rebinding of di, but a component access into the
    di object. Since there is no local di object, Python goes out to the
    outer scope to find it... You have "opened" the di "box", and stuck
    something inside the box -- but the box isn't being changed (whereas: di
    = {g:1} is changing the box itself).

    > g += 1


    and this rebinds "g" to a new integer derived from incrementing
    the original binding.


    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Nov 25, 2004
    #4
    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. chris
    Replies:
    1
    Views:
    359
    John C. Bollinger
    Aug 19, 2003
  2. Jacek Dziedzic

    ifstream::get() surprise

    Jacek Dziedzic, Aug 26, 2004, in forum: C++
    Replies:
    5
    Views:
    4,170
    Jacek Dziedzic
    Aug 27, 2004
  3. John Hunter

    operator double() surprise in cxx

    John Hunter, Apr 30, 2004, in forum: Python
    Replies:
    7
    Views:
    356
    =?iso-8859-1?q?Beno=EEt_Dejean?=
    Apr 30, 2004
  4. Roman Suzi

    2.2 <-> 2.3 surprise

    Roman Suzi, May 31, 2004, in forum: Python
    Replies:
    0
    Views:
    342
    Roman Suzi
    May 31, 2004
  5. Shalabh Chaturvedi

    Re: 2.2 <-> 2.3 surprise

    Shalabh Chaturvedi, May 31, 2004, in forum: Python
    Replies:
    1
    Views:
    356
    Duncan Booth
    Jun 1, 2004
Loading...

Share This Page