Is this a good idea or a waste of time?

Antoon Pardon apardon at forel.vub.ac.be
Mon Aug 28 06:30:49 EDT 2006


On 2006-08-25, Simon Forman <rogue_pedro at yahoo.com> wrote:
> 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.

I don't understand this argument. Can't type checking be seen as
enforcing a code invariant?

-- 
Antoon Pardon



More information about the Python-list mailing list