How to validate the __init__ parameters
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Tue Dec 22 01:02:01 EST 2009
On Tue, 22 Dec 2009 00:18:21 +0000, r0g wrote:
> Yikes, glad to be set me straight on that one! Thanks :) It's a pity
> though, I really like the way it reads. Is there anything similar with
> ISN'T disabled when optimizations are turned on?
Yes: an explicit test-and-raise.
if not condition:
raise Exception
> I guess I could do the following but it seems a bit ugly in comparison
> :(
>
> if len(foo) <= 5: raise Exception
Write a helper function:
def fail_unless(condition, exception=Exception, msg=""):
if not condition:
raise exception(msg)
fail_unless(len(foo) <= 5, ValueError, 'string too long')
--
Steven
More information about the Python-list
mailing list