string.templatelib --- 對模板字串字面值的支援

原始碼:Lib/string/templatelib.py


模板字串

在 3.14 版被加入.

Template strings are a mechanism for custom string processing. They have the full flexibility of Python's f-string(f 字串), but return a Template instance that gives access to the static and interpolated (in curly brackets) parts of a string before they are combined.

To write a t-string, use a 't' prefix instead of an 'f', like so:

>>> pi = 3.14
>>> t't-strings are new in Python {pi!s}!'
Template(
   strings=('t-strings are new in Python ', '!'),
   interpolations=(Interpolation(3.14, 'pi', 's', ''),)
)

Types

class string.templatelib.Template

The Template class describes the contents of a template string. It is immutable, meaning that attributes of a template cannot be reassigned.

The most common way to create a Template instance is to use the template string literal syntax. This syntax is identical to that of f-strings, except that it uses a t prefix in place of an f:

>>> cheese = 'Red Leicester'
>>> template = t"We're fresh out of {cheese}, sir."
>>> type(template)
<class 'string.templatelib.Template'>

Templates are stored as sequences of literal strings and dynamic interpolations. A values attribute holds the values of the interpolations:

>>> cheese = 'Camembert'
>>> template = t'Ah! We do have {cheese}.'
>>> template.strings
('Ah! We do have ', '.')
>>> template.interpolations
(Interpolation('Camembert', ...),)
>>> template.values
('Camembert',)

The strings tuple has one more element than interpolations and values; the interpolations “belong” between the strings. This may be easier to understand when tuples are aligned

template.strings:  ('Ah! We do have ',              '.')
template.values:   (                   'Camembert',    )

Attributes

strings: tuple[str, ...]

A tuple of the static strings in the template.

>>> cheese = 'Camembert'
>>> template = t'Ah! We do have {cheese}.'
>>> template.strings
('Ah! We do have ', '.')

Empty strings are included in the tuple:

>>> response = 'We do have '
>>> cheese = 'Camembert'
>>> template = t'Ah! {response}{cheese}.'
>>> template.strings
('Ah! ', '', '.')

The strings tuple is never empty, and always contains one more string than the interpolations and values tuples:

>>> t''.strings
('',)
>>> t''.values
()
>>> t'{'cheese'}'.strings
('', '')
>>> t'{'cheese'}'.values
('cheese',)
interpolations: tuple[Interpolation, ...]

A tuple of the interpolations in the template.

>>> cheese = 'Camembert'
>>> template = t'Ah! We do have {cheese}.'
>>> template.interpolations
(Interpolation('Camembert', 'cheese', None, ''),)

The interpolations tuple may be empty and always contains one fewer values than the strings tuple:

>>> t'Red Leicester'.interpolations
()
values: tuple[object, ...]

A tuple of all interpolated values in the template.

>>> cheese = 'Camembert'
>>> template = t'Ah! We do have {cheese}.'
>>> template.values
('Camembert',)

The values tuple always has the same length as the interpolations tuple. It is always equivalent to tuple(i.value for i in template.interpolations).

方法

__new__(*args: str | Interpolation)

While literal syntax is the most common way to create a Template, it is also possible to create them directly using the constructor:

>>> from string.templatelib import Interpolation, Template
>>> cheese = 'Camembert'
>>> template = Template(
...     'Ah! We do have ', Interpolation(cheese, 'cheese'), '.'
... )
>>> list(template)
['Ah! We do have ', Interpolation('Camembert', 'cheese', None, ''), '.']

If multiple strings are passed consecutively, they will be concatenated into a single value in the strings attribute. For example, the following code creates a Template with a single final string:

>>> from string.templatelib import Template
>>> template = Template('Ah! We do have ', 'Camembert', '.')
>>> template.strings
('Ah! We do have Camembert.',)

If multiple interpolations are passed consecutively, 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 empty placeholders in the strings attribute:

>>> from string.templatelib import Interpolation, Template
>>> template = Template(
...     Interpolation('Camembert', 'cheese'),
...     Interpolation('.', 'punctuation'),
... )
>>> template.strings
('', '', '')
iter(template)

Iterate over the template, yielding each non-empty string and Interpolation in the correct order:

>>> cheese = 'Camembert'
>>> list(t'Ah! We do have {cheese