The only problem I see with explicitly passing in the number of characters to the dedent function is that you couple your code with th source of that code. What happens when you copy+paste that function to a class where the indention level does not match. You then will have to change that number, or else your code will break. Also, if you run your code through pylent or something and it changes your indenting from tabs to spaces.<br><br>On Sunday, June 30, 2013 10:56:26 PM UTC-7, Ron Adam wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">
<br>
<br>On 06/30/2013 08:57 PM, Guido van Rossum wrote:
<br>> On Sun, Jun 30, 2013 at 6:47 PM, Nick Coghlan<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="WwSYxooS4VsJ">ncog...@gmail.com</a>> wrote:
<br>>> >On 1 July 2013 11:09, Steven D'Aprano<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="WwSYxooS4VsJ">st...@pearwood.info</a>> wrote:
<br>>>> >>but in either case, I think the choice of --- as delimiter is ugly and
<br>>>> >>arbitrary, and very likely is ambiguous (currently, x = ---1 is legal code).
<br>>>> >>Similar suggestions to this have been made many times before, you should
<br>>>> >>search the archives:
<br>>>> >>
<br>>>> >><a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/<wbr>mailman/listinfo/python-ideas</a>
<br>>> >
<br>>> >I'm still partial to the idea of offering textwrap.indent() and
<br>>> >textwrap.dedent() as string methods.
<br>>> >
<br>>> >1. You could add a ".dedent()" at the end of a triple quoted string
<br>>> >for this kind of problem. For a lot of code, the runtime cost isn't an
<br>>> >issue.
<br>>> >2. A JIT would definitely be able to avoid recalculating the result every time
<br>>> >3. Even CPython may eventually gain constant folding for that kind of
<br>>> >method applied directly to a string literal
<br>>> >4. I dedent and indent long strings more often than I capitalize,
<br>>> >center, tab expand, or perform various other operations which already
<br>>> >grace the str type as methods.
<br>> That's a compelling argument. Let's do it. (Assuming the definition of
<br>> exactly how to indent or dedent is not up for discussion -- if there
<br>> are good reasons to disagree with textwrap now's the time to bring it
<br>> up.)
<br>
<br>It would be an improvement to have them as methods, but I'd actually like
<br>to have Str.indent(n) method that takes a value for the leading white space.
<br>
<br>The value to this method would always be a positive number, and any common
<br>leading white space would be replaced by the new indent amount.
<br>
<br>S.indent(0) would be the same as S.dedent().
<br>
<br>s = """\
<br>A multi-line string
<br>with 4 leading spaces.
<br>""".indent(4)
<br>
<br>
<br> s = """\
<br> A multi-line string
<br> with 4 leading spaces.
<br> """.indent(4)
<br>
<br>
<br> if cond:
<br> s = """\
<br> Another multi-line string
<br> with 4 leading spaces.
<br> """.indent(4)
<br>
<br>
<br>
<br>The reason I prefer this is ...
<br>
<br>It's more relevant to what I'm going to use the string for and is not just
<br>compensating for the block indention level, which has nothing to do with
<br>how I'm going to use the string.
<br>
<br>It explicitly specifies the amount of leading white space I want in the
<br>resulting string object. If I want a different indent level, I can just
<br>change the value. Or call the indent method again with the new value.
<br>
<br>I don't need to know what the current leading white space is on the string,
<br>just what I want for my output.
<br>
<br>
<br>
<br>Strangely, the online docs for textwrap include an indent function that
<br>works a bit different, but it is no longer present in textwrap. Looks like
<br>an over site to me.
<br>
<br>Cheers,
<br> Ron
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>______________________________<wbr>_________________
<br>Python-ideas mailing list
<br><a href="javascript:" target="_blank" gdf-obfuscated-mailto="WwSYxooS4VsJ">Python...@python.org</a>
<br><a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/<wbr>mailman/listinfo/python-ideas</a>
<br></blockquote>