[Python-ideas] Multi-line strings that respect indentation

spir denis.spir at gmail.com
Fri Nov 5 07:42:26 CET 2010


On Fri, 05 Nov 2010 12:23:09 +1100
Steven D'Aprano <steve at pearwood.info> wrote:

> Daniel da Silva wrote:
> > On several occasions I have run into code that will do something like the
> > following with a multiline string:
> [...]
> > To me, this is rather ugly because it messes up the indentation of
> > some_func(). Suppose we could have a multiline string, that when started on
> > a line indented four spaces, ignores the first four spaces on each line of
> > the literal when creating the actual string?
> > 
> > In this example, I will use four quotes to start such a string. 
> 
> Please no. Three quotes is large enough. Also, four quotes currently is 
> legal: it is a triple-quoted string that begins with a quotation mark. 
> You would be changing that behaviour and likely breaking code.
> 
> I don't think we need syntax for this, but if we do, I'd prefer to add a 
> prefix similar to the r"" or u"" syntax. Perhaps w"" to normalise 
> whitespace?
> 
> But as I said, I don't think we need syntax for this. I'd be happy if 
> textwrap.dedent() became a built-in string method.
> 
> def some_func():
>      x, y = process_something()
>      val = """
>      <xml>
>        <myThing>
>          <val>%s</val>
>          <otherVal>%s</otherVal>
>        </myThing>
>      </xml>
>      """.dedent() % (x, y)
>      return val

Yes, that would be a good workaround. (Does dedent() base it's behaviour on first (non-empty) line?)

Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com




More information about the Python-ideas mailing list