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

Michael Selik michael.selik at gmail.com
Wed Aug 10 19:32:15 EDT 2016


On Wed, Aug 10, 2016, 6:44 PM Juan Pablo Romero Méndez <
jpablo.romero at gmail.com> wrote:

> As to why I asked that, there are several reasons: I have a very concrete
> need right now to find pragmatic ways to increase code quality, reduce
> number of defects, etc. in a Python code base. But also I want to
> understand better the mind set and culture of Python's community.
>

That's a much better question. Arguing about type checking is somewhat of a
red herring. It's best not to fight the language, but to use its strengths.

I often find that trying to add type and value checks just makes my code
harder to read and harder to maintain, even if they solve a particular bug
in the moment. A better solution is to refactor in such a way that those
checks are unnecessary.

Are you familiar with context managers and the iterator protocol? If not, I
suggest you start by looking for setup/cleanup code or try/except/finally
blocks that you can refactor into a context manager and by looking for
awkward loops with explicit external state that you can refactor into an
iterator.

If you really must add type- and value-checks, I suggest trying to place
them in a property so that they don't clutter the more interesting code.

>



More information about the Python-list mailing list