[Python-ideas] Warning on conditionals

Andrew Barnert abarnert at yahoo.com
Tue Dec 17 18:07:36 CET 2013


On Dec 17, 2013, at 7:14, MRAB <python at mrabarnett.plus.com> wrote:

> On 17/12/2013 13:46, Peter Otten wrote:
>> When a newbie writes
>> 
>> if animal == "cat" or "dog":
>>     ...
>> 
>> it is clear that he has the wrong idea about how this is evaluated, but in
>> the general case
>> 
>> if animal == cat or animal_barks:
>>     ...
>> 
>> this is not an error. A check would be of very limited use.
>> That said, there is an external tool that issues a warning:
>> 
>> $ cat tmp.py
>> import random
>> a = random.choice("ab")
>> if a == "b" or "c":
>>     print "yes"
>> else:
>>     assert False, "unreachable"
>> $ pychecker tmp.py
>> Processing module tmp (tmp.py)...
>> yes
>> 
>> Warnings...
>> 
>> tmp.py:3: Using a conditional statement with a constant value (c)
> It's the kind of thing that you could have in IDLE.

We have multiple different linters and style checkers for a reason. Until one of them is so clearly perfect that there's no need for the others, I think it would be more useful to make it easier to integrate checkers into IDLE than to try to duplicate their functionality.

Also, a pretty high percentage of newbies don't use IDLE, so this wouldn't solve the problem for most people.


More information about the Python-ideas mailing list