Confusion re "global" statement

Chris Stromberger bit_bucket5 at hotmail.com
Sun Aug 31 14:41:48 EDT 2003


This doesn't seem like it should behave as it does without using
"global d" in mod().

d = {}

def mod():
  d['hey'] = 3

mod()
print d  

When run, it prints {'hey': 3}.  Seems like it should print {} w/o
using "global d".

Can someone explain?  I guess it has to do with the fact that I'm not
reassigning the name d in the function, but it seems counter-intuitive
that I'm able to modify a global inside the function w/o saying
"global d".

Thanks,
Chris




More information about the Python-list mailing list