[Python-ideas] explicitation lines in python ?

Stephen J. Turnbull stephen at xemacs.org
Sun Jun 27 15:31:34 CEST 2010

Daniel DELAY writes:
 > Le 26/06/2010 04:58, Stephen J. Turnbull a écrit :
 > > the more verbose
 > >
 > >      htmltable = ''.join('<tr>{}</tr>'.format(htmlline) for line in table) \
 > >          with htmlline = ''.join('<td>{}</td>'.format(cell) for cell in line)
 > >
 > > looks better.  Note that the "with" clause becomes an optional part of
 > > an assignment statement rather than a suite controlled by the
 > > assignment, and the indentation is decorative rather than syntactic.
 > >    
 > This syntax on one line is interesting if we see "refinement" as a way 
 > to make more readable a too long line.
 > But I'm not sure wether this syntax is compatible with nesting different 
 > levels of refinement in a recursive way, as I did in an example.

I'm not sure of all the corner cases myself, but it seems to me that
the above example could be extended to

    htmltable = ''.join(tr.format(htmlline) for line in table) \
        with tr = '<tr>{}</tr>', \
        htmlline = ''.join(td.format(cell) for cell in line) \
            with td = '<td>{}</td>'

although it's not as prettily formatted as your examples.

 > Using "with" as an optional part of assignment seems to me rather 
 > restrictive, as too complex expressions may appear anywhere involving an 
 > expression, not only where expressions are assigned to a variable with "=".

That was deliberate.  If it's not an assignment, it's easy enough (and
preserves locality) to insert an assignment to a new variable on the
preceding line.

More information about the Python-ideas mailing list