Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73054

Re: None in string => TypeError?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'compiler': 0.07; 'none,': 0.07; 'false,': 0.09; 'happen,': 0.09; 'literal': 0.09; 'none)': 0.09; 'repeated': 0.09; 'subject:None': 0.09; 'subject:string': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; "'c',": 0.16; '10:59': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'frozenset': 0.16; 'inclined': 0.16; 'iteration.': 0.16; 'none.': 0.16; 'notation': 0.16; 'roy': 0.16; 'status)': 0.16; 'syntax,': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'first.': 0.19; '>>>': 0.22; 'appears': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'easier': 0.31; 'usually': 0.31; '"",': 0.31; 'constant': 0.31; 'file': 0.32; 'this.': 0.32; 'regular': 0.32; 'checking': 0.33; "i'd": 0.34; 'but': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'keyword': 0.36; 'set.': 0.36; 'subject:?': 0.36; 'should': 0.36; 'list': 0.37; 'clear': 0.37; 'rather': 0.38; 'that,': 0.38; 'little': 0.38; 'moving': 0.39; 'how': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'ian': 0.60; 'skip:i 50': 0.60; "you're": 0.61; 'first': 0.61; 'smith': 0.68; 'construction': 0.72; 'clearer': 0.84; 'optimized.': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=+pkXz1OgEKu1fHSKsdrsKvGo3GbsOfQhPckWvjX+8aU=; b=xiTv+GHJJf0ClgotJxxkvdQNttp6uKq/Yf8nsCjGKVoEKorcJodQLpAWnn/oed0wSK Lb6/EudM1ty2Hd9faKsc5PDu1EsDId9a47rOPweNlVarnWUVp4FPQynBCuN+cPlMuYhj UDVyWYxpA5fVrkxEZkcBP/Ll3Vb1hzwCw8pyKdGye4ZUMqG60Q7ODysqtHZgeqY+cGAJ 9N4EfpINzPehGCNu3MBQMOCGafTprjhU9Yf1xcY2LcY2b+v+kFMb0CAkILAVx354VQTk jGUNnjLm8a0HkFZNHnP6nk2si4k10lqS+DrHmSdPhYRNkkSUnVuJxPLCchgKQy7k1xJW s6iw==
MIME-Version 1.0
X-Received by 10.53.8.162 with SMTP id dl2mr22454420vdd.24.1402335632814; Mon, 09 Jun 2014 10:40:32 -0700 (PDT)
In-Reply-To <CALwzidkRYToie4f385AwyDq0mG2TEqSz0J8si=R0H0ZMFs4vMg@mail.gmail.com>
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 Tue, 10 Jun 2014 03:40:32 +1000
Subject Re: None in string => TypeError?
From Chris Angelico <rosuav@gmail.com>
Cc Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.10931.1402335635.18130.python-list@python.org> (permalink)
Lines 66
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1402335635 news.xs4all.nl 2834 [2001:888:2000:d::a6]:50721
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:73054

Show key headers only | View raw


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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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