How to validate the __init__ parameters

Albert van der Horst albert at spenarnc.xs4all.nl
Mon Jan 4 13:17:04 EST 2010


In article <mailman.2271.1261450134.2873.python-list at python.org>,
Steve Holden  <steve at holdenweb.com> wrote:
<SNIP>
>
>What's the exact reason for requiring that a creator argument be of a
>specific type? So operations on the instances don't go wrong? Well, why
>not just admit that we don't have control over everything, and just *let
>things go wrong* when the wrong type is passed?
>
>What will then happen? Why, an exception will be raised!
>
>You might argue that the user, seeing the traceback from the exception,
>won't know what to make of it. In response to which I would assert (pun
>intended) that neither would the traceback produced by the assertion
>failure.
>
>Given that somebody who knows what they are talking about has to cast
>their eye over a traceback to understand what's wrong with the program,
>it's not much extra effort for that person realise that a method has
>been called with an invalid argument type. I accept that there are
>legitimate arguments in opposition to this point of view, but it has a
>certain engineering efficiency (as long as we are prepared to work
>inside frameworks like Django that will encapsulate any error tracebacks
>so that you see all the information that you need, and the user gets
>some other inexplicable error message like "A programmer probably
>screwed up).

I can agree to a large extent with this reasoning.
However, let us consider an example.
I just solved the euler problem 269, something with cutting a
piece of paper at exact boundaries. At some point I have
reasoned that at least one of 4 arguments must be greater
than one, such that an infinite recursion is prevented.
Now I greatly prefer to have an assert there and then,
instead of the infinite recursion, if my reasoning is wrong.
You know it is not the only point where I did a similar
assumption, and the problem was complicated enough as it is
(for me).

This triggers a question: I can see the traceback, but it
would be much more valuable, if I could see the arguments
passed to the functions. Is there a tool?

>
>In other words, "be prepared to fix your program when it goes wrong" is
>a valid alternative to trying to anticipate every single last thing that
>might go wrong. This philosophy might not be appropriate for
>extra-terrestrial exploration, but most Python programmers aren't doing
>that.

Funny that you make this distinction. There is less of a distinction
than you may think.
Even while in embedded programming we anticipate as much as possible.
there is also the watch dog timer, that resets the whole system for
things unforeseen, hardware failures included. You bet space crafts
have those on board.

>
>regards
> Steve

Groetjes Albert
--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst




More information about the Python-list mailing list