Optimize flag question

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sat Feb 25 22:26:02 EST 2006


On Sat, 25 Feb 2006 17:56:42 -0800, bonono wrote:

> 
> Steve Holden wrote:
>> > Some other functions rely on the AssertionError exception to indicate to
>> > the user that something went wrong instead of using a user defined
>> > exception.
>> >
>>
>> The real problem here is that you appear to be using AssertionError in
>> an inappropriate way. If some caller passes an incorrect argument to
>> your function, raise a ValueError. If you are passed the wrong type of
>> data, raise a TypeError. And so on. Or, as you imply you thought about,
>> raise a user-defined error.
>>
>> Generally speaking you should reserve assert for circumstances where you
>> expect some invariant condition to be true. Otherwise use an "if"
>> statement and raise some other exception if the condition is True.
>>
> What would be the occasion that AssertionError be the right exception
> to raise then ?

Surely that would be when an assert statement fails?

I don't think AssertionError should be called by hand. At least, I
wouldn't do so.


-- 
Steven.




More information about the Python-list mailing list