Python and generic programming

Josiah Carlson jcarlson at uci.edu
Fri Oct 22 02:33:47 EDT 2004


Roman Suzi <rnd at onego.ru> wrote:

> Most Python programs are already kinda generic, yes. But generic
> programming adds multi-sorted types control and constraints to ensure type
> safety...

"kinda generic"?  That is quite the understatement.

As for "type safety", Python has runtime type checking, and as a
programmer, if you want to have varying dispatch based on type, you are
free to do so:

foo_dispatch = {(int, int):foo_int_int, #some defined function
                ...}

def foo(arg1, arg2):
    foo_dispatch[(type(arg1), type(arg2))](arg1, arg2)


With decorator syntax, the above becomes even easier.


Your arguments claiming Python is not generic, and your suggestions that
Python is not type safe while being generic are unfounded.  One likely
reason why you don't find many examples of what I show above, is because
it is usually not necessary; even for production applications.

 - Josiah




More information about the Python-list mailing list