Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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; 'programmer': 0.03; 'argument': 0.05; 'beginner': 0.05; 'subject:Python': 0.06; '(so': 0.07; 'assignment': 0.07; 'correct.': 0.07; 'granted,': 0.07; 'odd': 0.07; 'plenty': 0.07; 'variables': 0.07; '"if': 0.09; 'beginners': 0.09; 'explanation': 0.09; 'immutable': 0.09; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'sentence': 0.09; 'python': 0.11; 'translation': 0.12; '"=="': 0.16; '"is"': 0.16; "(it's": 0.16; 'be:': 0.16; 'buggy': 0.16; 'c/c++': 0.16; 'check?': 0.16; 'compares': 0.16; 'comparisons,': 0.16; 'correctness': 0.16; 'distinct': 0.16; 'dump': 0.16; 'integer,': 0.16; 'justified': 0.16; 'mutable': 0.16; 'received:67.192.241.150': 0.16; 'received:smtp150.dfw.emailsrvr.com': 0.16; 'rhs': 0.16; 'singleton': 0.16; 'two,': 0.16; 'wanted.': 0.16; ':-)': 0.16; 'language': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'bit': 0.19; 'trying': 0.19; "python's": 0.19; 'slightly': 0.19; 'examples': 0.20; 'fit': 0.20; '>>>': 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; '(or': 0.24; 'received:(smtp server)': 0.26; 'second': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'chris': 0.29; 'feature': 0.29; 'am,': 0.29; "i'm": 0.30; 'about.': 0.31; 'comparison': 0.31; 'equality': 0.31; 'gary': 0.31; 'probably': 0.32; "we're": 0.32; 'entirely': 0.33; 'sense': 0.34; 'could': 0.34; 'agree': 0.35; 'something': 0.35; 'equal': 0.35; 'no,': 0.35; 'objects': 0.35; 'point.': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'that!': 0.36; 'material': 0.36; 'possible': 0.36; 'should': 0.36; 'example,': 0.37; 'too': 0.37; 'two': 0.37; 'easily': 0.37; 'performance': 0.37; 'starting': 0.37; 'step': 0.37; 'being': 0.38; 'same.': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'expensive': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'even': 0.60; 'skip:u 10': 0.60; 'read': 0.60; 'continued': 0.60; 'introduced': 0.61; 'details.': 0.61; 'simply': 0.61; "you're": 0.61; 'first': 0.61; 'save': 0.62; 'advanced': 0.63; 'address': 0.63; 'our': 0.64; 'more': 0.64; 'needing': 0.65; 'talking': 0.65; 'worth': 0.66; 'note:': 0.66; 'between': 0.67; 'other.': 0.75; 'potentially': 0.81; 'ambiguous': 0.84; 'describes': 0.84; 'ear': 0.84; 'lesson.': 0.84; 'coach': 0.91; 'walking': 0.91; '2013': 0.98 X-Virus-Scanned: OK Date: Sat, 10 Aug 2013 21:29:20 -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> <5207034A.6070608@islandtraining.com> In-Reply-To: Content-Type: multipart/alternative; boundary="------------050809090303050500030506" 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: 207 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376195799 news.xs4all.nl 15875 [2001:888:2000:d::a6]:39625 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52357 This is a multi-part message in MIME format. --------------050809090303050500030506 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 08/10/2013 08:43 PM, Chris Angelico wrote: > On Sun, Aug 11, 2013 at 4:21 AM, Gary Herron > wrote: >> On 08/10/2013 06:00 PM, Chris Angelico wrote: >>> 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. > No, I'm not missing your point; I'm disagreeing with it. I think that > 'is' should be taught, that it is every bit as important as '=='; > you're walking down the path of "GOTO considered harmful", of decrying > some particular language feature because it can be misused. // I agree that both "==" and "is" must be taught. But it's the order in which things are introduced which I'm quibbling about. Something like this makes sense (to me): Lesson 1: Use "==" for comparisons, save "is" for a more advanced lesson. Lesson 2: Use "is" for singleton types like "if a is None:" and other easily defined circumstances. Lesson 3: The whole truth, accompanied by a whole chapter's worth of material that describes Python's data model and the difference between value versus identity and assignment versus binding ... A beginner, on his first program or two, can understand 1, and perhaps parrot 2 without understanding (or needing to). But the step from there to 3 is huge. It's folly to dump that on a first-time programmer. (It's probably even folly to dump that on a seasoned programmer just starting in Python. I still remember not understanding the explanation for "is" when I first read it. And it continued to make no sense until I had enough experience to understand the difference betwen C/C++ assignment to variables and Python's binding of variables.) > >>> 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. > Granted, English is a poor litmus test for code. But in this > particular example, we're talking about immutable types (simple > integers), where value and identity are practically the same. A Python > implementation would be perfectly justified in interning *every* > integer, in which case the 'is' would work perfectly here. The > distinction between the two is important when the objects are mutable > (so they have an identity that's distinct from their current values). Granted. But please note: There is *nothing* in that sentence which is fit for a beginner programmer. ... "immutable", "value/identity", "interning" ... In one ear and out the other. :-) > > ChrisA --------------050809090303050500030506 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
On 08/10/2013 08:43 PM, Chris Angelico wrote:
On Sun, Aug 11, 2013 at 4:21 AM, Gary Herron
<gary.herron@islandtraining.com> wrote:
On 08/10/2013 06:00 PM, Chris Angelico wrote:
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.
No, I'm not missing your point; I'm disagreeing with it. I think that
'is' should be taught, that it is every bit as important as '==';
you're walking down the path of "GOTO considered harmful", of decrying
some particular language feature because it can be misused.
 
I agree that both "==" and "is" must be taught.  But it's the order in which things are introduced which I'm quibbling about.  Something like this makes sense (to me):
Lesson 1: Use "==" for comparisons, save "is" for a more advanced lesson.

Lesson 2: Use "is" for singleton types like "if a is None:" and other easily defined circumstances.

Lesson 3: The whole truth, accompanied by a whole chapter's worth of material that describes Python's data model and the difference between value versus identity and assignment versus binding ...
A beginner, on his first program or two, can understand 1, and perhaps parrot 2 without understanding (or needing to).   But the step from there to 3 is huge.  It's folly to dump that on a first-time programmer.  (It's probably even folly to dump that on a seasoned programmer just starting in Python.  I still remember not understanding the explanation for "is" when I first read it.  And it continued to make no sense until I had enough experience to understand the difference betwen C/C++ assignment to variables and Python's binding of variables.)



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.
Granted, English is a poor litmus test for code. But in this
particular example, we're talking about immutable types (simple
integers), where value and identity are practically the same. A Python
implementation would be perfectly justified in interning *every*
integer, in which case the 'is' would work perfectly here. The
distinction between the two is important when the objects are mutable
(so they have an identity that's distinct from their current values).

Granted.  But please note:  There is *nothing* in that sentence which is fit for a beginner programmer.  ... "immutable", "value/identity", "interning" ...  In one ear and out the other. :-)


ChrisA

--------------050809090303050500030506--