mirror of https://github.com/python/cpython
Documentation for issue 5237, auto-numbered format fields. Contributed by Terry J. Reedy.
This commit is contained in:
parent
e56101136c
commit
4c074382bd
|
@ -156,6 +156,7 @@ docs@python.org), and we'll be glad to correct the problem.
|
||||||
* Paul Prescod
|
* Paul Prescod
|
||||||
* Eric S. Raymond
|
* Eric S. Raymond
|
||||||
* Edward K. Ream
|
* Edward K. Ream
|
||||||
|
* Terry J. Reedy
|
||||||
* Sean Reifschneider
|
* Sean Reifschneider
|
||||||
* Bernhard Reiter
|
* Bernhard Reiter
|
||||||
* Armin Rigo
|
* Armin Rigo
|
||||||
|
|
|
@ -221,21 +221,26 @@ The grammar for a replacement field is as follows:
|
||||||
|
|
||||||
.. productionlist:: sf
|
.. productionlist:: sf
|
||||||
replacement_field: "{" `field_name` ["!" `conversion`] [":" `format_spec`] "}"
|
replacement_field: "{" `field_name` ["!" `conversion`] [":" `format_spec`] "}"
|
||||||
field_name: (`identifier` | `integer`) ("." `attribute_name` | "[" `element_index` "]")*
|
field_name: arg_name ("." `attribute_name` | "[" `element_index` "]")*
|
||||||
|
arg_name: (`identifier` | `integer`)?
|
||||||
attribute_name: `identifier`
|
attribute_name: `identifier`
|
||||||
element_index: `integer`
|
element_index: `integer`
|
||||||
conversion: "r" | "s"
|
conversion: "r" | "s"
|
||||||
format_spec: <described in the next section>
|
format_spec: <described in the next section>
|
||||||
|
|
||||||
In less formal terms, the replacement field starts with a *field_name*, which
|
In less formal terms, the replacement field starts with a *field_name* that specifies
|
||||||
can either be a number (for a positional argument), or an identifier (for
|
the object whose value is to be formatted and inserted
|
||||||
keyword arguments). Following this is an optional *conversion* field, which is
|
into the output instead of the replacement field.
|
||||||
|
The *field_name* is optionally followed by a *conversion* field, which is
|
||||||
preceded by an exclamation point ``'!'``, and a *format_spec*, which is preceded
|
preceded by an exclamation point ``'!'``, and a *format_spec*, which is preceded
|
||||||
by a colon ``':'``.
|
by a colon ``':'``. These specify a non-default format for the replacement value.
|
||||||
|
|
||||||
The *field_name* itself begins with either a number or a keyword. If it's a
|
The *field_name* itself begins with an *arg_name* that is either either a number or a
|
||||||
number, it refers to a positional argument, and if it's a keyword it refers to a
|
keyword. If it's a number, it refers to a positional argument, and if it's a keyword,
|
||||||
named keyword argument. This can be followed by any number of index or
|
it refers to a named keyword argument. If the numerical arg_names in a format string
|
||||||
|
are 0, 1, 2, ... in sequence, they can all be omitted (not just some)
|
||||||
|
and the numbers 0, 1, 2, ... will be automatically inserted in that order.
|
||||||
|
The *arg_name* can be followed by any number of index or
|
||||||
attribute expressions. An expression of the form ``'.name'`` selects the named
|
attribute expressions. An expression of the form ``'.name'`` selects the named
|
||||||
attribute using :func:`getattr`, while an expression of the form ``'[index]'``
|
attribute using :func:`getattr`, while an expression of the form ``'[index]'``
|
||||||
does an index lookup using :func:`__getitem__`.
|
does an index lookup using :func:`__getitem__`.
|
||||||
|
@ -244,6 +249,7 @@ Some simple format string examples::
|
||||||
|
|
||||||
"First, thou shalt count to {0}" # References first positional argument
|
"First, thou shalt count to {0}" # References first positional argument
|
||||||
"Bring me a {}" # Implicitly references the first positional argument
|
"Bring me a {}" # Implicitly references the first positional argument
|
||||||
|
"From {} to {}" # Same as "From {0] to {1}"
|
||||||
"My quest is {name}" # References keyword argument 'name'
|
"My quest is {name}" # References keyword argument 'name'
|
||||||
"Weight in tons {0.weight}" # 'weight' attribute of first positional arg
|
"Weight in tons {0.weight}" # 'weight' attribute of first positional arg
|
||||||
"Units destroyed: {players[0]}" # First element of keyword argument 'players'.
|
"Units destroyed: {players[0]}" # First element of keyword argument 'players'.
|
||||||
|
|
Loading…
Reference in New Issue