PEP new assert idiom
vze4rx4y at verizon.net
Sun Nov 7 04:50:43 CET 2004
> This is very similar to what I'm proposing, with the only inconvinience
> that is uglier to type:
> assert \
> exp1 and \
> exp2 and \
> Instead of:
> assert \
> Well, I realize I didn't expressed my thoughts very clearly and that
> it's indeed a very minor change in python's syntax. It won't let anyone
> does anything new, IFAIK, but it makes a common pattern of code a little
> more beautiful, (and why not? more expressive). If one consider the fact
> that it won't break old code (only in one very unlikely case) I don't
> see it as a completely unreasonable suggestion. Other people may think
> differently though.
The odds of Guido accepting this proposal are about zero. As you say, it
doesn't do anything new, but it does require altering the grammar. Besides,
Also, Guido tends to not be persuaded by arguments about "too much typing".
This is doubly true is you're talking about replacing an "and" with a comma (big
Also, one of the existing alternatives is quite readable:
err = 'Errormsg'
assert exp1, err
assert exp2, err
assert exp3, err
The alternative has the advantage that a failure will point to the exact
expression that failed. The disadvantage is the repetition of the error
message; however, I disagree that your use case is common. Instead, it is
likely more advantageous to have different error messages for each expression.
For example, the following comes from the post condition checks in QR matrix
assert Q.tr().mmul(Q)==eye(min(m,n)), "Q is not orthonormal"
assert isinstance(R,UpperTri), "R is not upper triangular"
assert R.size==(m,n), "R is does not match the original dimensions"
assert Q.mmul(R)==self, "Q*R does not reproduce the original matrix"
One could link all of these by an "and" or the proposed comma, but then you
end-up with a single, less informative error message, "The QR decomposition
bombed, but I won't tell you why ;-) ".
More information about the Python-list