[Python-Dev] str.dedent

Ian Bicking ianb at colorstudy.com
Sun Nov 13 03:00:42 CET 2005


Noam Raphael wrote:
> 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())

I think a better argument for this is that dedenting a literal string is
more of a syntactic operation than a functional one.  You don't think
"oh, I bet I'll need to do some dedenting on line 200 of this module, I
better import textwrap".  Instead you start writing a long string
literal once you get to line 200.  You can do it a few ways:

  some_func("line1\nline2")
  some_func("line1\n"
            "line2")
  some_func("""\
line1
line2""")
  # If nice whitespace would be pretty but not required:
  some_func("""
            line1
            line2""")

I often do that last one with HTML and SQL.

In practice textwrap.dedent() isn't one of the ways you are likely to
write this statement.  At least I've never done it that way (and I hit
the issue often), and I don't think I've seen code that has used that in
this circumstance.

Additionally I don't think textwrapping has anything particular to do
with dedenting, except perhaps that both functions were required when
that module was added.

I guess I just find the import cruft at the top of my files a little
annoying, and managing them rather tedious, so saying that you should
import textwrap because it makes a statement deep in the file look a
little prettier is unrealistic.  At the same time, the forms that don't
use it are rather ugly or sloppy.


-- 
Ian Bicking  |  ianb at colorstudy.com  |  http://blog.ianbicking.org


More information about the Python-Dev mailing list