Velocity Reviews > Global surprise

# Global surprise

Nick
Guest
Posts: n/a

 11-24-2004
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
Guest
Posts: n/a

 11-24-2004
"Nick" <(E-Mail Removed)> wrote in message
news:co2vp1\$ec9\$(E-Mail Removed)...
> 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}

bruno modulix
Guest
Posts: n/a

 11-25-2004
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

Dennis Lee Bieber
Guest
Posts: n/a

 11-25-2004
On Wed, 24 Nov 2004 22:56:14 +0100, "Nick" <(E-Mail Removed)> 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.

--
> ================================================== ============ <
> http://www.velocityreviews.com/forums/(E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Hoffa Cisco 1 10-25-2006 06:50 PM Hoffa Cisco 0 10-25-2006 01:04 PM Webster MCSE 6 09-28-2003 10:39 AM Mah - Dee MCSE 6 09-03-2003 10:55 PM JTT MCSE 3 08-15-2003 02:19 AM

Advertisments