8000 Initial pass at documenting the Template class in string.templatelib · python/cpython@36a4a15 · GitHub
[go: up one dir, main page]

Skip to content

Commit 36a4a15

Browse files
committed
Initial pass at documenting the Template class in string.templatelib
1 parent 945af60 commit 36a4a15

File tree

2 files changed

+115
-0
lines changed

Doc/library/string.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,3 +972,9 @@ Helper functions
972972
or ``None``, runs of whitespace characters are replaced by a single space
973973
and leading and trailing whitespace are removed, otherwise *sep* is used to
974974
split and join the words.
975+
976+
977+
978+
.. toctree::
979+
980+
string.templatelib.rst

Doc/library/string.templatelib.rst

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
:mod:`!string.templatelib` --- Templates and Interpolations for t-strings
2+
=========================================================================
3+
4+
.. module:: string.templatelib
5+
:synopsis: Support for t-string literals.
6+
7+
**Source code:** :source:`Lib/string/templatelib.py`
8+
9+
--------------
10+
11+
12+
.. seealso::
13+
14+
:ref:`f-strings` -- Format strings (f-strings)
15+
16+
17+
.. _templatelib-template:
18+
19+
Template
20+
--------
21+
22+
The :class:`Template` class describes the contents of a template string.
23+
24+
The most common way to create a new :class:`Template` instance is to use the t-string literal syntax. This syntax is identical to that of :ref:`f-strings`, except that the string is prefixed with a ``t`` instead of an ``f``. For example, the following code creates a :class:`Template` that can be used to format strings:
25+
26+
>>> name = "World"
27+
>>> greeting = t"Hello {name}!"
28+
>>> print(list(greeting))
29+
['Hello ', Interpolation('World'), '!']
30+
31+
It is also possible to create a :class:`Template` directly, using its constructor. This takes an arbitrary collection of strings and :class:`Interpolation` instances:
32+
33+
>>> from string.templatelib import Template, Interpolation
34+
>>> name = "World"
35+
>>> greeting = Template("Hello, ", Interpolation(name), "!")
36+
>>> print(list(greeting))
37+
['Hello, ', Interpolation('World'), '!']
38+
39+
.. class:: Template(*args)
40+
41+
Create a new :class:`Template` object.
42+
43+
:param args: A mix of strings and :class:`Interpolation` instances in any order.
44+
:type args: str | Interpolation
45+
46+
If two or more consecutive strings are passed, they will be concatenated into a single value in the :attr:`~Template.strings` attribute. For example, the following code creates a :class:`Template` with a single final string:
47+
48+
>>> from string.templatelib import Template
49+
>>> greeting = Template("Hello ", "World", "!")
50+
>>> print(greeting.strings)
51+
('Hello World!',)
52+
53+
If two or more consecutive interpolations are passed, they will be treated as separate interpolations and an empty string will be inserted between them. For example, the following code creates a template with a single value in the :attr:`~Template.strings` attribute:
54+
55+
>>> from string.templatelib import Template, Interpolation
56+
>>> greeting = Template(Interpolation("World"), Interpolation("!"))
57+
>>> print(greeting.strings)
58+
('',)
59+
60+
.. attribute:: strings
61+
62+
A :ref:`tuple <tut-tuples>` of the static strings in the template.
63+
64+
>>> name = "World"
65+
>>> print(t"Hello {name}!".strings)
66+
('Hello ', '!')
67+
68+
Empty strings *are* included in the tuple:
69+
70+
>>> name = "World"
71+
>>> print(t"Hello {name}{name}!".strings)
72+
('Hello ', '', '!')
73+
74+
.. attribute:: interpolations: tuple[Interpolation, ...]
75+
76+
A tuple of the interpolations in the template.
77+
78+
>>> name = "World"
79+
>>> print(t"Hello {name}!".interpolations)
80+
(Interpolation('World'),)
81+
82+
83+
.. attribute:: values: tuple[Any, ...]
84+
85+
A tuple of all interpolated values in the template.
86+
87+
>>> name = "World"
88+
>>> print(t"Hello {name}!".values)
89+
('World',)
90+
91+
.. method:: __iter__() -> typing.Iterator[str | Interpolation]
92+
93+
Iterate over the template, yielding each string and :class:`Interpolation` in order.
94+
95+
>>> name = "World"
96+
>>> print(list(t"Hello {name}!"))
97+
['Hello ', Interpolation('World'), '!']
98+
99+
Empty strings are *not* included in the iteration:
100+
101+
>>> name = "World"
102+
>>> print(list(t"Hello {name}{name}"))
103+
['Hello ', Interpolation('World'), Interpolation('World')]
104+
105+
106+
107+
108+
109+

0 commit comments

Comments
 (0)
0