Alternate numeric proposal, Take 2

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Mon Jul 23 19:18:10 EDT 2001


After thinking this over all day, I have come up with a
(hopefully) better proposal.

A module (only) might be allowed to declare it's preferences
like so:

    from __numerics__ import original

which would yield the semantics that Guido plans for 2.2, or

    from __numerics__ import standard

to get the 2.4+ semantics.  The __numerics__ package is magic
much as the __future__ module is, but it maps to a "real"
package, called simply numerics (without the __ leader/trailer).

The original module would be quite simple:

    # original numerics module

    from __numerics__ import standard
    
    def div(a, b):
        return a // b

standard *is* magic, implemented in the interpreter core (as
math is normally done now) and would be required here to prevent 
calling into another module recursively.

In the standard Python distribution, this would be the only 
module in the numerics package; but additional numeric formats
could be implemented to satisfy those with special needs:

    from __numerics__ import rational
    from __numerics__ import decimal
    from __numerics__ import fixedpoint

The fixedpoint module would probably define a member function
.defaultprecision() to set the default precision of numbers.

NOTE ESPECIALLY that this change would only affect mathematics
involving standard types (int, float, long) plus the *specific*
types added for the special extension modules shown above.  Doing
this:

    from __numerics__ import original

    class MyNumericType:
        ... code here implementing some kind of number ...

    a = 1
    b = MyNumericType(2)

    print a + b

will use the normal object-oriented coercion rules, just as it does
now.

The only headache I see is that mixing types (fixedpoint and rational
for instance) in a single module is not covered... but I can't for
the life of me think how you might want to anyway.








More information about the Python-list mailing list