Meta-literals



On Oct 26, 4:54 am, Lennart Benschop <lenna...@xxxxxxxxx> 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

.



Relevant Pages

  • Python on Nintendo DS
    ... I tried porting Python to the Nintendo DS, ... Heres a screenshot of it running in the Dualis emulator. ... As for the installation of tools required to compile .nds files, ...
    (comp.lang.python)
  • Re: Python vs. Lisp -- please explain
    ... Nobody denies that Python can be easily run in interactive mode. ... does Python compile your source code before executing it? ... 25Mb Python interpreter install and hope I won't notice, ...
    (comp.lang.python)
  • Re: Calling on Methods and Properties within Worksheet Modules
    ... The only reason why I can think of doing that, the Object data type is ... VB.NET base code, ... to other limitations and the lack of being able to bind at compile time. ... Declare l_wsh As Object ' note not as worksheet ...
    (microsoft.public.excel.programming)
  • Re: Presumably an import is no faster or slower than opening a file?
    ... minimise its loading effect on the system as it could be called lots ... It needs a configuration file of some sort which I want to keep ... the first import will take care of compiling the python source to byte- ... modification time / compile / save" cycle. ...
    (comp.lang.python)
  • Re: Using Python for programming algorithms
    ... all known Python implementations compile to byte-code. ... Interactive computing in C ...
    (comp.lang.python)

Loading