Adding static typing to Python

Fernando Pérez fperez528 at yahoo.com
Tue Feb 19 05:01:58 EST 2002


Terry Reedy wrote:

> The same compiler will probably
> 
> wrongly reject   2 * 3Liters (= 6 Liters),
> wrongly accept  2Liters * 3Liters (= 6 Liters**2, not 6 Liters),
> wrongly reject   2LitersperHour * 3Hours (= 6 Liters),
> 
> and similarly for division, so such a compiler is worse than useless
> unless you restrict yourself to +- operations.  Do you know of any
> compiler for any general-purpose language that correctly handles
> measurement unit arithmetic out of the box?
> 
> One can write a class (or base class and set of classes) in Python
> that will do so correctly, and I believe there is at least one such
> module available.  Such can probably be done more efficiently that
> before in 2.2 by deriving from float.

Indeed:

http://starship.python.net/crew/hinsen/scientific.html

In particular:

Scientific.Physics.PhysicalQuantities: physical quantities
        with units, including unit conversion and arithmetic operations
        with unit checks.
   
An example:

In [1]: v1 = 3 m/s
In [2]: v2 = 1000 cm/h
In [3]: v1+v2
Out[3]= 3.003 m/s
In [4]: v1+v2**2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

[ snipped traceback]

TypeError: Incompatible units

Beautiful, isn't it?

Cheers,

f.



More information about the Python-list mailing list