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

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


On Fri, 05 Nov 2010 10:10:14 +0900
"Stephen J. Turnbull" <stephen at xemacs.org> wrote:

> Daniel da Silva writes:
> 
>  > 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?
> 
> We do.
> 
> from textwrap import dedent
> def some_func():
>     x, y = process_something()
> 
>     val = dedent("""\
>     <xml>
>       <myThing>
>         <val>%s</val>
>         <otherVal>%s</otherVal>
>       </myThing>
>     </xml>
>     """) % (x, y)
> 
>     return val
> 
> I don't think the function call is ugly enough to fix with syntax.

It's just reversing the point of view. The right thing would be to have Python code correctly aligned on indentation _by default_. And possibly provide an alternative for cases (which?) where one would want indent not taken into account.

Denis

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

spir.wikidot.com




More information about the Python-ideas mailing list