Proposal: Magic Constants

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Wed Aug 27 09:00:52 EDT 2003


A while back (a long while actually) I was a 
participant in a long argument over decimal and 
rational numbers, and their inclusion into the 
core of Python.

Last night in a dream I came up with an 
interesting solution (no, really!) which, in the 
cold light of morning, still seems pretty cool.

The gist is this:  Constants would be allowed to 
be trailed by any single alphabetic character. 
When compiled, a constant with a nonstandard 
letter code would be stored in the compiled code 
in the form of a text literal, appropriately 
marked.

When the code is executed, and the special 
literal is processed, the interpreter would look 
up a conversion function in an internal list.  If 
no conversion is found, a runtime error would be 
raised.

Modules to handle alternate numeric formats, such 
as decimal, fixed-point, rational, etc. would 
include a call to a builtin registration function 
which would allow them to "take over" a specific 
alphabetic specifier.

So, to use a supposed Decimal.py module, one 
would do thusly:

    "decimal arithmetic example"

    import Decimal

    a = 1.05D

and so on.

Limitations:  The literal would be required by 
the compiler to match the format of a real number
(float or integer); the conversion function could
raise its own runtime error if the text string 
were out of its range.  So, to write a rational
number:

    "rational arithmetic example"

    import Rational

    b = 1R/15

Note that the 1R is actually the rational 1/1, 
whereas the 15 is an integer; the division sign 
is not part of the rational here, but actually
indicates division.  The result would be the 
rational 1/15, which is assigned to the variable 
b.

The only other disadvantage I can see is that a 
small class of syntax errors become runtime 
errors.  I'm don't think this is a major thing 
though.

Thoughts and opinions welcome...

Chris Gonnerman -- chris.gonnerman at newcenturycomputers.net
http://newcenturycomputers.net






More information about the Python-list mailing list