PEP 285: Adding a bool type

Martin v. Loewis martin at v.loewis.de
Sun Mar 31 05:39:09 EST 2002


Tim Peters <tim.one at comcast.net> writes:

> In context, your original int(b) suggestion was in response to my:
> 
>     people have a legitimate need to make an arbitrary true/false
>     expression produce 0 and 1 too, if only to preserve 0/1-based
>     true/false output.  operator.truth() has always been the best
>     way to do exactly that.  Alternatives like "(boolexpr) + 0" and
>     "(boolexpr) and 1 or 0" and "(0,1)[boolexpr]" reek in comparison.
> 
[...]
> So you may say that "clearly the assumption there is that b is a bool", but
> it's not at all clear that was a *reasonable* assumption in the context in
> which you first said it:  I've been thru the business twice now of
> explaining why int() isn't suitable for the cases I was talking about.
> int(bool) is not a case I was talking about.

Your list of alternatives that reek in comparison was
puzzling. int(bool(obj)) doesn't reek to me: it is obvious what it
does, it does that efficiently, and it requires less typing than
operator.truth(obj).

In that context, I took Guido's response as "why are you bringing up
this silly list of alternatives, yet ignoring the obvious alternative:
int is the right way to get an int from a bool": int(bool(obj)).

Regards,
Martin



More information about the Python-list mailing list