Automatic reformatting of triple-quoted strings (was Re: Few things)

Carlos Ribeiro carribeiro at gmail.com
Fri Nov 26 18:37:33 EST 2004


On 25 Nov 2004 16:41:00 -0800, bearophile <bearophilehugs at lycos.com> wrote:
> I think a better syntax for such multiline strings can be something
> like: remove from all the beginnings of the lines successive to the
> first one a number of spaces equal to the position of ''' in the
> soucecode.

I was thinking exactly about this earlier today. There is a utility
function described somewhere in the docutils documentation that does
that. I've borrowed that code and called it "stripindent". It handles
all stuff that you mentioned and also tabs & space conversion. I
already call it almost everywhere I use the triple-quote strings.

The end result is that my code is full of constructs of the form:

    sqlquery = stripindent("""
        select column1, column2
        from sometable
        where column1 > blahblahblah
        """)

And I thought, "wouldn't it be nice if Python automatically
reformatted such strings"? Of course, this is not a change to be taken
lightly. Some pros and cons:

0) it automatically supports what is already done by tools such as
pydoc, coctools, doctest, and every Python-enabled IDE that gets
information from docstrings.

1) the source code reads much better; the intention of the writer in
the case above is clearly *not* to have all those extra spaces
clutering the string contents.

2) It encourages use of triple-quoted strings in real code (by making
it more practical) and avoids idioms such as:

s = stripindent("""...
    """)
s = "abcdef..." +
    "rstuvwxyz..."
s = "abcdef..." \
    "rstuvwxyz..."

3) it uses indentation to change the string parsing behavior.
Indentation already has meaning in Python, but not in this situation.

4) It's a change, and people are usually afraid of changes, specially
in this case where it may look like there are so little to gain from
it.

5) it may break old code that uses triple-quoted strings, and that may
require the extra spaces at the beginning of each line.

6) it may lead to surprised in some cases (specially for Python old-timers).


At this point, this is not still a serious proposal, but more like a
"Call for Comments". I have another bunch of ideas being worked out
for possibly future PEPs ("iunpack" & named tuples), so why not give
this one a try?

The idea is as follows:

1) triple-quote strings will automatically be reformatted to remove
any extra space on the left side due to indentation. The indentation
will be controled by the position of the leftmost non-space character
in the string.

2) raw triple-quoted strings will *NOT* be reformatted. Any space to
the left side is deemed to be significant.

This is indeed a quite simple idea, with the potential to simplify
some code. It will also encourage people to write triple-quoted
strings for long strings, which is something that people usually do to
avoid the extra space.


-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list