I love assert
anton.schattenfeld at gmail.com
Wed Nov 12 21:53:30 CET 2014
On Tuesday, November 11, 2014 11:41:06 AM UTC-8, Peter Cacioppi wrote:
> I get the impression that most Pythonistas aren't as habituated with assert statements as I am. Is that just a misimpression on my part? If not, is there a good reason to assert less with Python than other languages?
> As far as I can tell, Python supports assert perfectly well. When run with the optimization flagging, the asserts are truly removed.
> I think one needs to take care with some basic assert coding - it's not a substitute for unit tests, it doesn't absolve you of normal exception responsibilities, and, most of all, it should be used for passive inspection and not action. But given these guidelines, I still find it very useful as "active comments".
I am not sure how and when you use assert, but something that stops me from using assert is that in many cases I would use them to
1) Check type of an incoming argument/returned value
2) Check value of an incoming argument/returned value
But the issues I can see here is that assert throws AssertError, while there is a specialized error for each of the case: 1) TypeError 2) ValueError.
Moreover for the 1) case it makes it impossible to dynamically substitute an object with another object that implements the same interface.
More information about the Python-list