[Python-ideas] Make "is" checks on non-singleton literals errors
Masklinn
masklinn at masklinn.net
Mon Oct 8 22:14:52 CEST 2012
On 2012-10-08, at 22:07 , Guido van Rossum wrote:
> On Mon, Oct 8, 2012 at 12:59 PM, Masklinn <masklinn at masklinn.net> wrote:
>> On 2012-10-08, at 21:48 , 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.
>>
>> How would the rather common pattern of using an `object` instance as a
>> placeholder be handled? An identity test precisely expresses what is
>> meant and desired in that case, while an equality test does not.
>
> It wouldn't be affected. The warning should only be emitted if either
> argument to 'is' is a literal number or string. Even if x could be an
> object instance I still don't see how it would lend meaning to "if x
> is 4:".
I went from the description and missed the "literals" part of "non-singleton
literals".
Sorry about that.
More information about the Python-ideas
mailing list