[Python-Dev] PEP 285, inheritance reversed
Guido van Rossum
guido@python.org
Fri, 15 Mar 2002 17:00:45 -0500
> Being a newbie on this list, I was hoping to keep my mouth shut a
> little longer and just wait to see how your discussions rounded out,
> but since the topic seems to have died, I figure I'll make my say:
>
> Isn't the inheritance between bool and int backwards?
>
> ints can be used as bools
> longs can be used as bools
> floats can be used as bools
> lists can be used as bools
> dicts can be used as bools
> strings can be used as bools
> etc... can be used as bools
>
> bools can't be promoted to dicts
> bools can't be promoted to lists
> bools can be promoted to ints, longs, floats, and strings, (and a few
> others) but only in a limited way.
>
> In some OO fashion, doesn't all of this imply that ints, longs, lists,
> dicts, strings, ... should derive from bool instead of bool deriving
> from int? Then bool implements __int__, __long__, __str__, __repr__,
> or the C extension equivalent to promote where needed?
No, it means that there are two subtly different concepts. One
concept is "being usable as a truth value". This is a property of all
objects, and hence all types/classes indeed inherit this ability from
the ultimate base class, 'object'. (Notwithstanding the fact that
a class can override this property to always raise an exception.)
The other concept is "a normalized truth value". Traditionally, we
have used the integers 0 and 1 for these. This is what the bool type
from PEP 285 wants to supplant. It has to be a subclass of int for
backwards compatibility reasons.
> ps: The intro to the list said I should introduce myself... I'm a
> software engineer living and working in Tucson, Arizona. The
> company I work for has started using Python in a larger and larger
> capacity for digital signal processing and other misc scientific
> computing. I've followed the language for 3-4 of years now.
Cool! Let me know if you have an entry for our collection of Python
users:
http://www.python.org/psa/Users.html
(I know, the page contains many stale entries. I'm hoping eventually
to make a much nicer page with a smaller number of "big names".)
--Guido van Rossum (home page: http://www.python.org/~guido/)