Strong typing implementation for Python

Bartc bc at freeuk.com
Mon Oct 12 20:43:24 EDT 2015


On 13/10/2015 01:07, Steven D'Aprano wrote:
> On Tue, 13 Oct 2015 04:20 am, Marko Rauhamaa wrote:
>
>> As for managing complexity, many people believe static typing is a
>> crucial tool. I disagree. Static typing adds vast amounts of noise to
>> the code.
>
> Only if you are stuck in the 1970s. And even then, it is not always noise,
> type declarations or annotations often make useful documentation.
>
> Consider the following piece of code:
>
> def addone(x):
>      return x + 1
>
>
> The human programmer reading that can trivially infer that x must be a
> number (or at least something which supports numeric addition). So can the
> compiler. In a strongly typed language with no numeric promotion, the
> compiler can infer that x must be an int. In a language with numeric
> promotion, it can infer that x must be an int or float.

That's type inference. And is harder than it looks. In Python, x could 
be almost anything, and still be legal, if X has type T and "+" has been 
defined between T and integer. (It would need to analyse an entire 
program and it still can't always be sure.)

> Where is the "vast amounts of noise" added to the code?

It comes from writing this:

  def addone(int x)int:

And not all types are as compact as 'int' (have a look at some C or C++ 
header files).

-- 
Bartc






More information about the Python-list mailing list