[Python-ideas] Multi-line strings that respect indentation
Daniel da Silva
ddasilva at umd.edu
Fri Nov 5 03:08:01 CET 2010
Thanks for all of your replies. I did not know about the dedent() function.
I will use it for now on. If anyone else desires to push this, feel free,
but I am satisfied.
Daniel
On Thu, Nov 4, 2010 at 9:23 PM, 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
>
>
>
> --
> Steven
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20101104/f53f7c1f/attachment.html>
More information about the Python-ideas
mailing list