[Python-ideas] Warning on conditionals
Amber Yust
amber.yust at gmail.com
Tue Dec 17 16:34:38 CET 2013
A better check might be to look for cases where both (a) and/or are used in
a condition and (b) the right-hand side of the and/or is a constant.
On Tue Dec 17 2013 at 7:15:06 AM, MRAB <python at mrabarnett.plus.com> wrote:
> On 17/12/2013 13:46, Peter Otten wrote:
> > f p wrote:
> >
> >> Hi, I'm new here,
> >>
> >> There are cases of bad use of int or str constants without comparison
> >> operators in the if statement as shown in this example:
> >>
> >> http://stackoverflow.com/questions/20633818/python-if-elif-always-not-
> > working.
> >>
> >> Maybe the case in which the if statement is defeated should rate a
> >> warning.
> >
> > 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.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131217/4afaab60/attachment.html>
More information about the Python-ideas
mailing list