mix statically typed with dynamically typed

Alf P. Steinbach alfps at start.no
Thu Jan 28 22:43:37 CET 2010

* Yingjie Lan:
> 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.
> Any thoughts?

Python already has the /syntax/, e.g.

   >>> def speed( dist: float, time: float ) -> float:
   ...   return dist/time
   >>> print( speed.__annotations__ )
   {'dist': <class 'float'>, 'return': <class 'float'>, 'time': <class 'float'>}
   >>> _

However, this syntax, while exploitable, is by default nothing but an annotation 
device, like doc strings.

I'm not sure I like your idea of introducing static typing to increase speed, 
but it could be done without introducing new syntax simply by defining a special 
meaning to such annotation expressions that are 'type' invocations, say, then like

   def speed( dist: type( float ), time: type( float ) ) -> type( float )

Since there are umpteen projects to increase speed of Python this idea may 
already have been explored...

Cheers & hth.,

- Alf (who has some other ideas)

More information about the Python-list mailing list