What's the best way to minimize the need of run time checks?

Michael Selik michael.selik at gmail.com
Tue Aug 9 14:51:38 EDT 2016


On Tue, Aug 9, 2016 at 2:31 PM Juan Pablo Romero Méndez <
jpablo.romero at gmail.com> wrote:

> Hello,
>
> In online forums sometimes people complain that they end up having to test
> constantly for None, or that a function's argument has a specific type /
> shape (which immediately brings the following aphorism to mind: "Any
> sufficiently large test suite contains an ad hoc, bug-ridden, slow
> implementation of half of Haskell's type system").
>
> Some responses I've seen to that particular complaint is that they are
> doing it wrong. That they are trying to force a type system into a dynamic
> language (Clojure, Ruby, Python, Javascript, etc).
>
> Do you guys have any resources you like that addresses the following issue:
> What is the best way to use the dynamic features of Python to avoid having
> to write a poor's man type system?
>

It'll be easiest to discuss a specific example. Can you share a chunk of
code which frustrates you?

In general, Python's TypeError and AttributeError serve much the same
purpose as other languages' type systems. Where other languages might force
you to create named, formally specified types, Python allows informally
specified interfaces.

"File-like" is a good example. Rather than go through the frustration of a
formal definition for what is file-like, stay productive and flexible with
duck typing. Objects that don't have the appropriate methods or attributes
will cause a TypeError or AttributeError. If you're passing in the wrong
kind of object, you'll find out almost as soon as you write the code
(because you test early and often), just as you would with a statically
analyzed type system.



More information about the Python-list mailing list