[Python-ideas] New str whitespace cleaning method?
Ron Adam
ron3200 at gmail.com
Sat May 25 02:04:30 CEST 2013
On 05/24/2013 04:46 PM, Jim Jewett wrote:
> On Fri, May 24, 2013 at 4:06 PM, Wolfgang Maier
> <wolfgang.maier at biologie.uni-freiburg.de> wrote:
>> >Triple-quoted strings could be made a subclass of str,
> Not really, but the new-prefix solution (d""" ... """) is a moral equivalent.
I don't care for dedent, (even with the 'd' prefix syntax.) It only covers
about 20% of the use cases that I care about. Which is why I generally
don't bother using it.
A much more useful alternative is to have a margin() method on strings that
will take a width argument and works independent of the indent level the
expression. It's not a dedent, or indent, or maybe it's both. ;-)
message = """\
A simple paragraph
of several
lines.
""".margin(2)
A simple paragraph
of several
lines.
message = """\
A simple paragraph
of several
lines.
""".margin(16)
A simple paragraph
of several
lines.
A margin method covers 90 percent of times I think about using dedent, and
is geared more towards what I really need in those cases.
A margin of 0, gives us the dedent use.
message = """\
A simple paragraph
of several
lines.
""".margin(0)
A simple paragraph
of several
lines.
And calling a margin() method multiple times gives predictable results.
You could use textwraps TextWrapper class with it's initial_indent() and
subsequent_indent(), along with dedent() to do the same things as above.
But it's a lot of work for simple cases like these.
Cheers,
Ron
More information about the Python-ideas
mailing list