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

Steven D'Aprano steve at pearwood.info
Tue Oct 9 04:03:27 CEST 2012


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.



-- 
Steven



More information about the Python-ideas mailing list