On 11/27/17 9:12 PM, Ivan Pozdeev via Python-ideas wrote:
The `assert' statment was created the same as in previous languages like C/C++: a check to only do in debug mode, when you can't yet trust your code to manage and pass around internal data correctly. Examples are array bounds and object state integrity constraints.
Unlike C, Python does the aforementioned checks all the time, i.e. it's effectively always in "debug mode". Furthermore, validation checks are an extremily common use case.
I have been using assert in my in-house code for input validation for a long time, the only thing preventing me from doing the same in public code is the fact that it only raises AssertionError's while library code is expected to raise TypeError or ValueError on invalid input.
The last drop that incited me to do this proposition was https://stackoverflow.com/questions/2142202/what-are-acceptable-use-cases-fo... where none of the _seven_ answer authors (beside me) managed to make a _single realistic use case_ for `assert' as a debug-only check.
So, I'm hereby proposing to:
- make `assert' stay in optimized mode
- change its syntax to raise other types of exceptions
E.g.: "assert condition, type, value" or "assert condition, type, exception_constructor_argument(s)" to maintain backward compatibility.
You are proposing:
assert condition, type, value
Why not just use Python as it is now, with this:?
if not condition: raise type(value)
I don't see a reason to change the assert statement. You can do what you need without the change.