Adding static typing to Python

Justin Sheehy justin at iago.org
Tue Feb 19 14:45:57 EST 2002


ajeru at vknn.org (Alexander Jerusalem) writes:

> And the final argument I have for static type checking is that it
> enables method dispatching based on parameter types. In a statically
> typed language you can create two methods that have the same name but
> differ on the parameter types. The correct method will be called for
> you depending on the type of the argument you pass in your call. That
> makes for a quite flexible way of extending a program. You can just
> add another method with the same name and another type to handle a
> special case without touching the existing methods.

I use parametric polymorphism quite a bit in languages like Java.

However, Python allows you to do something that is often even better,
which wouldn't work at all with forced static typing: 
signature-based polymorphism.  (I know that there is a better term for
this, but I can't think of it right now.)

Instead of dispatching on an object's type, you dispatch on its capabilities.

Sometimes this dispatch is implicit via a method call.

The encouragement of this pattern is a very strong point for Python, imo.

-Justin

 





More information about the Python-list mailing list