Adding static typing to Python

Magnus Lyckå magnus at thinkware.se
Thu Feb 21 20:50:15 CET 2002


gbreed at cix.compulink.co.uk wrote:

> It looks like "strong typing" 
> doesn't have a universally agreed definition, so I suppose the best thing 
> is to avoid the term.


But we have two orthogonal dimensions here, and it is
meaningful to explain the differences between C and
Python for instance.

Clearly the definition you showed suffers from trying
to describe two distinct qualities with one name. "C and
C++ are sometimes described as strongly typed, but are
perhaps better described as weakly typed." This certainly
contradict the statement that it's the static quality that
"gives strength".

In C, the type of a variable name is decided at compile time,
and in Python, it's decided at runtime. That is ONE difference,
and dynamic / static typing probably describes that best.

The OTHER aspect of typing is that while C types are static,
they aren't very rigid, or whatever you'd like to call it.
You can add integers to memory pointers, and through casting
you can make the compiler do the most meaningless thing without
as much as a slight complaint. In Python you can't. There are
a few well defined ways of coercing types. Unicode containing
only ASCII can be coerced into strings, since there is no doubt
about interpretations, and numerical types can be coerced in a
sensible way, but in general Python will refuse to do meaningless
operations on incompatible types.

I think it's good to push for the terms "static" and "dynamic"
when that's what we mean. But we have to describe the other aspect
too. It's unfortunate that strong/weak typing has come to be used
for two different things. Just like "hacker". We could of course
invent new terms like robust / brittle typing, but I don't think
that is less difficult than to re-educate people about what strong
and weak typing really is.

But I'm open to suggestions...




More information about the Python-list mailing list