Typing system vs. Java

Chris Barker chrishbarker at home.net
Tue Jul 31 13:03:54 EDT 2001


Peter Hansen wrote:
> > I for one would like to see a strongly typed variant
> > of Python: if for nothing else, I'd like to be able to compile my code.

I would like to see OPTIONAL static typing in Python. There are places
where it makes a lot of sense, and places where it doesn't. Personally,
I would probably only use it in small computational functions that I
could efficiently compile.

> More to the point, why would you want to compile your Python code?

> If for performance, the better approach is perhaps to profile your
> code, then rewrite the performance-critical portions as C
> extensions.  Then you have pretty much the best of both worlds.

No you don't. The best of both worlds would be to profile my code, and
then statically type those few performance critical portions. This would
be a whole lot less work than re-writing those parts in C.
 
For performances, sake another nice addition would be the introduction
of the "homogenous" sequence. Python already has some of these: strings,
the library array module, NumPy arrays. They allow efficient computation
for methods and functions that know about them (and these are all
written in C), such as the string methods, the re module, and, of
course, NumPy Ufuncs. Unfortunatly, Python itself has no concept of the
homogenous sequence. If it did then a lot of optimization could be done
inside map calls, or list comprehensions, etc. As an example, look at
the following trivial example:

a = range(100)
b = range(100)
c = [(x**2 + y**2) for x in a for y in b] 

In this case, both a and b are homogenous lists of integers. When the
list comprehension is called, every single time (x**2 + y**2) is
evaluated, the interpreter must check the type of x and y and do the
appropriate operation. If  the interpreter knew that a and b were
homogenous, it would only have to do that check once, which could speed
up things a LOT. Of course you could use NumPy for this example, but
there are others where NumPy's array operations are not enough.

Even without any static type declaration, the homogenous sequence could
be possible. It would be pretty easy for tuples, since they are
immutable. Even for lists, at every append, the type of the appended
object could be checked, and the homogenous flag turned off if the type
doesn't match.

Does anyone else think this could be a good idea???

-Chris

-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list