Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73260
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'explicitly': 0.05; 'compiler': 0.07; 'variables': 0.07; 'friday,': 0.09; 'latter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'variables.': 0.09; '{},': 0.09; 'def': 0.12; 'dictionary,': 0.16; 'email name:robert': 0.16; 'false:': 0.16; 'given,': 0.16; 'globals': 0.16; 'key)': 0.16; 'key):': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'variables,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'seems': 0.21; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'subject:/': 0.26; 'header:X -Complaints-To:1': 0.27; '"",': 0.31; '13,': 0.31; 'object.': 0.31; 'produces': 0.31; 'provided,': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; '(2)': 0.35; 'no,': 0.35; 'but': 0.35; 'accessing': 0.36; 'should': 0.36; 'mapping': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'skip:u 10': 0.60; 'skip:n 10': 0.64; 'different': 0.65; 'compare:': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Asymmetry in globals __getitem__/__setitem__ |
| Date | Fri, 13 Jun 2014 12:53:54 +0200 |
| Organization | None |
| References | <mailman.11039.1402597117.18130.python-list@python.org> <bvuvg0FpihtU2@mid.individual.net> <878up1wde6.fsf@elektro.pacujo.net> <d485bfa1-ae0c-44e2-b212-3479ca1762d8@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd9019.dip0.t-ipconnect.de |
| User-Agent | KNode/4.11.5 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.11053.1402656857.18130.python-list@python.org> (permalink) |
| Lines | 69 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1402656857 news.xs4all.nl 2920 [2001:888:2000:d::a6]:53958 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:73260 |
Show key headers only | View raw
robert@robertlehmann.de wrote:
> On Friday, June 13, 2014 8:07:45 AM UTC+2, Marko Rauhamaa wrote:
>>
>> The documentation is a bit vague about it:
>>
>> If only globals is provided, it must be a dictionary, which will be
>> used for both the global and the local variables. If globals and
>> locals are given, they are used for the global and local variables,
>> respectively. If provided, locals can be any mapping object.
>
>
> Interesting. This paragraph explicitly states "locals can be any mapping
> object," but that seems to be false:
>
>
> class Namespace(dict):
> def __getitem__(self, key):
> print("getitem", key)
> def __setitem__(self, key, value):
> print("setitem", key, value)
>
> def fun():
> x # should call locals.__getitem__
No, x is a global here.
> y = 1 # should call locals.__setitem__
>
> exec(fun.__code__, {}, Namespace())
>
>
> Neither __getitem__ nor __setitem__ seem to be called on the local
> variables.
Accessing fun.__code__ is clever, but unfortunately the compiler produces
different bytecodes for loading/storing variables inside a function.
Compare:
>>> import dis
>>> def fun(x=2):
... x
... y = 1
...
>>> dis.dis(fun.__code__)
2 0 LOAD_FAST 0 (x)
3 POP_TOP
3 4 LOAD_CONST 1 (1)
7 STORE_FAST 1 (y)
10 LOAD_CONST 0 (None)
13 RETURN_VALUE
>>> dis.dis(compile("x\ny=2", "<nofile>", "exec"))
1 0 LOAD_NAME 0 (x)
3 POP_TOP
2 4 LOAD_CONST 0 (2)
7 STORE_NAME 1 (y)
10 LOAD_CONST 1 (None)
13 RETURN_VALUE
Only the latter works as advertised:
>>> exec("x\ny=1", {}, Namespace())
getitem x
setitem y 1
Back to comp.lang.python | Previous | Next — Previous in thread | 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