2000-07-24 15:01:55 -04:00
|
|
|
PEP: 215
|
|
|
|
Title: String Interpolation
|
|
|
|
Version: $Revision$
|
2006-03-23 15:13:19 -05:00
|
|
|
Last-Modified: $Date$
|
2007-06-27 20:11:17 -04:00
|
|
|
Author: ping@zesty.ca (Ka-Ping Yee)
|
2011-03-03 23:58:22 -05:00
|
|
|
Status: Superseded
|
2000-08-23 02:04:33 -04:00
|
|
|
Type: Standards Track
|
2017-01-10 14:30:39 -05:00
|
|
|
Content-Type: text/x-rst
|
2000-08-23 02:04:33 -04:00
|
|
|
Created: 24-Jul-2000
|
2007-06-19 00:20:07 -04:00
|
|
|
Python-Version: 2.1
|
2000-08-23 02:04:33 -04:00
|
|
|
Post-History:
|
2011-03-03 23:58:22 -05:00
|
|
|
Superseded-By: 292
|
2000-08-23 02:04:33 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
|
2001-01-30 12:09:53 -05:00
|
|
|
Abstract
|
2017-01-10 14:30:39 -05:00
|
|
|
========
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
This document proposes a string interpolation feature for Python
|
|
|
|
to allow easier string formatting. The suggested syntax change
|
|
|
|
is the introduction of a '$' prefix that triggers the special
|
|
|
|
interpretation of the '$' character within a string, in a manner
|
|
|
|
reminiscent to the variable interpolation found in Unix shells,
|
|
|
|
awk, Perl, or Tcl.
|
2001-01-30 12:09:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
2017-01-10 14:30:39 -05:00
|
|
|
=========
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
This document is in the public domain.
|
2001-01-30 12:09:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
Specification
|
2017-01-10 14:30:39 -05:00
|
|
|
=============
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
Strings may be preceded with a '$' prefix that comes before the
|
|
|
|
leading single or double quotation mark (or triplet) and before
|
|
|
|
any of the other string prefixes ('r' or 'u'). Such a string is
|
|
|
|
processed for interpolation after the normal interpretation of
|
|
|
|
backslash-escapes in its contents. The processing occurs just
|
|
|
|
before the string is pushed onto the value stack, each time the
|
|
|
|
string is pushed. In short, Python behaves exactly as if '$'
|
|
|
|
were a unary operator applied to the string. The operation
|
|
|
|
performed is as follows:
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
The string is scanned from start to end for the '$' character
|
|
|
|
(``\x24`` in 8-bit strings or ``\u0024`` in Unicode strings). If there
|
|
|
|
are no '$' characters present, the string is returned unchanged.
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
Any '$' found in the string, followed by one of the two kinds of
|
|
|
|
expressions described below, is replaced with the value of the
|
|
|
|
expression as evaluated in the current namespaces. The value is
|
|
|
|
converted with ``str()`` if the containing string is an 8-bit string,
|
|
|
|
or with ``unicode()`` if it is a Unicode string.
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
1. A Python identifier optionally followed by any number of
|
|
|
|
trailers, where a trailer consists of:
|
|
|
|
- a dot and an identifier,
|
|
|
|
- an expression enclosed in square brackets, or
|
|
|
|
- an argument list enclosed in parentheses
|
|
|
|
(This is exactly the pattern expressed in the Python grammar
|
|
|
|
by "``NAME`` trailer*", using the definitions in Grammar/Grammar.)
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
2. Any complete Python expression enclosed in curly braces.
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
Two dollar-signs ("$$") are replaced with a single "$".
|
2001-01-30 12:09:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
Examples
|
2017-01-10 14:30:39 -05:00
|
|
|
========
|
|
|
|
|
|
|
|
Here is an example of an interactive session exhibiting the
|
|
|
|
expected behaviour of this feature::
|
|
|
|
|
|
|
|
>>> a, b = 5, 6
|
|
|
|
>>> print $'a = $a, b = $b'
|
|
|
|
a = 5, b = 6
|
|
|
|
>>> $u'uni${a}ode'
|
|
|
|
u'uni5ode'
|
|
|
|
>>> print $'\$a'
|
|
|
|
5
|
|
|
|
>>> print $r'\$a'
|
|
|
|
\5
|
|
|
|
>>> print $'$$$a.$b'
|
|
|
|
$5.6
|
|
|
|
>>> print $'a + b = ${a + b}'
|
|
|
|
a + b = 11
|
|
|
|
>>> import sys
|
|
|
|
>>> print $'References to $a: $sys.getrefcount(a)'
|
|
|
|
References to 5: 15
|
|
|
|
>>> print $"sys = $sys, sys = $sys.modules['sys']"
|
|
|
|
sys = <module 'sys' (built-in)>, sys = <module 'sys' (built-in)>
|
|
|
|
>>> print $'BDFL = $sys.copyright.split()[4].upper()'
|
|
|
|
BDFL = GUIDO
|
2001-01-30 12:09:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
Discussion
|
2017-01-10 14:30:39 -05:00
|
|
|
==========
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
'$' is chosen as the interpolation character within the
|
|
|
|
string for the sake of familiarity, since it is already used
|
|
|
|
for this purpose in many other languages and contexts.
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
It is then natural to choose '$' as a prefix, since it is a
|
|
|
|
mnemonic for the interpolation character.
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
Trailers are permitted to give this interpolation mechanism
|
|
|
|
even more power than the interpolation available in most other
|
|
|
|
languages, while the expression to be interpolated remains
|
|
|
|
clearly visible and free of curly braces.
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
'$' works like an operator and could be implemented as an
|
|
|
|
operator, but that prevents the compile-time optimization
|
|
|
|
and presents security issues. So, it is only allowed as a
|
|
|
|
string prefix.
|
2001-01-30 12:09:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
Security Issues
|
2017-01-10 14:30:39 -05:00
|
|
|
===============
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
"$" has the power to eval, but only to eval a literal. As
|
|
|
|
described here (a string prefix rather than an operator), it
|
|
|
|
introduces no new security issues since the expressions to be
|
|
|
|
evaluated must be literally present in the code.
|
2001-01-30 12:09:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
Implementation
|
2017-01-10 14:30:39 -05:00
|
|
|
==============
|
|
|
|
|
|
|
|
The ``Itpl`` module at [1]_ provides a
|
|
|
|
prototype of this feature. It uses the tokenize module to find
|
|
|
|
the end of an expression to be interpolated, then calls ``eval()``
|
|
|
|
on the expression each time a value is needed. In the prototype,
|
|
|
|
the expression is parsed and compiled again each time it is
|
|
|
|
evaluated.
|
|
|
|
|
|
|
|
As an optimization, interpolated strings could be compiled
|
|
|
|
directly into the corresponding bytecode; that is::
|
|
|
|
|
|
|
|
$'a = $a, b = $b'
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
could be compiled as though it were the expression::
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
('a = ' + str(a) + ', b = ' + str(b))
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
so that it only needs to be compiled once.
|
2001-01-30 12:09:53 -05:00
|
|
|
|
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
References
|
|
|
|
==========
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
.. [1] http://www.lfw.org/python/Itpl.py
|
2001-01-30 12:09:53 -05:00
|
|
|
|
2000-07-24 15:01:55 -04:00
|
|
|
|
2017-01-10 14:30:39 -05:00
|
|
|
..
|
|
|
|
Local Variables:
|
|
|
|
mode: indented-text
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
End:
|