<div dir="ltr">Might not be the best example because there exists a comparably concise way without using addition:<div><br><div><div>if all([ST1 < ET1, ST2 < ET2, ET2 <= ST1 or ET1 <= ST2]):</div></div><div>    <time intervals are okay></div><div><br></div><div>but I do see the point</div><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 21, 2015 at 5:10 PM, Rob Cliffe <span dir="ltr"><<a href="mailto:rob.cliffe@btinternet.com" target="_blank">rob.cliffe@btinternet.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div bgcolor="#FFFFFF" text="#000000"><span>
    On 21/01/2015 13:27, Chris Angelico wrote:<br>
    </span><blockquote type="cite"><span>
      <pre>On Wed, Jan 21, 2015 at 10:32 PM, Michael Mitchell
<a href="mailto:epsilonmichael@gmail.com" target="_blank"><epsilonmichael@gmail.com></a> wrote:
</pre>
      <blockquote type="cite">
        <pre>PEP 285 provides some justification for why arithmetic operations should be
valid on bools and why bool should inherit from int (see points (4) and (6)
respectively). Since it's been 12 years (it's possible this has been brought
up again between now and then), I thought it could be worthwhile to take
another look.

I am mostly interested in a resurveying of the questions:
1) Would it still be very inconvenient to implement bools separately from
ints?
2) Do most Python users still agree that arithmetic operations should be
supported on booleans?
</pre>
      </blockquote>
      </span><pre>All of them allow you to write stuff like:

if (((1==1) + (1==0) + (0==0)) == 2)

which will be true (if a little over-parenthesized for some people's
liking).
</pre>
    </blockquote>
    May I offer a more realistic example, adapted from a real-life
    program:<br>
    <br>
    ST1 = '20:00'<br>
    ET1 = '22:00'<br>
    ST2 = '22:00'<br>
    ET2 = '06:00'<br>
    # These variables represent times of day (24-hour clock).<br>
    # We want to check that [ST1, ET1) and [ST2, ET2) are non-empty,
    non-overlapping time periods,<br>
    # (allowing the time to wrap past midnight) as they are in this
    example<br>
    # (the time periods are considered to include the <b>S</b>tart <b>T</b>ime
    but not the <b>E</b>nd <b>T</b>ime).<br>
    # A naive approach might be:<br>
    if (    (ST1 <  ET1 <= ST2 <  ET2)<br>
         or (ET2 <= ST1 <  ET1 <= ST2)<br>
         or (ST2 <  ET2 <= ST1 <  ET1)<br>
         or (ET1 <= ST2 <  ET2 <= ST1) ):<br>
    <br>
                <time intervals are OK><br>
    <br>
    # but this can be coded much more succinctly if at first sight less
    transparently (and sorry, Chris, without redundant parentheses <span><span> :-) </span></span>) as:<br>
    <br>
    if (ST1<ET1) + (ET1 <= ST2) + (ST2<ET2) + (ET2<=ST1) ==
    3:<br>
    <br>
                <time intervals are OK><br>
    <br>
    # Lines 1,2,3,4 in the naive approach correspond exactly<br>
    # to the cases where only the 4th/3rd/2nd/1st term, respectively,<br>
    # is False in the succinct approach.<br>
    <br>
    So arithmetic on booleans may not be "pure" but it can be handy.<br>
  </div>

<br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br></blockquote></div><br></div></div></div>