PEP new assert idiom

Alex Martelli aleaxit at yahoo.com
Sun Nov 7 10:30:34 CET 2004


Fábio Mendes <niels_bohr at uol.com.br> wrote:

> > So follow PEP 8's recommendation instead:
> > 
> > assert (
> >    exp1 and
> >    exp2 and
> >    ...
> >    expn
> >    ), 'Errormsg'
> > 
> I didn't know this would work for 'and/or' operators (even I've read the
> style guide PEP!), sorry. It's not super beautiful, but I could live
> happy with that :)

It works for the parentheses -- what operators are inside the
parentheses, including none at all, doesn't matter.  A logical line
isn't finished until all kinds of parentheses are balanced.


> > I don't see the parentheses (which clarify the syntax issue you
> > mentioned in your first post) and 'and' as any uglier than the commas.
> > And having commas mean line continuation is out of the question.
> 
> I never said that commas should mean line continuation (as a backslash).
> It CAN be used as implicit line continuations in lot cases, (like
> function arguments, for instance. This would be just one more case.

Nope, comma can never be used as implicit line continuation in Python
(that's different from some other languages).  The opened and yet
unclosed parentheses are what cause several physical lines to be part of
the same logical line -- not commas, nor other operator or punctuation.

Consider for example:

print ( 'some text'
  'and some more'
  'and yet more' )

look, ma: no commas (nor other punctuation nor operators, yet three
physical lines merge into just one logical line -- thanks to the
parentheses.  This will print

some textand some moreand yet more

because adjacent string literals merge into one string literal (in
Python just like in C).

If you put commas there WITH the parentheses, you'd be printing out a
tuple.  With the commas and WITHOUT the parentheses, syntax error.

You may be associating "commas continue lines" with "function arguments"
because function definitions and calls DO use parentheses, and separate
arguments with commas.  But it's really not an ideal mental model.


Alex



More information about the Python-list mailing list