Clarity vs. code reuse/generality

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Jul 4 21:27:30 EDT 2009


On Sat, 04 Jul 2009 21:14:45 +0000, kj wrote:

>>Technically these are known as "invariants". An assertion will always be
>>True if the program is bug-free, no matter what the user might throw at
>>it; it's not the same as validation.
> 
> What *user* are you talking about???  I've stated a bazillion times that
> this function is meant to be called only from within this module.

In that case, you would be the user, and like any user, you might 
accidentally call the function with invalid data.

I believe this discussion started because you are presenting this as code 
for novices. In that case, it is absolutely important that you start off 
by teaching them the Right Way to do things. As a general rule, the Right 
Way is to do an explicit test and raise rather than use assert.

In production code, "internal use only" code is a grey area where assert 
is sometimes justified (although I'll point out that in practice, code 
written for internal use only has a habit of being called by others). But 
you're not writing production code, you're writing *teaching code*, where 
even more important than code correctness is user education.



-- 
Steven



More information about the Python-list mailing list