[Python-ideas] triple-quoted strings and indendation

Bruce Leban bruce at leapyear.org
Fri May 13 19:17:01 CEST 2011


On Fri, May 13, 2011 at 9:14 AM, Stephen J. Turnbull <stephen at xemacs.org>wrote:

> Matthias Lehmann writes:
>  > Well, it's not the fault of textwrap.dedent, that is has no notion of
>  > the indendation-level of its argument. As far as I know, that is
>  > something, only the parser knows (not that I know anything about the
>  > Python parser).
>
> Oh, I thought you were referring to the indentation within the string
> (on the first line), not where the string begins.  Sorry!
>
> But I think there's real trouble here, because there are different
> styles of indentation, as we've seen.  You'd have to enforce one for
> triple-quoted strings, but that's likely to conflict with many
> developers' ideas about the matter.  That's really not something the
> parser should be doing ....


If this feature were to be added, we would surely want to ignore the
indentation on the first line regardless of the previous line since it
shouldn't depend on whether or not I use two or four space indents:

    fun_func(-"""
        multiple
        lines
        """)
#   ^^^^ don't want these spaces in my string

but unless we force people to follow the convention that you must have a
line break after the opening """ we would need to ignore indentation
starting with the second line for people who use this style:

    fun_func(-"""foo
                 bar
                 more""")

Now personally, I'd probably follow that first style but if this were a
language feature I wouldn't think it should only work for one style. Here's
pseudo-code:

if s[0] == '\n':  # style = first case above
    strip first character and strip indentation starting with first line
else if s[0] == ' ':
    strip indentation starting with first line   # style = """\
else:
    strip indentation starting with second line  # style = second case above


--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110513/a32ea2c4/attachment.html>


More information about the Python-ideas mailing list