Static typing

Shane Hathaway shane at zope.com
Fri Jul 25 20:41:47 EDT 2003


On Fri, 25 Jul 2003, Bjorn Pettersen wrote:

> However, the only way said programmer can do that is by looking at
> _your_ code, and then changing _your_ code. Wouldn't:
> 
>   def foo(bar, baz):
>       """...
>          Expects:
>             bar: integer in the range (0..65535)
>             baz: string (used as binary data)
>          ...
>       """
> 
> be much more useful for said programmer? Afterall, we're all adults
> here.

It depends on the context and intent.  Type checking is useful in several 
ways, even in Python:

- When you're evolving new code, function signatures change often, and you
can use type checks as a safety belt.  You can save time this way if you
apply it in moderation.  (The problem with statically typed languages is
that they usually mandate this technique to the point that it becomes a
large burden.)

- If you're writing secure code, you can't trust the inputs.  Of course,
in this case you need stronger checks than assert statements.

- If you're translating Python to a lower level language, you can use type 
checks as reliable hints for the translator.

- When you're working with a large codebase, many developers, and many
parts changing simultaneously, type checks can enhance your functional and
system testing strategies.

There have been several articles and interviews lately saying that type
checking is a distraction and that testing is the right way to go.  I
agree that unit tests should come first, but type checks remain a useful
tool that increases productivity when used in moderation.

Shane





More information about the Python-list mailing list