[Python-Dev] [PEP] += on return of function call result (original) (raw)
Luke Kenneth Casson Leighton lkcl@samba-tng.org
Mon, 19 May 2003 12:53:17 +0000
- Previous message: [Python-Dev] [PEP] += on return of function call result
- Next message: [Python-Dev] [PEP] += on return of function call result
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
jeff,
beat bolli's code example:
count[word] = count.get(word, 0) + 1
i think best illustrates what issue you are trying to raise.
okay, we know there are two issues so let's give an example that removes one of those issues:
count = {}
count[word] = count.get(word, []) + ['hello']
the issue is that the difference between the above 'hello' example and this:
count.get(word, []) += ['hello']
is that you don't know what STORE to use after the use of get() in the second example, but you do in the first example because it's explicity set out.
so, does this help illustrate what might be done?
if it's possible to return a result and know what should be done with it, then surely it should be possible to return a result from a += "function" and know what should be done with it?
l.
On Sat, May 17, 2003 at 10:21:39AM -0500, Jeff Epler wrote:
I think that looking at the generated bytecode is useful.
# Running with 'python -O' >>> def f(x): x += 1 >>> dis.dis(f) 0 LOADFAST 0 (x) 3 LOADCONST 1 (1) 6 INPLACEADD 7 STOREFAST 0 (x) *** 10 LOADCONST 0 (None) 13 RETURNVALUE >>> def g(x): x[0] += 1 >>> dis.dis(g) 0 LOADGLOBAL 0 (x) 3 LOADCONST 1 (0) 6 DUPTOPX 2 9 BINARYSUBSCR 10 LOADCONST 2 (1) 13 INPLACEADD 14 ROTTHREE 15 STORESUBSCR *** 16 LOADCONST 0 (None) 19 RETURNVALUE >>> def h(x): x.a += 1 >>> dis.dis(h) 0 LOADGLOBAL 0 (x) 3 DUPTOP 4 LOADATTR 1 (a) 7 LOADCONST 1 (1) 10 INPLACEADD 11 ROTTWO 12 STOREATTR 1 (a) *** 15 LOADCONST 0 (None) 18 RETURNVALUE In each case, there's a STORE step to the inplace statement. In the case of the proposed def j(x): x() += 1 what STORE instruction would you use? >>> [opname for opname in dis.opname if opname.startswith("STORE")] ['STORESLICE+0', 'STORESLICE+1', 'STORESLICE+2', 'STORESLICE+3', 'STORESUBSCR', 'STORENAME', 'STOREATTR', 'STOREGLOBAL', 'STOREFAST', 'STOREDEREF'] If you don't want one from the list, then you're looking at substantial changes to Python.. (and STOREDEREF probably doesn't do anything that's relevant to this situation, though the name sure sounds promising, doesn't it) Jeff
--
expecting email to be received and understood is a bit like picking up the telephone and immediately dialing without checking for a dial-tone; speaking immediately without listening for either an answer or ring-tone; hanging up immediately and then expecting someone to call you (and to be able to call you).
every day, people send out email expecting it to be received without being tampered with, read by other people, delayed or simply - without prejudice but lots of incompetence - destroyed.
please therefore treat email more like you would a CB radio to communicate across the world (via relaying stations): ask and expect people to confirm receipt; send nothing that you don't mind everyone in the world knowing about...
- Previous message: [Python-Dev] [PEP] += on return of function call result
- Next message: [Python-Dev] [PEP] += on return of function call result
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]