[Python-Dev] Revive the types sig?

Paul Prescod paulp at ActiveState.com
Wed Mar 14 09:55:38 EST 2001


Paul Moore wrote:
> 
>...
> 
> My view on all this is that type annotations are likely to be counterproductive,
> in the face of Python's high degree of polymorphism. For example, what is the
> type of
> 
> def fac(n):
>     if n <= 1: return 1
>     return n * fac(n-1)
> 
> ?
> 
> Obvious answer, integer -> integer. And that's likely what the programmer would
> say.
> 
> But why can't this function be used with *any* type which supports comparison
> with the number 1, multiplication, and subtraction of 1?

Sure it can. But as a programmer, I probably haven't really thought
through the meaning of this code for users who pass in strings or lists
of SocketObjects. Therefore I should not be blamed when someone passes
in a random type and gets strange behavior.

Anyhow, this kind of totally polymorphic code is exceedingly rare. If
you had done division in there, floats would work radically differently
than integers. It isn't safe to say that just because the right
operators are defined, they mean the right thing.

Type annotations of any type are a known trade-off between raw
flexibility, safety and error reporting. C++ and ML are way on one end
of the spectrum. Python is way on the other end. There is a middle
ground.

-- 
Python:
    Programming the way
    Guido
    indented it.
       - (originated with Skip Montanaro?)




More information about the Python-list mailing list