Meta-literals

Carl Banks pavlovevidence at gmail.com
Fri Oct 26 21:55:49 EDT 2007


On Oct 26, 4:54 am, Lennart Benschop <lenna... at xs4all.nl> wrote:
> Python has had the Decimal data type for some time now. The Decimal data
> type is ideal for financial calculations. Using this data type would be
> more intuitive to computer novices than float as its rounding behaviour
> matches more closely what humans expect. More to the point: 0.1 and 0.01
> are exact in Decimal and not exact in float.
>
> Unfortunately it is not very easy to access the Decimal data type. To obtain
> the decimal number 12.34 one has to do something like this:
>
> import decimal
> x=decimal.Decimal("12.34")
>
> Of course we can intruduce a single character function name as an alias for
> the Decimal type constructor, but even then we have to use both parentheses
> and quotes for each and every decimal constant. We cannot make it shorter
> than D("12.34")
>
> With Python 3000 major changes to the language are carried out anyway. My
> proposal would require relatively minor changes.
>
> My proposal:
> - Any decimal constant suffixed with the letter "D" or "d" will be
>   interpreted as a literal of the Decimal type. This also goes for
>   decimal constants with exponential notation.

This brings something to mind.

One thing I've always thought would be cool would be to have
programmable literals.  (Note: Python 3 has a ban on programmable
syntax, and this is clearly falls under that umbrella, although in a
limited form.  So this is not a proposal of any sort, just idle
chatter. :)

The idea is: the compiler would see the literal, evaluate it at
compile time, and marshal the evaluated value into the *.pyc file.
More easily said than done, yes, and I'm aware of the issues in doing
that.  But it'd be pretty useful, I'd think.

Decimal literals has been mentioned.

In fact it could benefit a lot of numeric objects, such as those in
gmpy.

Regular expressions.  A significant speed deficiency of regexps in
Python relative to Perl is that Python has to compile regular
expressions every time the program is invoked.  What if you could
compile the regexp at, you know, compile time, and marshall the
compiled state?  It could speed things up a bit, especially for small,
regexp heavy scripts.

Oh, well, you get the idea.  It's not going to happen, but it'd be
cool while not anything crucial.


Carl Banks




More information about the Python-list mailing list