mix statically typed with dynamically typed

Diez B. Roggisch deets at nospam.web.de
Thu Jan 28 16:27:21 EST 2010


Am 28.01.10 22:12, schrieb Yingjie Lan:
> We all know that Python is dynamically typed, and dynamically typed languages are generally slower than statically typed ones. I wonder if it is possible at all for Python to mix statically-typed-ness with dynamically-typed-ness to boost up its speed a little bit, especially when speed is needed. For example, you define a function like this:
>
> def speed(float dist, float time):
>      return dist/time
>
> then the compiler would generate code to first check parameter types (or even do some casts if appropriate, say cast an int into float) in the beginning of this function. and the rest of the function would then be compiled with the assumption that 'dist' and 'time' are of the type float.
>
> Of course, dynamically-typed-ness is still the same as before. Python is well known for providing multiple programming paradigms, I wonder if we could also sneak this in nicely.

There are various attempts to achieve this.

The most generic one, which is most promising in the long run is PyPy, 
the implementation of Python in itself, with the added benefit of making 
code-generators that emit e.g. C based on Python-code.

Then there is Cython, which blends Python with C & integrates very nicely.

Last but not least, for you actual example, psyco is the easiest thing 
to use, it's a JIT aimed to especially optimize numeric operations as 
the one you present.

Diez



More information about the Python-list mailing list