[Python-Dev] Efficient predicates for the standard library

Alex Martelli aleaxit at yahoo.com
Mon Oct 6 03:28:38 EDT 2003


On Sunday 05 October 2003 03:24 am, Raymond Hettinger wrote:
   ...
> In this case, a single example in the docs may suffice:
>
>     if False in imap(isinstance, seqn, repeat(int)):
>         raise TypeError("All arguments must be of type int")

If assert is seen as the typical way to posit such checks, then maybe:

assert False not in imap(isinstance, seq, repeat(int)), "All args must be int"

might be considered to be a didactically preferable example.

Personally, I would not really consider this optimal, with either way of
expression.  Python's error messages are slowly but surely getting better in
that, instead of just saying that something (e.g.) "must be int" (and leaving
the coder in the dark about WHAT it was instead), more and more such
messages are saying "must be int, not str" or the like.  Giving examples
that lead to less-informative error messages is, IMHO, not a good idea; to
give more information in case of errors, of course, does require a bit more
code in the check.  I guess for sanity checks that are meant to never really
trigger an error message, one might be inclined to ignore this issue -- at
least until the first time one such message does trigger and one has to go
back and re-instrument the checks to be more informative;-).  Sorry for the
aside, but I care more about helpful error messages than about "efficient
predicates", where the efficiency gain bids fair to be a micro-optimization...


Alex




More information about the Python-Dev mailing list