New submission from Gregory P. Smith <[email protected]>:
A Python pattern in code is to keep everything indented to look pretty while,
yet when the triple quoted multiline string in question needs to not have
leading whitespace, calling textwrap.dedent("""long multiline constant""") is a
common pattern.
rather than doing this computation at runtime, this is something that'd make
sense to do at compilation time. A natural suggestion for this would be a new
letter prefix for multiline string literals that triggers this.
Probably not worth "wasting" a letter on this, so I'll understand if we reject
the idea, but it'd be nice to have rather than importing textwrap and calling
it all over the place just for this purpose.
There are many workarounds but an actual syntax would enable writing code that
looked like this:
```python
class Castle:
def __init__(self, name, lyrics=None):
if not lyrics:
lyrics = df"""\
We're knights of the round table
We dance whene'er we're able
We do routines and scenes
With footwork impeccable.
We dine well here in {name}
We eat ham and jam and spam a lot.
"""
self._name = name
self._lyrics = lyrics
```
Without generating a larger temporary always in memory string literal in the
code object that gets converted at runtime to the desired dedented form via a
textwrap.dedent() call. I chose "d" as the the letter to mean dedent. I don't
have a strong preference if we ever do make this a feature.
----------
components: Interpreter Core
messages: 342373
nosy: gregory.p.smith
priority: low
severity: normal
status: open
title: Compile time textwrap.dedent() equivalent for str or bytes literals
type: enhancement
versions: Python 3.9
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue36906>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com