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: 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: References: <048960da-c132-407f-b1b3-4612a3dd7697@googlegroups.com> <20140609185728.3cac55ab@x34f> <03B8D21C-62B4-4344-AA25-FF82501C33D0@panix.com> Date: Tue, 10 Jun 2014 03:40:32 +1000 Subject: Re: None in string => TypeError? From: Chris Angelico Cc: Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Tue, Jun 10, 2014 at 3:22 AM, Ian Kelly wrote: > On Mon, Jun 9, 2014 at 10:59 AM, Chris Angelico wrote: >> On Tue, Jun 10, 2014 at 2:53 AM, Roy Smith 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 ( at 0x012BE660, file "", line 2>) 3 LOAD_CONST 2 ('x..') 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