Are the critiques in "All the things I hate about Python" valid?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Feb 17 09:30:24 EST 2018


On Sat, 17 Feb 2018 03:29:49 -0500, Terry Reedy wrote:

> His dreadful strawperson code snippet should not be allowed even in a
> beginning programming class, let alone in professional programs.
> 
> def foo(x):
>      if is_valid(x):
>          return "hello world"
>      else:
>          return bar(x)


Indeed. Well said Terry. The author complains that we don't know what 
function foo() does, as if that's a problem with the Python language.

Of course we can write crap code in any language: the function has a 
ludicrous name, a badly named argument, and no documentation or comments. 
But he pretends that this is a limitation of Python. Not cool.

What's this Pascal function do? (I'm more familiar with Pascal than C, 
but I'm sure some kind person can translate it into the equivalent 
idiomatic C.)

function foo(x: integer): Str256;
  begin
    if is_valid(x):
       foo := 'hello world';
    else:
       foo := bar(x);
  end;

Apart from now knowing that x is an integer, and that it returns a 
string, we're just as much in the dark, despite the static type system. 
What's the meaning of the function? What counts as valid input and output?

No idea.

And the static type checker won't tell you. It won't complain that the 
function is called "foo" or that the argument is "x" or that is_valid 
actually returns the result of a random flip-toss but only on Tuesdays, 
the rest of the time it always returns True.

And ironically, in a *modern* statically typed language, you may not even 
need the type declarations. After all, in a modern type-checker, the 
compiler can infer that since foo returns 'hello world', it must return a 
string; it can probably even infer the type of the input. So in such a 
language, the source code may not even display the input or output types, 
because the compiler can infer what they logically must be.

The only difference between such a modern statically typed language with 
no (or very few) type declarations, and Python, is that Python checks the 
types at run-time and the statically typed language checks them at 
compile-time. That gives a pros and cons:

Pros for static typing:

- catch some errors sooner;

- allow the compiler to make some optimizations resulting in
  faster code;

- a sufficiently sound type system may be able to prove that
  a certain class of bugs (type bugs) cannot occur;

(assuming you trust that the compiler is bug-free).

Cons for static typing:

- programmer may have to include lots of tedious code to 
  placate the compiler;

- programmer may have to fight the compiler to force it to
  allow perfectly safe operations which the type-checker
  disallows;

- type inference is not always fool-proof;

- typically badly suited to interactive or exploratory 
  programming, and less flexible;

- may give the programmer a false sense of security that
  just because the program compiles, it must be bug-free.


There's a reason we often joke about programmers saying "It compiles, 
quick, let's ship it!".



-- 
Steve




More information about the Python-list mailing list