Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73054
| References | <048960da-c132-407f-b1b3-4612a3dd7697@googlegroups.com> <20140609185728.3cac55ab@x34f> <03B8D21C-62B4-4344-AA25-FF82501C33D0@panix.com> <CAPTjJmqt+2doOdr-UJTMsDu1f1ZnKD+WEp5SY_LGekR3-FsuOg@mail.gmail.com> <CALwzidkRYToie4f385AwyDq0mG2TEqSz0J8si=R0H0ZMFs4vMg@mail.gmail.com> |
|---|---|
| Date | 2014-06-10 03:40 +1000 |
| Subject | Re: None in string => TypeError? |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.10931.1402335635.18130.python-list@python.org> (permalink) |
On Tue, Jun 10, 2014 at 3:22 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Mon, Jun 9, 2014 at 10:59 AM, Chris Angelico <rosuav@gmail.com> wrote:
>> On Tue, Jun 10, 2014 at 2:53 AM, Roy Smith <roy@panix.com> wrote:
>>> In retrospect, I suspect:
>>>
>>> hourly_data = [(t if status in set('CSRP') else None) for (t,
>>> status) in hours]
>>>
>>> is a little cleaner.
>>
>> I'd go with this. It's clearer that a status of 'SR' should result in
>> False, not True. (Presumably that can never happen, but it's easier to
>> read.) I'd be inclined to use set literal syntax, even though it's a
>> bit longer - again to make it clear that these are four separate
>> strings that you're checking against.
>
> Depending on how much work this has to do, I might also consider
> moving the set construction outside the list comprehension since it
> doesn't need to be repeated on every iteration.
Set literal notation will accomplish that, too, for what it's worth.
>>> def x():
hourly_data = [(t if status in {'C','S','R','P'} else None) for (t,
status) in hours]
>>> dis.dis(x)
2 0 LOAD_CONST 1 (<code object <listcomp> at
0x012BE660, file "<pyshell#10>", line 2>)
3 LOAD_CONST 2 ('x.<locals>.<listcomp>')
6 MAKE_FUNCTION 0
9 LOAD_GLOBAL 0 (hours)
12 GET_ITER
13 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
16 STORE_FAST 0 (hourly_data)
19 LOAD_CONST 0 (None)
22 RETURN_VALUE
>>> dis.dis(x.__code__.co_consts[1])
2 0 BUILD_LIST 0
3 LOAD_FAST 0 (.0)
>> 6 FOR_ITER 36 (to 45)
9 UNPACK_SEQUENCE 2
12 STORE_FAST 1 (t)
15 STORE_FAST 2 (status)
18 LOAD_FAST 2 (status)
21 LOAD_CONST 5 (frozenset({'R', 'S', 'C', 'P'}))
24 COMPARE_OP 6 (in)
27 POP_JUMP_IF_FALSE 36
30 LOAD_FAST 1 (t)
33 JUMP_FORWARD 3 (to 39)
>> 36 LOAD_CONST 4 (None)
>> 39 LIST_APPEND 2
42 JUMP_ABSOLUTE 6
>> 45 RETURN_VALUE
>>> isinstance(x.__code__.co_consts[1].co_consts[5],set)
False
Interestingly, the literal appears to be a frozenset rather than a
regular set. The compiler must have figured out that it can never be
changed, and optimized.
Also, this is the first time I've seen None as a constant other than
the first. Usually co_consts[0] is None, but this time co_consts[4] is
None.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
None in string => TypeError? Roy Smith <roy@panix.com> - 2014-06-09 08:34 -0700
Re: None in string => TypeError? Ryan Hiebert <ryan@ryanhiebert.com> - 2014-06-09 10:42 -0500
Re: None in string => TypeError? Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-09 09:50 -0600
Re: None in string => TypeError? Paul Sokolovsky <pmiscml@gmail.com> - 2014-06-09 18:57 +0300
Re: None in string => TypeError? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-06-09 16:14 +0000
Re: None in string => TypeError? Chris Angelico <rosuav@gmail.com> - 2014-06-10 02:31 +1000
Re: None in string => TypeError? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-06-09 16:07 +0000
Re: None in string => TypeError? MRAB <python@mrabarnett.plus.com> - 2014-06-09 17:06 +0100
Re: None in string => TypeError? Shiyao Ma <i@introo.me> - 2014-06-10 00:13 +0800
Re: None in string => TypeError? Roy Smith <roy@panix.com> - 2014-06-09 12:53 -0400
Re: None in string => TypeError? Chris Angelico <rosuav@gmail.com> - 2014-06-10 02:59 +1000
Re: None in string => TypeError? Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-09 11:22 -0600
Re: None in string => TypeError? Chris Angelico <rosuav@gmail.com> - 2014-06-10 03:40 +1000
Re: None in string => TypeError? Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-09 11:58 -0600
Re: None in string => TypeError? Chris Angelico <rosuav@gmail.com> - 2014-06-10 04:02 +1000
csiph-web