[Python-ideas] "While" suggestion
Steven Bethard
steven.bethard at gmail.com
Wed Jul 30 00:04:47 CEST 2008
On Tue, Jul 29, 2008 at 3:19 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> Roman Susi wrote:
>> Python already has dict/list literals + list comprehensions.
>
> Better to think that Python has generator expressions which can be used in
> list, set, and dict comprehensions (the latter two in 3.0 and maybe 2.6).
You probably don't want to think about it that way - a list/set/dict
comprehension does not actually create a generator. Instead, it
basically just inlines the equivalent for loop. Note that there's no
YIELD_VALUE opcode for the comprehensions:
>>> def dict_comp(x, y):
... return {z:x for z in y}
...
>>> def dict_gen(x, y):
... return dict((z, x) for z in y)
...
>>> dis.dis(dict_comp.__code__.co_consts[1])
2 0 BUILD_MAP 0
3 DUP_TOP
4 STORE_FAST 1 (_[1])
7 LOAD_FAST 0 (.0)
>> 10 FOR_ITER 17 (to 30)
13 STORE_FAST 2 (z)
16 LOAD_FAST 1 (_[1])
19 LOAD_DEREF 0 (x)
22 ROT_TWO
23 LOAD_FAST 2 (z)
26 STORE_SUBSCR
27 JUMP_ABSOLUTE 10
>> 30 RETURN_VALUE
>>> dis.dis(dict_gen.__code__.co_consts[1])
2 0 LOAD_FAST 0 (.0)
>> 3 FOR_ITER 17 (to 23)
6 STORE_FAST 1 (z)
9 LOAD_FAST 1 (z)
12 LOAD_DEREF 0 (x)
15 BUILD_TUPLE 2
18 YIELD_VALUE
19 POP_TOP
20 JUMP_ABSOLUTE 3
>> 23 LOAD_CONST 0 (None)
26 RETURN_VALUE
Steve
--
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
--- Bucky Katt, Get Fuzzy
More information about the Python-ideas
mailing list