Global surprise

Nick nospam at nnn.com
Wed Nov 24 16:56:14 EST 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





More information about the Python-list mailing list