PEP new assert idiom

Fábio Mendes niels_bohr at uol.com.br
Sat Nov 6 20:50:14 EST 2004


> > >>> erromsg = 'Errormsg'
> > >>> assert statement 1, errormsg 
> > >>> assert statement 2, 'errormsg
> > >>> (...)
> > >>> assert statement n, errormsg
> 
> Why isn't this equivalent?  Apart from the fact that you keep, here and
> elsewhere, using 'statement' where you hopefully mean 'expression' --
> you cannot assert a statement, you can only assert an expression.

There is only the conceptually subtle difference that in the 1st you
asserts the whole block of expressions, the second you assert each
expression. They're likely to be the same, probably they will be
equivalent in all cases but I can't backup the truth or falsity of that
statement. The pythonic counterpart involves too much typing and is more
error prone.

> Now, calling expressions expressions, rather than very confusingly
> statements, can you explain how the semantics of this, with foo being
> called, would differ from those of
> 
> assert exp1 and exp2 and exp3 ... and expN, 'errormsg'

This is very similar to what I'm proposing, with the only inconvinience
that is uglier to type:

assert \
  exp1 and \
  exp2 and \
  ...
  expn,
  'ErrorMsg'

Instead of:

assert \
  exp1,
  exp2,
  ...
  expn,
  'Errormsg'

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.

Thanks,
Fabio



More information about the Python-list mailing list