Out-denting multiline comments

Bengt Richter bokr at oz.net
Fri Aug 9 17:37:22 EDT 2002


On 9 Aug 2002 00:32:54 -0700, vvainio at tp.spt.fi (Ville Vainio) wrote:

>"Delaney, Timothy" <tdelaney at avaya.com> wrote in message news:<mailman.1028849175.24992.python-list at python.org>...
>
>> > From: Max M [mailto:maxm at mxm.dk]
>> > 
>> > Is there a standard idiom for un-tabbing, de-denting, un-denting (or 
>
>> There's been talk of exposing this functionality outside of this one domain
>> though (since it is useful for things other than docstrings).
>
>Yes, it was discussed on Python-Dev a while back (until Guide told us
>to move the discussion here), and apparently implementing a "dedent"
>method in the upcoming textwrap module got some support. What is not
>clear, however, is who should implement (ie copy-paste from inspect)
>the method. I was told to "go for it", which probably means that it's 
>good idea - but having no write access to python CVS means someone
>else should probably do it.
>
ISTM the real question is if you could get a compile-time way of adjusting
the string that is indented on the source, so as not be indented in its definition.
E.g., you could have an 'm' for margin prefix to the string to cause a dedent
as much as the shortest expanded white space on a non-blank line. E.g.,

s = m"""
   01234
      34567
    123
"""
would give you the same as
s = """
01234
   34567
 123
"""

To combine with raw string, you might concatenate with a prefixed m'' string, e.g.,

s = m''r"""
  \whatever
"""

If it's just going to be a function or method, I think it ought to have a re-indent parameter
with default 0. E.g.,
s = m"""
   01234
      34567
    123
"""
print s.redent(1)
 01234
    34567
  123

I can't think of a decent syntax to do that at compile time, unless there could be
a cteval() (compile-time-eval) function that the compiler would recognize and use
recursively to compile _and_ evaluate an expression to generate the resulting literal
constant (of any type).

Regards,
Bengt Richter



More information about the Python-list mailing list