How to validate the __init__ parameters

r0g aioe.org at technicalbloke.com
Tue Dec 22 07:33:30 EST 2009


Bruno Desthuilliers wrote:
> Steve Holden a écrit :
> (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?
> 
> validation isn't only about types, but that's not the point...
> 
>> What will then happen? Why, an exception will be raised!
> 
> Not necessarily.




Yes, that's what I'm more concerned about. As Steve says, the easy ones
will cause an exception somewhere anyway but the really tricky bugs
might not trouble the interpreter at all and go on to cause errors and
corruption elsewhere that may not even be noticed immediately.

I'm sad there's no particularly elegant alternative (an 'ensure' keyword
maybe ;) as I do like how they read.

I think I'll carry on using them liberally anyway, armed with the
knowledge I can't rely on them in any code I distribute, and
deliberately raising errors when execution NEEDS to be stopped.

Thanks for the insight all.

Roger.



More information about the Python-list mailing list