PEP new assert idiom
Fábio Mendes
niels_bohr at uol.com.br
Sun Nov 7 00:58:36 EST 2004
> 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
> decomposition:
>
> 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 ;-) ".
>
You have a good point here. Maybe what I proposed is only a distraction.
We don't want a syntax tailored to every sittuation as it will serve
only for showcase selected programs that express the full elegance of
the language, but to the programmer, it gains the burden of memorizing
the 100 and so operators that makes the language very expressive. Of
course each person will develop a personal dialect, which will make his
code very expressive in a specific domain, but he'll talk to
nobodyelse... For that we have perl. So I guess i'm off this thread, but
someone pointed a PEP for a 'validate' statement which still holds. At
least it will help to elucidate the somewhat confusing meaning of the
assert statement. (you shouldn't use it at runtime?? which level of
optimizations disable it?).
Fabio
More information about the Python-list
mailing list