Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'operator': 0.03; 'definitions.': 0.07; 'indexing': 0.07; 'method.': 0.07; 'skip:` 10': 0.07; 'suppose': 0.07; '*is*': 0.09; 'arrays': 0.09; 'currently,': 0.09; 'differently.': 0.09; 'expected.': 0.09; 'implements': 0.09; 'imply': 0.09; 'api': 0.11; 'python': 0.11; 'jan': 0.12; "(it's": 0.16; '70%': 0.16; 'argument.': 0.16; 'behave': 0.16; 'boolean': 0.16; 'comparisons,': 0.16; 'finite': 0.16; 'inverse': 0.16; 'item)': 0.16; 'itself,': 0.16; 'magic': 0.16; 'mark.': 0.16; 'mechanics.': 0.16; 'numpy': 0.16; 'of)': 0.16; 'operator.': 0.16; 'operators.': 0.16; 'quantum': 0.16; 'reedy': 0.16; 'subject:`': 0.16; 'task.': 0.16; 'two,': 0.16; 'which,': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'module': 0.19; 'translated': 0.19; 'fit': 0.20; 'seems': 0.21; 'appears': 0.22; 'manual': 0.22; 'separate': 0.22; 'header:User- Agent:1': 0.23; 'case.': 0.24; 'exists': 0.24; 'mathematical': 0.24; 'fine': 0.24; 'sort': 0.25; 'class.': 0.26; 'define': 0.26; 'second': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'correct': 0.29; 'leave': 0.29; '(this': 0.29; 'am,': 0.29; "doesn't": 0.30; "i'm": 0.30; 'membership': 0.31; 'argue': 0.31; 'behaving': 0.31; 'commonly': 0.31; 'operators': 0.31; 'received:132': 0.31; '"the': 0.34; 'skip:_ 10': 0.34; 'maybe': 0.34; 'could': 0.34; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'located': 0.36; 'really': 0.36; 'false': 0.36; 'module.': 0.36; 'done': 0.36; 'method': 0.36; 'useful': 0.36; 'should': 0.36; 'received:209': 0.37; 'expected': 0.38; 'implement': 0.38; 'represent': 0.38; 'skip:o 20': 0.38; 'message-id:@gmail.com': 0.38; 'rich': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'little': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'most': 0.60; 'tell': 0.60; 'new': 0.61; 'details.': 0.61; 'matter': 0.61; "you're": 0.61; 'first': 0.61; 'more': 0.64; 'chance': 0.65; '(that': 0.65; 'land': 0.65; 'believe': 0.68; 'useful.': 0.68; 'default': 0.69; 'square': 0.74; 'special': 0.74; 'behavior': 0.77; '50%': 0.78; 'believe,': 0.84; 'hand.': 0.84; 'subject:skip:o 10': 0.84; 'tricky': 0.84; 'outset': 0.91; '30%': 0.93; 'suited': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:message-id:date:from:user-agent:mime-version:to:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=saLSLwHdI6Sr7Ixt9hTm33hsGhO1rQtyksFSZOjon4M=; b=JR5wcDHpdoe4eVxWX8Pm471i3lAqyPkjGAbVaO5IybqeGDcP6VR1GId4B+buU2iRqU 0PX1gdvccgw4t3vc9y9x5Hz9FapOJX5Brfpi2FnBSaEca6eKsM6x6sZ6btzrN1SkOqMg UDyloV50vtC2rUi6hd7g9RpPw768pp2irhdWYFeZakM3kemnTJFDl/hK/6Fk5EkLnmsc tHj/IXI0W1iDRSb3fwoPpC9DltT6oUWjtAwC4sDRjQIHisbzM4E3Lmib/O9JcM6Zndi2 Sl0RbsnH7apul6+qDD+uPM9Gsfje20d6Prk5k6iwOuLNkH4Ml21VdEu7AUL4VESdNuc1 t9fQ== X-Received: by 10.58.90.5 with SMTP id bs5mr12350905veb.60.1366399401376; Fri, 19 Apr 2013 12:23:21 -0700 (PDT) Date: Fri, 19 Apr 2013 15:23:19 -0400 From: Matthew Gilson User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.5; rv:14.0) Gecko/20120713 Thunderbird/14.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Feature Request: `operator.not_in` References: <5171543D.708@gmail.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 90 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366399409 news.xs4all.nl 2246 [2001:888:2000:d::a6]:43097 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43932 On 4/19/13 2:27 PM, Terry Jan Reedy wrote: > On 4/19/2013 10:27 AM, Matthew Gilson wrote: > ) It seems to me that the operator module should have a `not_in` or >> `not_contains` function. It seems asymmetric that there exists a >> `is_not` function which implements `x is not y` but there isn't a >> function to represent `x not in y`. > > There is also no operator.in. True. I'm not arguing that there should be ... > There is operator.contains and operator.__contains__. Thankfully :-) > > There is no operator.not_contains because there is no __not_contains__ > special method. (Your point two, which I disagree with.) > But there's also no special method `__is_not__`, but there's a corresponding `is_not` in the operator module so I don't really see that argument. It's a matter of functionality that I'm thinking about in the first part. itertools.starmap(operator.not_in,x,y) vs. itertools.starmap(lambda a,b: a not in b,x,y) Pretty much every other operator in python (that I can think of) has an analogous function in the operator module. >> 2) I suspect this one might be a little more controversial, but it seems >> to me that there should be a separate magic method bound to the `not in` >> operator. > > The reference manual disagrees. > "The operator not in is defined to have the inverse true value of in." I would still leave that as the default behavior. It's by far the most useful and commonly expected. And I suppose if you *can't* have default behavior like that because that is a special case in itself, then that makes this second part of the request dead in the water at the outset (and I can live with that explanation). > >> Currently, when inspecting the bytecode, it appears to me >> that `not x in y` is translated to `x not in y` (this supports item 1 >> slightly). However, I don't believe this should be the case. In >> python, `x < y` does not imply `not x >= y` because a custom object can >> do whatever it wants with `__ge__` and `__lt__` -- They don't have to >> fit the normal mathematical definitions. > > The reason for this is that the rich comparisons do not have to return > boolean values, and do not for numarray arrays which, I believe, > implement the operators itemwise. > Yes, you're correct about numpy arrays behaving that way. It can be very useful for indexing them. It would also be fine for a special method `__not_contains__` to be expected to return a boolean value as well. It could still be very useful. Consider a finite square well from quantum mechanics. I could define `in` for my particle in the square well to return `True` if there is a 70% chance that it is located in the well (It's a wave-function, so it doesn't have a well defined position -- the particle could be anyway, but 7 out of 10 measurements will tell me it's in the well). It might be nice if I could define `not in` to be `True` if there is only a 30% chance that it is in the well. Of course, this leaves us with a no-man's land around the 50% mark. Is it in the well or not? There's no telling. I'm sure you could argue that this sort of thing *could* be done with rich comparisons, but I would consider that a deflection from the point at hand. It seems it should be up to the user to design the API most suited for their task. Or what about a `Fraternity` class. Are the new pledges in the fraternity or not? Maybe they should be considered neither in, nor out until pledge season is over. > > I don't see any reason why containment should behave differently. > > 'Design by analogy' is tricky because analogies often leave out > important details. __contains__ *is* expected to return true/false. > > " object.__contains__(self, item) > Called to implement membership test operators. Should return true > if item is in self, false otherwise" > > -- > Terry Jan Reedy > >