strange behavor....

Mark Wooding mdw at distorted.org.uk
Mon Nov 15 06:07:12 EST 2010


Arnaud Delobelle <arnodel at gmail.com> writes:

> >>> exec "a=2" in d
> assigning 2 to 'a'
> >>> d['a']
> 2
>
> >>> exec "global a; a = 3" in d
> >>> d['a']
> 3

Oooh, now isn't that an interesting wrinkle?  I've been careful (without
drawing attention) to restrict my arguments to variables inside
functions, largely because Python's global scopes work completely
differently.  (Python's global scopes work similarly to traditional Lisp
and Scheme, which isn't at all objectionable, but it's not the only way
of managing global scopes.  Queinnec's Lisp In Small Pieces describes
others.)

> So even if the globals() dictionary is custom, its __setitem__ method is
> *not* called.

Fascinating.  Thank you.

-- [mdw]



More information about the Python-list mailing list