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


Groups > comp.lang.python > #52339

Re: Python Basic Doubt

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'argument': 0.05; 'beginner': 0.05; 'true,': 0.05; 'subject:Python': 0.06; 'granted,': 0.07; 'none:': 0.07; 'odd': 0.07; 'expected.': 0.09; 'omit': 0.09; 'def': 0.12; '"""use': 0.16; '"=="': 0.16; '"is"': 0.16; "(i'm": 0.16; 'buggy': 0.16; 'check?': 0.16; 'compares': 0.16; 'comparisons,': 0.16; 'correctness': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'object()': 0.16; 'rhs': 0.16; 'simplest': 0.16; 'substitution': 0.16; 'wanted.': 0.16; 'wrote:': 0.18; 'dependent': 0.19; 'slightly': 0.19; 'example': 0.22; 'aug': 0.22; 'instance,': 0.24; 'versions': 0.24; 'equivalent': 0.26; 'nearly': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'needed.': 0.30; 'message-id:@mail.gmail.com': 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; 'but': 0.35; 'received:google.com': 0.35; 'possible': 0.36; '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; 'simply': 0.61; 'address': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'teach': 0.65; 'between': 0.67; 'believe': 0.68; 'potentially': 0.81; 'device,': 0.91; '2013': 0.98
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; bh=oy+Cnutb8FjyjBI0iZmnpqjT7eKhs9bH+GCgCmzkZog=; b=Vf4jEg/+x+Lh1/+Nw9gUqt/QpHhd3Ffeu4/WkwYelRv/+9+xjKBFbs8mRI6aY6Q0K0 brHb/ykOgJ/MLLZeuwh4VugeUu0QUUEVq84O0O03wlb0jL+jM/MY7bJAvawE1spnDgnp eFRCub0ctumyfFTRyxApO4WyO+3qtKK8d+L9z7i6gqcaeNkqYyTThN/9OmPeEl5EkMe/ KYdJ8pIPpeI3daUppVFIGpb1eH09qeYcuM/kqwe3gRaL2oeVtH1iF5MqGlvnNdbhY1tt rIdRh/oe36cc64FfA0tVOayuLc4yblNqDRgZl45HNK3O0aVENCBrSR1ZCl3NceOSq856 a6/A==
MIME-Version 1.0
X-Received by 10.52.34.40 with SMTP id w8mr2973594vdi.7.1376182851647; Sat, 10 Aug 2013 18:00:51 -0700 (PDT)
In-Reply-To <5206DDED.8030506@islandtraining.com>
References <CAL0E0u6wO_UBniWoSpePvhKhPDG_nf4p1rqYYrGwzoHTqp6ZHA@mail.gmail.com> <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> <5206B527.6080700@islandtraining.com> <CAPTjJmoM=MU2eqa2i_5_RbveXMqScQqu_uFjonwdrZX7Vsc49g@mail.gmail.com> <5206DDED.8030506@islandtraining.com>
Date Sun, 11 Aug 2013 02:00:51 +0100
Subject Re: Python Basic Doubt
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.447.1376183302.1251.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1376183302 news.xs4all.nl 15909 [2001:888:2000:d::a6]:33781
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52339

Show key headers only | View raw


On Sun, Aug 11, 2013 at 1:42 AM, Gary Herron
<gary.herron@islandtraining.com> 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.)

> 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 ==.

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Python Basic Doubt Chris Angelico <rosuav@gmail.com> - 2013-08-11 02:00 +0100

csiph-web