[Python-ideas] Make "is" checks on non-singleton literals errors
Guido van Rossum
guido at python.org
Tue Oct 9 23:38:19 CEST 2012
On Tue, Oct 9, 2012 at 2:32 PM, Devin Jeanpierre <jeanpierreda at gmail.com> wrote:
> On Mon, Oct 8, 2012 at 10:14 PM, Guido van Rossum <guido at python.org> wrote:
>> 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. :-)
>
> Is this any better than making `x is 0` raise a TypeError with a
> message about what's wrong (as suggested by Mike Graham)?
>
> In both cases, `x is 0` is basically worthless, but at least if it
> raises an exception people can understand what "went wrong", because
> of the error message that comes with the exception.
But it's not a runtime error. It should depend on whether a literal is
used in the source code, not whether the argument is an int. (There
are tons of situations where it makes sense to dynamically compare two
objects that may happen to be ints using 'is' -- just not when it's a
literal.)
So I claim that it should be a message produced during compilation --
or by a lint-like tool, as others have argued.
--
--Guido van Rossum (python.org/~guido)
More information about the Python-ideas
mailing list