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

Markus Unterwaditzer markus at unterwaditzer.net
Mon Jul 1 12:52:32 CEST 2013


I realize how unclear i've been. What i am asking for is to have a way to
handle strings like this:

def foo():
    return """
    look at me
        i am cool
    i can do indentation\n"""

I rather want to write code that looks a lot more like this:

def foo():
    return """
        look at me
            i am cool
        i can do indentation
    """

So the first line would have four spaces at the beginning, the second eight,
the third four again. The "\n" character after "indentation" would be the
last character in the string.

With a new string method i would probably try this:

def foo():
    """
        look at me
            i am cool
        i can do indentation
    """.dedent()

But, by this string method behaving like textwrap.dedent, this would also
remove the first four spaces of each line, which is not the behavior i wanted.

-- Markus


On Mon, Jul 01, 2013 at 08:23:26AM +0200, Markus Unterwaditzer wrote:
> Thanks for pointing it out, i have to admit that i didn't read the documentation carefully, and yes, that indeed makes my example invalid, but then there are still strings that have indentation on all lines, which would be completely trunchated with dedent().
> 
> -- Markus (from phone)
> 
> Daniel Robinson <gottagetmac at gmail.com> wrote:
> >Have you tried using textwrap.dedent with this string? It gives what I
> >think is the expected result:
> >
> >'\nfoo: Bar\nuser:\n    fname: Hans\n    lname: Gans\n'
> >
> >since it only removes common leading whitespace.
> >
> >On Mon, Jul 1, 2013 at 1:50 AM, Markus Unterwaditzer <
> >markus at unterwaditzer.net> wrote:
> >
> >> I think this could cause problems with multi-line strings that
> >contain
> >> additional indentation:
> >>
> >>     def get_yaml():
> >>         x = """
> >>         foo: Bar
> >>         user:
> >>             fname: Hans
> >>             lname: Gans
> >>         """.dedent()
> >>         return x
> >>
> >>
> >> While i don't see many arguments why somebody would want to store
> >> configuration
> >> files inside a string, i am sure many beginners who try to use this
> >method
> >> will
> >> be surprised by its behavior.
> >>
> >> -- Markus
> >>
> >> On Mon, Jul 01, 2013 at 11:47:29AM +1000, Nick Coghlan wrote:
> >> > On 1 July 2013 11:09, Steven D'Aprano <steve at pearwood.info> wrote:
> >> > > but in either case, I think the choice of --- as delimiter is
> >ugly and
> >> > > arbitrary, and very likely is ambiguous (currently, x = ---1 is
> >legal
> >> code).
> >> > > Similar suggestions to this have been made many times before, you
> >> should
> >> > > search the archives:
> >> > >
> >> > > http://mail.python.org/mailman/listinfo/python-ideas
> >> >
> >> > I'm still partial to the idea of offering textwrap.indent() and
> >> > textwrap.dedent() as string methods.
> >> >
> >> > 1. You could add a ".dedent()" at the end of a triple quoted string
> >> > for this kind of problem. For a lot of code, the runtime cost isn't
> >an
> >> > issue.
> >> > 2. A JIT would definitely be able to avoid recalculating the result
> >> every time
> >> > 3. Even CPython may eventually gain constant folding for that kind
> >of
> >> > method applied directly to a string literal
> >> > 4. I dedent and indent long strings more often than I capitalize,
> >> > center, tab expand, or perform various other operations which
> >already
> >> > grace the str type as methods.
> >> >
> >> > Cheers,
> >> > Nick.
> >> >
> >> > --
> >> > Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
> >> > _______________________________________________
> >> > Python-ideas mailing list
> >> > Python-ideas at python.org
> >> > http://mail.python.org/mailman/listinfo/python-ideas
> >> _______________________________________________
> >> Python-ideas mailing list
> >> Python-ideas at python.org
> >> http://mail.python.org/mailman/listinfo/python-ideas
> >>
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas


More information about the Python-ideas mailing list