[Python-Dev] PEP 285: Adding a bool type

Guido van Rossum guido@python.org
Sat, 30 Mar 2002 09:23:49 -0500


> > [David Abrahams]
> > >       6) Should we eventually remove the inheritance relationship
> > >          between Int and Bool?
> > >
> > > I hope so. Bool is-a Int doesn't seem like the right relationship to
> > > me, unless perhaps we make Int is-a Long... naah, not even then.
> >
> > Hm.  In your favorite language bool is one of the integral types.
> > Doesn't that imply pretty much the same thing?
> 
> Well, I try not to play favorites <0.002 wink>, but the implicit
> conversion rules in C++ are admittedly and unfortunately liberal.
> However, bool is detectably NOT derived from int in C++:
> 
>     void f(int const&);
>     f(false); // error!
>     assert(boost::is_base_and_derived<int,bool>::value); // fails!

I don't understand this example, but I though int and bool weren't
considered classes in C++, so I'm not sure this matters.  I know you
don't care, but C99 *does* consider bool an integral type.

Another argument for deriving bool from int: implementation
inheritance.  A bool must behave just like an int, and this is most
easily accomplished this way: the bool type doesn't have
implementations for most operations: it inherits them from the int
type, which find a bool acceptable where an int is required.

> > Anyway, do you have a use case where it matters?
> 
> Given that Bool is immutable, I have no cases other than examples of
> type introspection which can be written to account for the fact that
> Bool is-a Int. However, it does make certain things trickier to get
> right:
> 
> numeric_types = [ Int, Long, Bool, Float, Complex ]
> for t in numeric_types:
>     if isinstance(x, t):
>         # Do something...
> 
> This sort of thing could depend on getting the order of the list right
> (I didn't).

Using isinstance() this way is usually bad.  And who knows that long
doesn't inherit from int?  (At some point in the future it may, or
they may become the same thing -- see PEP 237.

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