[Python-Dev] ACCEPTED: PEP 285

David Eppstein eppstein at ics.uci.edu
Wed Apr 3 18:45:21 EST 2002


In article <mailman.1017874091.20985.python-list at python.org>,
 David Ascher <DavidA at ActiveState.com> wrote:

> Would it not be "relatively" easy to add a compiler-time warning for
> most uses of this dangerous idiom?
> 
> At the very least, the boolean type could do the equivalent of:
> 
>   def __eq__(self, other):
>      if __debug__ and other not in (0,1,True,False):
>          warnings.warn("Testing of equality is best done by asking
> objects whether they're true, not comparing with a boolean",
> RunTimeWarning)
>      return int.__eq__(self, other)
> 
> or something similar?

I'd rather see a compiler time warning than the runtime warning you 
suggest, because it's likely that some completely innocuous "if x==y" 
code will trigger the runtime warning when one but not both of the 
variables ends up being a bool.  You only want to warn about comparisons 
to the constants True and False, not about comparisons to expressions 
that happen to take those values.

If you're going to consider compiler time warnings, another one should 
be for the syntax "if bool(x):" deprecated in the latest PEP 285 draft.

-- 
David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/



More information about the Python-list mailing list