[Python-ideas] Idea for new multi-line triple quote literal

MRAB python at mrabarnett.plus.com
Mon Jul 1 19:18:00 CEST 2013


On 01/07/2013 17:22, Philipp A. wrote:
> 2013/7/1 Guido van Rossum guido at python.org <mailto:guido at python.org>
>
>     That‘s a compelling argument. Let’s do it. (Assuming the definition of
>     exactly how to indent or dedent is not up for discussion — if there
>     are good reasons to disagree with textwrap now's the time to bring it
>     up.)
>
> I don’t know if it’s a good reason, but I’m of the opinion that the
> required backslash at the beginning of to-be-dedented string is strange:
>
> We try to eliminate escaped newlines elsewhere (e.g. bracing import
> statements, conditions and tuple values is preferred to escaping newlines)
>
> I think dedent (or however it’s going to be called) should remove common
> whitespace and, if the first line is completely empty, that first line
> as well.
>
> Also for your consideration would be scala’s way:
>
> """|spam
>    |eggs""".stripMargin() =="spam\neggs"
>
> """#spam
>    #eggs""".stripMargin('#') =="spam\neggs"
>
> i.e. removal of all leading whitespace up to and including a margin
> character/prefix. It could be a kwarg to “dedent”…
>
Or strip the common indent, and the initial empty line if present, and
optionally add an indent:

"""
     spam
     eggs
""".reindent() == "spam\neggs\n"

"""
     spam
     eggs
""".reindent(4) == "    spam\n    eggs\n"



More information about the Python-ideas mailing list