Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Which type should be used when testing static structure appartenance Date: Wed, 18 Nov 2015 02:28:31 +1100 Lines: 26 Message-ID: References: <20151117142739.GC20735@localhost.localdomain> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de OLBsDXISeD+nYa/RcWdHvgIOEELQMZhTIbM8iQk6coYQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'operator': 0.03; 'received:209.85.223': 0.03; 'difference,': 0.07; 'tuple': 0.09; 'tuple.': 0.09; 'stored': 0.10; 'wed,': 0.15; '"is': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'literals': 0.16; 'nicolas': 0.16; 'received:209.85.223.173': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:type': 0.16; 'subject:when': 0.16; 'tuple,': 0.16; 'wrote:': 0.16; 'basically': 0.18; '2015': 0.20; 'proposed': 0.20; 'to:name:python-list@python.org': 0.20; '(the': 0.22; 'am,': 0.23; 'wrote': 0.23; 'represents': 0.23; 'this:': 0.23; 'header:In- Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'fastest': 0.27; 'tend': 0.27; 'becomes': 0.30; 'realize': 0.32; 'doubt': 0.33; 'list': 0.34; 'received:google.com': 0.35; 'nov': 0.35; 'set.': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'expect': 0.37; 'list.': 0.37; 'received:209': 0.38; 'why': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'beat': 0.66; "they're": 0.66; 'here': 0.66; 'saw': 0.77; 'accurately': 0.84; 'chrisa': 0.84; 'difference.': 0.84; 'expresses': 0.84; 'utmost': 0.84; 'choice.': 0.93; 'colleague': 0.93 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:to :content-type:content-transfer-encoding; bh=S4gXR+Hj3ihaqPdkl0jqii+nbOaS/O5jjCERw2wumkM=; b=DGR3vcyM66Q9Bd+FxdktEhXVwe2UU2vqWwWsB38X+iHi64fiX2iPyq7XoKDczCulTN ssHkGoW/pOrM7F2QVKcZaTsG8cdvwiHr55iuQ5UZPeX0Xu3VJMO1tE3YfGm9Xc9pJa+y alLJR3Shs2S5wBAjso0M4M+6xWVZ+Q21/evmTI+xhUjW6HcmpTlf1Q696PuFUBZ2cSqZ LJ8nxbt/mCuIJiN7QNTDn9/j1xbnXR6V84vgYuWAAHfX9A+6HLxStbwaMN/Gavp8aGUJ HFSrxriuL2KOt1ldxtUd7HqU/9XnBwsnuEgkcP8d05utvrjeUxJ4C3hZ0KhJJ/3z7ONB uYWw== X-Received: by 10.107.3.163 with SMTP id e35mr26312135ioi.157.1447774111874; Tue, 17 Nov 2015 07:28:31 -0800 (PST) In-Reply-To: <20151117142739.GC20735@localhost.localdomain> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98929 On Wed, Nov 18, 2015 at 1:27 AM, Nicolas =C3=89vrard wro= te: > I saw just in time because in a review I wrote something like this: > > if operator not in ('where', 'not where') > > and my colleague proposed that I should use a list instead of a tuple. > But reading the mentioned tweet I tend to think that a set would be a > better choice. > > What are your opinion on this issue (I realize it's not something of > the utmost importance but rather a "philosophical" question). Definitely a set. I don't know why it would be better to use a list; there's no advantage here for the list. "x [not] in some_set" accurately represents the concept of "any of these will match". With a two-element list/tuple/set, I doubt there's going to be any significant performance difference, but if anything, I would expect the tuple to be the fastest (because it can be stored as a literal), and the other two need to be built; but CPython's optimizer has me beat there - they're all stored as literals (the list becomes a tuple, the set becomes a frozenset), meaning there's basically no difference. So you're free to use the one that expresses the concept of "is this part of this set of strings", which is the set. ChrisA