Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73232
| Date | 2014-06-12 20:18 +0200 |
|---|---|
| Subject | Asymmetry in globals __getitem__/__setitem__ |
| From | Robert Lehmann <mail@robertlehmann.de> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.11039.1402597117.18130.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
Hi all,
I have noticed there is a slight asymmetry in the way the interpreter
(v3.3.5, reproduced also in v3.5.x) loads and stores globals. While
loading globals from a custom mapping triggers __getitem__ just fine,
writing seems to silently ignore __setitem__.
class Namespace(dict):
def __getitem__(self, key):
print("getitem", key)
def __setitem__(self, key, value):
print("setitem", key, value)
def fun():
global x, y
x # should call globals.__getitem__
y = 1 # should call globals.__setitem__
exec(fun.__code__, Namespace())
# => getitem x
I would have expected "setitem y 1" to show up as well, but to no avail.
Am I doing something wrong? Is this on purpose?
Cheers,
Robert
PS. I found a 3.3.x commit (e3ab8aa
<http://hg.python.org/cpython/rev/e3ab8aa0216c>) which fixed the
LOAD_GLOBAL opcode to support other types than dict, but STORE_GLOBAL seems
to use bare PyDict_SetItem instead of dispatching to PyObject_SetItem.
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Asymmetry in globals __getitem__/__setitem__ Robert Lehmann <mail@robertlehmann.de> - 2014-06-12 20:18 +0200
Re: Asymmetry in globals __getitem__/__setitem__ Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-06-13 12:38 +1200
Re: Asymmetry in globals __getitem__/__setitem__ Marko Rauhamaa <marko@pacujo.net> - 2014-06-13 09:07 +0300
Re: Asymmetry in globals __getitem__/__setitem__ robert@robertlehmann.de - 2014-06-13 03:13 -0700
Re: Asymmetry in globals __getitem__/__setitem__ Peter Otten <__peter__@web.de> - 2014-06-13 12:53 +0200
Re: Asymmetry in globals __getitem__/__setitem__ Paul Sokolovsky <pmiscml@gmail.com> - 2014-06-13 14:28 +0300
Re: Asymmetry in globals __getitem__/__setitem__ Marko Rauhamaa <marko@pacujo.net> - 2014-06-13 15:32 +0300
csiph-web