[Python-Dev] str.dedent

Noam Raphael noamraph at gmail.com
Sat Nov 12 23:24:08 CET 2005


On 11/12/05, Raymond Hettinger <raymond.hettinger at verizon.net> wrote:
> > The motivation
> > is to be able to write multilined strings easily without damaging the
> > visual indentation of the source code
>
> That is somewhat misleading.  We already have that ability.  What is
> being proposed is moving existing code to a different namespace.  So the
> motivation is really something like:
>
>    I want to write
>        s = s.dedent()
>    because it is too painful to write
>        s = textwrap.dedent(s)
>
Sorry, I didn't mean to mislead. I wrote "easily" - I guess using the
current textwrap.dedent isn't really hard, but still, writing:

import textwrap
...

    r = some_func(textwrap.dedent('''\
                                  line1
                                  line2'''))

Seems harder to me than simply

    r = some_func('''\
                  line1
                  line2'''.dedent())

This example brings up another reason why "dedent" us a method is a
good idea: It is a common convention to indent things according to the
last opening bracket. "dedent" as a function makes the indentation
grow in at least 7 characters, and in 16 characters if you don't do
"from textwrap import dedent".

Another reason to make it a method is that I think it focuses
attention at the string, which comes first, instead of at the
"textwrap.dedent", which is only there to make the code look nicer.

And, a last reason: making dedent a built-in method makes it a more
"official" way of doing things, and I think that this way of writing a
multilined string inside an indented block is really the best way to
do it.

Noam


More information about the Python-Dev mailing list