Python and generic programming

Hans Nowak hans at
Wed Oct 27 00:42:21 CEST 2004

Christophe Cavalaria wrote:
> Oliver Fromme wrote:
>>The appropriate type checking is done at compile time.  That
>>is, if your program calls sort with two lists of different
>>type (e.g. a list of strings and a list of ints), then the
>>compiler will detect that and report an error.
> This must be a bad example because I expect to be able to sort a list of
> integers and a list of strings with the same sort function.

You can, actually:

# let l = ["foo"; "bar"; "baz"; "etc"];;
val l : string list = ["foo"; "bar"; "baz"; "etc"]
# sort l;;
- : string list = ["bar"; "baz"; "etc"; "foo"]

# sort [5; 7; 3; -3; 0; 8; 12];;
- : int list = [-3; 0; 3; 5; 7; 8; 12] I don't really understand what Oliver meant by that statement. 
The sort function will not work on lists that have different types in 
them, but that's a different issue.

Hans Nowak

More information about the Python-list mailing list