PEP new assert idiom
Paul Rubin
http
Sat Nov 6 23:16:16 EST 2004
Some time ago I found myself proposing a new "Validate" statement,
which would work exactly like "assert", except:
1) it would throw a ValidationError instead of AssertionError
if the condition failed
2) it would NOT be turned into a no-op by the optimizing compiler.
The purpose is runtime validation of user data, rather than sanity
checking the program logic. Example: I keep finding myself writing
code like:
x = float(raw_input('Enter a positive number: '))
assert x > 0, 'number wasn't positive'
This is incorrect because the assert statement is not guaranteed to be
executed. I propose to say instead,
validate x > 0, 'number wasn't positive'
which would do the right thing.
This proposal was reasonably well received when I've posted it before,
but I don't remember if there was any conclusion, and there was no
follow-up. Since we're again talking about PEP's regarding the assert
statement, should I try to put one together for this, in my copious
(hah) free time?
More information about the Python-list
mailing list