Global surprise

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nick

    #1

    Global surprise

    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


  • Paul Watson

    #2
    Re: Global surprise

    "Nick" <nospam@nnn.com > wrote in message
    news:co2vp1$ec9 $1@namru.matavn et.hu...[color=blue]
    > 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[/color]

    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.[color=blue][color=green][color=darkred]
    >>> g = 0
    >>> di = {}
    >>> def test():[/color][/color][/color]
    .... global g
    .... di[g] = 1
    .... g += 1
    ....[color=blue][color=green][color=darkred]
    >>> g[/color][/color][/color]
    0[color=blue][color=green][color=darkred]
    >>> di[/color][/color][/color]
    {}[color=blue][color=green][color=darkred]
    >>> test[/color][/color][/color]
    <function test at 0083844C>[color=blue][color=green][color=darkred]
    >>> test()
    >>> g[/color][/color][/color]
    1[color=blue][color=green][color=darkred]
    >>> di[/color][/color][/color]
    {0: 1}[color=blue][color=green][color=darkred]
    >>> test()
    >>> g[/color][/color][/color]
    2[color=blue][color=green][color=darkred]
    >>> di[/color][/color][/color]
    {1: 1, 0: 1}[color=blue][color=green][color=darkred]
    >>> print g, di[/color][/color][/color]
    2 {1: 1, 0: 1}


    Comment

    • bruno modulix

      #3
      Re: Global surprise

      Nick a écrit :[color=blue]
      > 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
      >
      >[/color]

      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

      Comment

      • Dennis Lee Bieber

        #4
        Re: Global surprise

        On Wed, 24 Nov 2004 22:56:14 +0100, "Nick" <nospam@nnn.com > declaimed
        the following in comp.lang.pytho n:

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

        This essentially means you can rebind the global g, and have it
        take effect in the outer scope.
        [color=blue]
        > di[g] = 1[/color]

        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).
        [color=blue]
        > g += 1[/color]

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


        --[color=blue]
        > =============== =============== =============== =============== == <
        > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
        > wulfraed@dm.net | Bestiaria Support Staff <
        > =============== =============== =============== =============== == <
        > Home Page: <http://www.dm.net/~wulfraed/> <
        > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

        Comment

        Working...