-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Add textwrap.dedent, .indent, as str methods. #62535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
To end proposals for new syntax to do what they do for triple-quoted strings. Nick Coghlan gave reasons as follows: run time cost small, can be optimized away, would be used more than some other string methods. In response, Guido said "That's a compelling argument. Let's do it." |
It turns out there's one slight wrinkle in this grand plan: it won't work for docstrings without some additional tweaking to allow for method calls in the docstring detection. >>> def f():
... """example""".lower()
...
>>> print(f.__doc__)
None
>>> import dis
>>> dis.dis(f)
2 0 LOAD_CONST 1 ('example')
3 LOAD_ATTR 0 (lower)
6 CALL_FUNCTION 0 (0 positional, 0 keyword pair)
9 POP_TOP
10 LOAD_CONST 0 (None)
13 RETURN_VALUE |
I still think the methods are worth adding regardless - I just anticipate a request to allow method calls on docstrings to follow not long after ;) |
str already have too many methods. Who uses str.swapcase() or str.zfill() now? I'm -0.5 for adding any new str methods. |
.dedent() is a no-brainer. JFDI. +1 .indent() I guess makes sense for symmetry, although I've rarely used it. -0 As for docstrings, I can imagine other alternatives, so let's let someone interested in that go through the PEP process. |
IMO it’s fine that docstrings continue to live as pure string literals, and documentation tools continue to follow PEP-257’s advice. |
If this goes ahead, would a bytes.dedent() method be also considered? I recently discovered that textwrap.dedent() does not work on bytes() in Python 3. I would have used it for the contents of an input file in a test case. For the record there’s an older bpo-1237680 on this, rejected in 2005. |
#81087 is a relating issue. |
I implemented compile time cleandoc in #106411. textwrap.dedent() is complex function. I want to make str.dedent() much simpler.
|
I created a poll on discuss.python.org for naming. |
What is the status of this? |
I need to write a PEP for this. But I don't have time for it for now. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: