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

Joshua Landau joshua.landau.ws at gmail.com
Mon Oct 8 22:38:31 CEST 2012


On 8 October 2012 21:26, Barry Warsaw <barry at python.org> wrote:

> On Oct 08, 2012, at 03:44 PM, Mike Graham 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.
>
> Conversely, I often see this:
>
>     if x == None
>
> and even
>
>     if x == True
>
> Okay, so maybe these are less harmful than the original complaint, but
> still,
> yuck!
>

We can't really warn against these.

>>> class EqualToTrue:
> ...     def __eq__(self, other):
> ...             return other is True
> ...
> >>> EqualToTrue() is True
> False
> >>> EqualToTrue() == True
> True
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20121008/fe75e2fb/attachment.html>


More information about the Python-ideas mailing list