Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.109.133.87.MISMATCH!newsfeed.xs4all.nl!newsfeed1.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.05; 'beginner': 0.05; 'true,': 0.05; 'subject:Python': 0.06; 'correct.': 0.07; 'granted,': 0.07; 'none:': 0.07; 'odd': 0.07; 'plenty': 0.07; '"if': 0.09; 'beginners': 0.09; 'expected.': 0.09; 'omit': 0.09; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'sentence': 0.09; 'def': 0.12; 'translation': 0.12; '"""use': 0.16; '"=="': 0.16; '"is"': 0.16; "(i'm": 0.16; 'be:': 0.16; 'buggy': 0.16; 'check?': 0.16; 'compares': 0.16; 'comparisons,': 0.16; 'correctness': 0.16; 'object()': 0.16; 'rhs': 0.16; 'simplest': 0.16; 'substitution': 0.16; 'wanted.': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; 'dependent': 0.19; 'slightly': 0.19; 'examples': 0.20; '>>>': 0.22; 'example': 0.22; 'aug': 0.22; 'header:User-Agent:1': 0.23; 'certainly': 0.24; 'instance,': 0.24; 'precise': 0.24; 'received:emailsrvr.com': 0.24; 'sorry,': 0.24; 'specify': 0.24; 'versions': 0.24; 'equivalent': 0.26; 'nearly': 0.26; 'received:(smtp server)': 0.26; 'second': 0.26; 'header:In-Reply- To:1': 0.27; 'correct': 0.29; 'chris': 0.29; 'am,': 0.29; 'needed.': 0.30; 'comparison': 0.31; 'equality': 0.31; 'gary': 0.31; 'up.': 0.33; 'cases': 0.33; 'entirely': 0.33; 'device': 0.34; 'could': 0.34; 'common': 0.35; 'something': 0.35; 'equal': 0.35; 'no,': 0.35; 'point.': 0.35; 'but': 0.35; 'there': 0.35; 'that!': 0.36; 'possible': 0.36; 'should': 0.36; 'too': 0.37; 'performance': 0.37; 'being': 0.38; 'whatever': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'that,': 0.38; 'expensive': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'skip:u 10': 0.60; 'details.': 0.61; 'simply': 0.61; "you're": 0.61; 'advanced': 0.63; 'address': 0.63; 'skip:n 10': 0.64; 'our': 0.64; 'more': 0.64; 'teach': 0.65; 'between': 0.67; 'believe': 0.68; 'potentially': 0.81; 'ambiguous': 0.84; 'coach': 0.91; 'device,': 0.91; '2013': 0.98 X-Virus-Scanned: OK Date: Sat, 10 Aug 2013 20:21:46 -0700 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130803 Thunderbird/17.0.8 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Python Basic Doubt References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> <5206B527.6080700@islandtraining.com> <5206DDED.8030506@islandtraining.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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376191905 news.xs4all.nl 15887 [2001:888:2000:d::a6]:46607 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52351 On 08/10/2013 06:00 PM, Chris Angelico wrote: > On Sun, Aug 11, 2013 at 1:42 AM, Gary Herron > wrote: >> On 08/10/2013 03:09 PM, Chris Angelico wrote: >>> _notpassed = object() >>> def frob(appendage, device=_notpassed): >>> """Use some appendage to frob some device, or None to frob nothing. >>> Omit device to frob whatever is currently held in that appendage""" >>> if device is _notpassed: >>> device = ... # whatever you need >>> if device is not None: >>> # frob the device >>> >>> But granted, equality comparisons are a LOT more common than identity >>> comparisons. >>> >>> ChrisA >> >> Everything you say is true, and even reasonable for those who know what's >> up. >> >> But for each of your examples, using "==" is equivalent to using "is". Each >> of >> if something == None >> if device == _not passed >> if device != None >> would all work as expected. In none of those cases is "is" actually needed. > Wrong. If you do equality comparisons, it's entirely possible for > something to be passed in that compares equal to the RHS without > actually being it, so "is" is precisely what's wanted. (Plus, why go > through a potentially expensive comparison check when you can simply > check object identity - which could be, for instance, an address > check? But performance is a distant second to correctness here.) You're missing my point. Our knee-jerk reaction to beginners using "is" should be: Don't do that! You almost certainly want "==". Consider "is" an advanced topic. Then you can spend as much time as you want trying to coach them into an understanding of the precise details. But until they have that understanding, they are well served by a rule-of-thumb that says: Use "==" not "is" for comparisons. > >> Given that, and the implementation dependent oddities, I still believe that >> it is *highly* misleading to teach a beginner about "is". >> >> Here's a challenge: What is the simplest non-contrived example where an >> "is" comparison is *required*. Where substitution of an "==" comparison >> would cause the program to fail or be significantly less efficient? (I'm >> not including the nearly immeasurably small timing difference between >> v==None and v is None.) > All it takes is a slightly odd or buggy __eq__ implementation and the > == versions will misbehave. To check if an argument is something, you > use "is", not ==. No, sorry, but any use of the word "is" in an English sentence is way too ambiguous to specify a correct translation into code. To check "if a calculation of some value is a million", you'd write value == 1000000 not value is 1000000 even though there are plenty of other examples where "is" would be correct. > > ChrisA