[Python-Dev] str.dedent

M.-A. Lemburg mal at egenix.com
Sun Nov 13 18:43:54 CET 2005

Noam Raphael wrote:
> Following Avi's suggestion, can I raise this thread up again? I think
> that Reinhold's .dedent() method can be a good idea after all.
> The idea is to add a method called "dedent" to strings. It would do
> exactly what the current textwrap.indent function does. 

You are missing a point here: string methods were introduced
to make switching from plain 8-bit strings to Unicode easier.

As such they are only needed in cases where an algorithm
has to work on the resp. internals differently or where direct
access to the internals makes a huge difference in terms
of performance.

In your use case, the algorithm is independent of the data type
interals and can be defined solely by using existing string
method APIs.

> The motivation
> is to be able to write multilined strings easily without damaging the
> visual indentation of the source code, like this:
> def foo():
>     msg = '''\
>              From: %s
>              To: %s\r\n'
>              Subject: Host failure report for %s
>              Date: %s
>              %s
>              '''.dedent() % (fr, ', '.join(to), host, time.ctime(), err)
> Writing multilined strings without spaces in the beginning of lines
> makes functions harder to read, since although the Python parser is
> happy with it, it breaks the visual indentation.

This is really a minor compiler/parser issue and not one which
warrants adding another string method.

Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Nov 13 2005)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
2005-10-17: Released mxODBC.Zope.DA 1.0.9        http://zope.egenix.com/

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::

More information about the Python-Dev mailing list