Writing more efficient code

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Tue Jan 2 21:28:23 EST 2007


Jon Harrop:
> I think this sort of functionality would be a welcome addition to Python.

I don't know.


> Perhaps it can be written in Python?

Pyparsing and beautifulsoup show that practically useful parsing
modules can be done using Python alone too.
Set module of Python 2.3, translated to C in Python 2.4 and improved in
Python 2.5 shows that sometimes Python is fit to create prototypes that
can be debugged, and whose API can be improved, and they can later
translated to a faster language.
But the decimal module shows that sometimes such translation can become
a bit difficult.
The re module shows that external modules too can be good enough
compared of Perl/Ruby built-in regex syntax.
Beside Python, various much faster languages may be used, like D,
Pyrex, C, or your loved OCaml. Or theoretically even ShedSkin. I think
D may be fit, the "Pyd" Python <-> D bridge is quite good, and it's
improving. And D is reaching version 1.0.
OCaml maybe can be used to produce Python compiled modules, so it can
be a possibility too, but then very few people are able to maintain it,
so maybe it's better to use a more common language (even D is more
common, because its syntax is easy to grasp by C++ programmers).


> What is the easiest way to add such functionality to Python?

I think implementation language isn't the main problem. I think the
definition of the purpose and API are more difficult.
Mathematica allows you to define rules that let the system chose the
right function (among some with the same name) according to the input
pattern or the kind of input. Guido has discussed some about something
similar, the multimethods. There are some ways and tricks to add such
capabilities to Python, but I don't think they are fast and reliable
enough for real programs. Maybe Python 3.0 will change this some.
If you want just to create something like a regular engine that works
on lists, that contains matching rules, rewriting rules and calls to
many user-defined functions, then I think you can do it with Python
(with Psyco if you want) in few lines (a really basic RE on lists can
be defined in about 20 lines, maybe a good system may can be built with
2000-10000 lines), but I don't know how much useful it can be, maybe it
can find some purpose (and maybe someone has already written such
module).


> I think that is an excellent idea. Who will pay me? ;-)

I don't know, probably no one. Most people don't even know how to use
such pattern matching programming paradigm. For Python it may become
just an experiment.

Bye,
bearophile




More information about the Python-list mailing list