[Python-ideas] explicitation lines in python ?
Daniel DELAY
danieldelay at gmail.com
Fri Jun 25 21:08:31 CEST 2010
If we could explicitate a too complex expression in an indented next
line, I would use this feature very often :
htmltable = ''.join( '<tr>{}</tr>'.format(htmlline) for line in table)
: # main line
htmlline : ''.join( '<td>{}</td>'.format(cell) for cell in
line) # explicitation(s) line(s)
(Sorry if this has already been discussed earlier on this list, I have
not read all the archives)
*******
in details :
List comprehension "<expression> for x in mylist" often greatly
improve readability of python programs, when <expression> is not too complex
When <expression> is too complex (ex: nested lists), this become not
readable, so we have to find another solution :
a) defining a function expression(x), or an iterator function, wich will
only used once in the code
b) or droping this beautiful syntax to replace it the very basic list
construction :
newlist = []
for x in myiterable
newlist.append(<expression>)
I often choose b), but I dislike both solutions :
- in solution a) function def can be far from list comprehension, in
fact instructions to build the new list are split in two different
places in the code.
- solution b) seems a bit better to me, but the fact we build a new list
from myiterable is not visible in a glance, unlike list comprehensions.
Renouncing to list comprehension occurs rather often when I write python
code
I think we could greatly improve readability if we could keep list
comprehension anywhere in all cases, but when necessary explicit a too
complex expression in an indented line :
htmltable = ''.join( '<tr>{}</tr>'.format(htmlline) for line in table)
: # main line
htmlline : ''.join( '<td>{}</td>'.format(cell) for cell in
line) # explicitation(s) line(s)
In the case the main line is the header of a "classical" indented block
(starting with "for", "if", "with"...) , this idented block would simply
follow the explicitation(s) line(s).
The explicitations lines can be surely identified as the lines than
begin with "identifier :" (when we are not in an unclosed dict)
with open('data.txt') as f :
if line in enumerate(mylist) : # main line
mylist : f.read().strip().lower() # explicitation(s) line(s)
print line # "classical" indented block
Another possible use of "explicitations lines" is a coding style wich
would start by "the whole picture" first, and completing with details
after, wich is the usual way we mentally solve problems.
Let's take an example : we want to write a function wich returns a
multiplication table in a simle html document.
When I solve this problem, il think a bit like that :
- I need to return an html page. For that I need a "header" and a
"body". My body will contain an "htmltable", wich be built from a
"table" of numbers etc.
My code could look like that :
def tag(content, *tags): # little convenient function
retval = content
for t in tags:
retval = '<{0}>{1}</{0}>'.format(t, retval)
return retval
def xhtml_mult_table(a, b):
return tag(header + body, 'html') :
header : tag('multiplication table', 'title')
body : tag(htmltable, 'tbody', 'table', 'body') :
htmltable : ''.join(tag(xhtmlline, 'tr') for line in table) :
table : headerline + otherlines :
headerline : [[''] + range(a)]
otherlines : [[y] + [x*y for x in range(a)] for y
in range(b)]
xhtmlline : ''.join(tag(str(cell), 'td') for cell in line)
This example is a "heavy use" of the "explicitation line" feature, to
illustrate how it could work.
I don't mean this should replace the "classical" syntax everywhere
possible, but for me this would be for me a nice way to explicitate
complex expressions from time to time, and the ability to use list
comprehension everywhere I wan't.
Daniel
More information about the Python-ideas
mailing list