[Python-ideas] Dart like multi line strings identation

Nick Coghlan ncoghlan at gmail.com
Mon Apr 2 10:46:01 EDT 2018


On 2 April 2018 at 23:06, Steven D'Aprano <steve at pearwood.info> wrote:
> On Mon, Apr 02, 2018 at 12:08:47PM +0000, Steve Barnes wrote:
>
>> This would reflect that, typically, a specific developer tends to want
>> either all or no multi-line text strings dedented.
>
> I don't know how you come to that conclusion.
>
> I certainly would not want "All or Nothing" when it comes to dedenting
> triple-quoted strings.

If we did flip the default with a "from __future__ import auto_dedent"
though, there would be an opportunity to consider the available
approaches for *adding* indentation after the fact, such as:

    indented = textwrap.indent(text, " " * 8)

or:

    indent = " " * 8
    indented = "\n".join((indent + line if line else line) for line in
text.splitlines())

Adding indentation is generally easier than removing it, since you can
operate on each line in isolation, rather than having to work out the
common prefix.

To allow exact recreation of the current indented multi-line string
behaviour, we could offer an `__indent__` constant, which the compiler
replaced with the leading indent of the current code block (Note: not
necessarily the indent level of the current line).

So where today we have:

    * leading indent by default
    * "textwrap.dedent(text)" to strip the common leading whitespace

In an auto-dedent world, we'd have:

    * the current block indent level stripped from each line after the
first in multi-line strings by default
    * add it back by doing "textwrap.indent(text, __indent__)" in the
same code block

I mostly find the current behaviour irritating, and work around it by
way of module level constants, but even so, I'm still not sure it
qualifies as being annoying enough to be worth the hassle of changing
it.

One relevant point though is that passing an already dedented string
through textwrap.dedent() will be a no-op, so the compatibility cases
to worry about will be those where *all* of the leading whitespace in
a multiline string is significant, including the common prefix arising
from the code block indentation.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list