[Python-Dev] str.dedent

"Martin v. Löwis" martin at v.loewis.de
Mon Nov 14 23:48:34 CET 2005


Noam Raphael wrote:
> There's no reason why multilined strings that are used only once
> should be defined at the beginning of a program (think about a simple
> CGI script, which prints HTML parts in a function.)

I find that simple CGI scripts are precisely the example *for* putting
multi-line string literals at the beginning of a file. There are
multiple styles for writing such things:
1. Put headers and trailers into separate strings. This tends to become
    tedious to maintain, since you always have to find the matching
    string (e.g. if you add an opening tag in the header, you have
    to put the closing tag in the trailer).

2. Use interpolation (e.g. % substitution), and put the strings into
    the code. This works fine for single line strings. For multi-line
    strings, the HTML code tends to clutter the view of the algorithm,
    whether it is indented or not. Functions should fit on a single
    screen of text, and adding multiline text into functions tends
    to break this requirement.

3. Use interpolation, and put the templates at the beginning. This makes
    the templates easy to inspect, and makes it easy to follow the code
    later in the file. It is the style I use and recommend.

Of course, it may occasionally become necessary to have a few-lines
string literally in a function; in most cases, indenting it along
with the rest of the function is fine, as HTML can stand extra spaces
with no problems.

Regards,
Martin



More information about the Python-Dev mailing list