Content-Length: 529744 | pFad | http://github.com/python/peps/commit/4eaa0841c1bc5e4006d7d1c62fa4b16047da41b7

08 PEP 822: cleanup and clalification (#4855) · python/peps@4eaa084 · GitHub
Skip to content

Commit 4eaa084

Browse files
authored
PEP 822: cleanup and clalification (#4855)
PEP 822: cleanup
1 parent d1415ef commit 4eaa084

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

peps/pep-0822.rst

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ maintainability.
5959
methods.
6060
* Concatenated single-line string literals are more verbose and harder to
6161
maintain. Writing ``"\n"`` at the end of each line is tedious.
62-
It's easy to miss the semicolons between many string concatenations.
62+
In argument lists or collection literals, separating commas between
63+
multi-line string literals are easy to miss.
6364
* ``textwrap.dedent()`` is implemented in Python so it requires some runtime
6465
overhead. Moreover, it cannot be used to dedent t-strings.
6566

@@ -82,8 +83,8 @@ However, this approach has several drawbacks:
8283
the ``dedent()`` method would need to accept an argument specifying
8384
the amount of indentation to remove.
8485
This would be cumbersome and error-prone for users.
85-
* When continuation lines (lines after line ends with a backslash) are used,
86-
they cannot be dedented.
86+
* When continuation lines (lines following a line that ends with a backslash)
87+
are used, they cannot be dedented.
8788
* f-strings may interpolate expressions as multiline string without indent.
8889
In such case, f-string + ``str.dedent()`` cannot dedent the whole string.
8990
* t-strings do not create ``str`` objects, so they cannot use the
@@ -93,8 +94,8 @@ However, this approach has several drawbacks:
9394
similar but would have different behaviors regarding dedentation.
9495

9596
The ``str.dedent()`` method can still be useful for non-literal strings,
96-
so this PEP does not preclude that idea.
97-
However, for ease of use with multiline string literals, providing dedicated
97+
so this PEP does not reject that idea.
98+
However, for dedenting multiline strings, especially t-strings, using dedicated
9899
syntax is superior.
99100

100101

@@ -103,11 +104,13 @@ Specification
103104

104105
Add a new string literal prefix "d" for dedented multiline strings.
105106
This prefix can be combined with "f", "t", "r", and "b" prefixes.
107+
As with existing string prefixes, uppercase and lowercase forms have the same
108+
meaning, and any order is allowed.
106109

107110
This prefix is only for multiline string literals.
108-
So it can only be used with triple quotes (``"""`` or ``'''``).
111+
It can only be used with triple quotes (``"""`` or ``'''``).
109112

110-
Opening triple quotes needs to be followed by a newline character.
113+
The opening triple quotes must be followed by a newline character.
111114
This newline is not included in the resulting string.
112115
The content of the d-string starts from the next line.
113116

@@ -127,7 +130,7 @@ the string.
127130

128131
* Lines that are longer than or equal in length to the determined indentation
129132
must start with the determined indentation.
130-
Othrerwise, Python raises an ``IndentationError``.
133+
Otherwise, Python raises an ``IndentationError``.
131134
The determined indentation is removed from these lines.
132135
* Lines that are shorter than the determined indentation (including
133136
empty lines) must be a prefix of the determined indentation.
@@ -140,11 +143,13 @@ So you cannot use ``\\t`` in indentations.
140143
And you can use line continuation (backslash at the end of line) and remove
141144
indentation from the continued line.
142145

143-
Examples:
146+
147+
Examples
148+
--------
144149

145150
.. code-block:: python
146151
147-
# d-string must starts with a newline.
152+
# d-string must start with a newline.
148153
s = d"" # SyntaxError: d-string must be triple-quoted
149154
s = d"""""" # SyntaxError: d-string must start with a newline
150155
s = d"""Hello""" # SyntaxError: d-string must start with a newline
@@ -180,7 +185,7 @@ Examples:
180185
...""" # Longest common indentation is '..'.
181186
print(repr(s)) # 'Hello\n\n\nWorld!\n.'
182187
183-
# Closing qutotes can be on the same line as the last content line.
188+
# Closing quotes can be on the same line as the last content line.
184189
# In this case, the string does not end with a newline.
185190
s = d"""
186191
..Hello
@@ -204,15 +209,15 @@ Examples:
204209
# Line continuation with backslash works as usual.
205210
# But you cannot put a backslash right after the opening quotes.
206211
s = d"""
207-
..Hello \
212+
..Hello.\
208213
..World!\
209214
.."""
210-
print(repr(s)) # 'Hello World!'
215+
print(repr(s)) # 'Hello.World!'
211216
212217
s = d"""\
213218
..Hello
214219
..World
215-
..""" # SyntaxError: d-string must starts with a newline.
220+
..""" # SyntaxError: d-string must start with a newline.
216221
217222
# d-string can be combined with r-string, b-string, f-string, and t-string.
218223
s = dr"""
@@ -228,15 +233,15 @@ Examples:
228233
print(repr(s)) # b'Hello\nWorld!\n'
229234
230235
s = df"""
231-
....Hello, {"world".title()}!
236+
....Hello,.{"world".title()}!
232237
...."""
233238
print(repr(s)) # 'Hello,.World!\n'
234239
235240
s = dt"""
236-
....Hello, {"world".title()}!
241+
....Hello,.{"world".title()}!
237242
...."""
238243
print(type(s)) # <class 'string.templatelib.Template'>
239-
print(s.strings) # ('Hello, ', '!\n')
244+
print(s.strings) # ('Hello,.', '!\n')
240245
print(s.values) # ('World',)
241246
242247
@@ -248,7 +253,7 @@ explained as follows:
248253

249254
* ``textwrap.dedent()`` is a regular function, but d-string is part of the
250255
language syntax. d-string has no runtime overhead, and it can remove
251-
indentation from t-strings.
256+
indentation even from t-strings.
252257

253258
* When using ``textwrap.dedent()``, you need to start with ``"""\`` to avoid
254259
including the first newline character, but with d-string, the string content
@@ -312,12 +317,11 @@ explained as follows:
312317
assert s1 == s2
313318
314319
315-
316320
Other Languages having Similar Features
317321
=======================================
318322

319323
Java 15 introduced a feature called `text blocks <https://openjdk.org/jeps/378>`__.
320-
Since Java had not used triple qutes before, they introduced triple quotes for
324+
Since Java had not used triple quotes before, they introduced triple quotes for
321325
multiline string literals with automatic indent removal.
322326

323327
C# 11 also introduced a similar feature called
@@ -337,7 +341,7 @@ that removes indent from lines in heredoc.
337341
Perl has `"Indented Here-documents <https://perldoc.perl.org/perl5260delta#Indented-Here-documents>`__
338342
since Perl 5.26 as well.
339343

340-
Java, Julia, and Ruby uses the least-indented line to determine the amount of
344+
Java, Julia, and Ruby use the least-indented line to determine the amount of
341345
indentation to be removed.
342346
Swift, C#, PHP, and Perl use the indentation of the closing triple quotes or
343347
closing marker.
@@ -408,10 +412,10 @@ Removing newline in the last line
408412
---------------------------------
409413

410414
Another idea considered was to remove the newline character from the last line.
411-
This idea is same to Swift's multiline string literals.
415+
This idea is similar to Swift's multiline string literals.
412416

413-
With this idea, user can write multiline string having indent without trailing
414-
newline like below:
417+
With this idea, users can write multiline strings with indentation without
418+
a trailing newline like below:
415419

416420
.. code-block:: python
417421

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/peps/commit/4eaa0841c1bc5e4006d7d1c62fa4b16047da41b7

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy