[Python-3000] PEP 31XX: A Type Hierarchy for Numbers (and other algebraic entities)

Jeffrey Yasskin jyasskin at gmail.com
Thu Apr 26 08:13:55 CEST 2007


On 4/25/07, Guido van Rossum <guido at python.org> wrote:
> Jeffrey, is there any way you can drop the top of the tree and going
> straight from Number to Complex -> Real -> Rational -> Integer? These
> are the things that everyone with high school math will know.

Backing up a bit, what kinds of types do you want this hierarchy to
support out of the box? Here are all of the concrete numeric types I
can think of. (Adding to this list is where the numpy people would be
really helpful.)

  complex(Real) (that is, pairs of any type of real number, treated as
complex. So complex(float) is another spelling for the built-in
complex, and complex(int) is the gaussian integers.)
  float, Rational, and Decimal,
  int,
  IntsMod(n) (for any n. prime n's are special. n==2^8 is a C unsigned
char. Also written as "Z/nZ")
  Interval(Real) ([3,5] * [1,3] == [1,15])
  Range (I'm not sure how this should work, but the idea is that
range(2,5) <: range(0,10).)

I'm inclined to exclude Interval because it can't be converted into an
int, and Range because operators will return results with types
different from their arguments, so it breaks my static typing and
mathematical prejudices.

Given the rest of the types, I think the following hierarchy is minimal:
class Complex(Abstract)  # complex({int, rational}) + AlgebraicComplex + Real
class AlgebraicComplex(Complex)  # complex({float,Decimal})
class Real(Complex)  # int + FractionalReal
class FractionalReal(Real)  # float, rational, Decimal

We don't want an Integer abstract class because there is only one type
in it, ``int``, so we'd have no guidance on picking the right methods.
Why not include IntsMod(n), you ask? Their arithmetic, particularly
division, is so different from true integers that I think they'd be
likely to violate the Liskov substitution principle. (IntsMod(11)(7) /
IntsMod(11)(3) == IntsMod(11)(6), whee!)

I'm not completely satisfied that there's no way to distinguish the
gaussian integers from Reals with a single check, but since none of
you are probably using them, the simpler hierarchy is probably worth
it. Maybe we should even combine Real with FractionalReal. Are there
any functions that work on floats but not ints or do different things
depending on the type?

The numpy people do need support for the mathematical ABCs, but if
they can be implemented in a separate module that most people don't
need to know about, I think they should be (I'll move the classes I
currently have into algebra.py to serve as a jumping-off point). And I
think that for ABCs to be viable, it must be possible to add
"superclasses" to an existing abstract class from a separate module.
Hopefully Jim's thread will be fruitful.

I haven't updated the PEP to correspond to this yet, in case it's even
worse than what I have. :)

-- 
Namasté,
Jeffrey Yasskin


More information about the Python-3000 mailing list