[Python-ideas] Make "is" checks on non-singleton literals errors

Guido van Rossum guido at python.org
Tue Oct 9 04:14:37 CEST 2012


On Mon, Oct 8, 2012 at 7:03 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Mon, Oct 08, 2012 at 12:48:07PM -0700, Guido van Rossum wrote:
>> On Mon, Oct 8, 2012 at 12:44 PM, Mike Graham <mikegraham at gmail.com> wrote:
>> > I regularly see learners using "is" to check for string equality and
>> > sometimes other equality. Due to optimizations, they often come away
>> > thinking it worked for them.
>> >
>> > There are no cases where
>> >
>> >     if x is "foo":
>> >
>> > or
>> >
>> >    if x is 4:
>> >
>> > is actually the code someone intended to write.
>> >
>> > Although this has no benefit to anyone but new learners, it also
>> > doesn't really do any harm.
>>
>> I think the best we can do is to make these SyntaxWarnings. I had the
>> same thought recently and I do agree that these are common beginners
>> mistakes that can easily hide bugs by succeeding in simple tests.
>
> In my experience beginners barely read error messages, let alone
> warnings.
>
> A SyntaxWarning might help intermediate users who have graduated beyond
> the stage of "my program doesn't work, please somebody fix it", but I
> believe that at best it will be ignored by beginners, if not actively
> confuse them. And I expect that most intermediate users will have
> already learned enough not to use "is" when then mean "==".
>
> So I'm -0 on doing anything to "fix" this. Many things in Python are
> potentially misleading:
>
> array = [[0]*10]*10
>
> On the other hand, I must admit that I've been known to accidently write
> "if x is 0:", so perhaps the real benefit is to prevent silly brainos
> (like typos -- thinkos perhaps?) among more experienced coders. Perhaps
> I should increase my vote to +0.

Exactly. Pragmatically, in large code bases this occurs frequently
enough to worry about it, and (unlike language warts like the aliasing
problem you alluded to above) it serves no useful purpose. I have seen
this particular mistake reported many times in Google's extensive
Python codebase.

Maybe we should do something more drastic and always create a new,
unique constant whenever a literal occurs as an argument of 'is' or
'is not'? Then such code would never work, leading people to examine
their code more closely. I betcha we have people who could change the
bytecode compiler easily enough to do that. (I'm not seriously
proposing this, except as a threat of what we could do if the
SyntaxWarning is rejected. :-)

-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list