Is this a good idea or a waste of time?

Simon Forman rogue_pedro at yahoo.com
Fri Aug 25 02:05:46 EDT 2006


asincero wrote:
> Would it be considered good form to begin every method or function with
> a bunch of asserts checking to see if the parameters are of the correct
> type (in addition to seeing if they meet other kinds of precondition
> constraints)?  Like:
>
>     def foo(a, b, c, d):
>        assert type(a) == str
>        assert type(b) == str
>        assert type(c) == int
>        assert type(d) == bool
>        # rest of function follows
>
> This is something I miss from working with more stricter languages like
> C++, where the compiler will tell you if a parameter is the wrong type.
>  If anything, I think it goes a long way towards the code being more
> self documenting.  Or is this a waste of time and not really "the
> Python way"?
>
> -- Arcadio

Generally asserts should be used to "enforce" invariants of your code
(as opposed to typechecking), or to check certain things while
debugging.

FWIW, if I saw code that that you presented here I'd assume the
programmer who wrote it was either very new(-bie-ish) or just very very
paranoid, so I guess I could say it's not pythonic...  :-)

BTW, speaking of "strictness",  "more stricter" is invalid English,
just "stricter" is the "correct" form.  ;-)

Peace,
~Simon




More information about the Python-list mailing list