[Python-Dev] bool conversion wart?

Guido van Rossum guido at python.org
Sun Feb 25 06:24:32 CET 2007


On 2/24/07, Georg Brandl <g.brandl at gmx.net> wrote:
> Guido van Rossum schrieb:
> > Can anyone who is in favor of changing this please come up with a spec
> > for the new bool() signature? What would you do for the most common
> > use case of bool(), which is converting an arbitrary value to its
> > Boolean equivalent without using an if test or the "not not x" hack?
>
> I think this subthread doesn't propose to change bool(), but only to
> stop inheriting bool from int, while giving bools an __index__ method
> that returns 0 or 1 to still allow them being used as list indices.
>
> The obvious consequence would be that arithmetic wouldn't work any
> longer with bools involved.
>
> Also, mapping access or set membership wouldn't work like now,
>
>  >>> d = {1: 2}
>  >>> d[True]
> 2
>  >>> True in set([1])
> True
>
> but that would only be logical.

How would this change be helpful? I'm utterly mystified by these
suggestions that bool would be more useful if it didn't behave like an
int in arithmetic.

On 2/24/07, skip at pobox.com <skip at pobox.com> wrote:
>
>     Neal> Except, all the numeric types do, including int, float, and
>     Neal> complex.  But not bool.
>
> The fact that bool is a subclass of int is more historic than necessary.  If
> not for Python's long usage of 0 and 1 to be the canonical False and True, I
> suspect that bool might have been implemented as a new standalone type.

Not necessarily. I really like the idea that bool is embedded in int,
just like int is embedded in float (real), and real is embedded in
complex.

The construction signature is a different issue -- some people seem to
forget that constructor signatures don't need to match their base
class; it's not required by Liskov substitutability.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list