PEP 502 – String Interpolation - Extended Discussion | peps.python.org
PEP 502 – String Interpolation - Extended Discussion PEP 502 – String Interpolation - Extended Discussion Author : Mike G. Miller Status : Rejected Type : Informational Created : 10-Aug-2015 Python-Version : 3.6 Table of Contents Abstract PEP Status Motivation Rationale Goals Limitations Background Printf-style formatting, via operator string.Template Class PEP 215 - String Interpolation str.format() Method PEP 498 – Literal String Formatting PEP 501 – Translation ready string interpolation Implementations in Other Languages Bash Perl Ruby Others Scala ES6 (Javascript) C#, Version 6 Apple’s Swift Additional examples New Syntax New String Prefix Additional Topics Safety Mitigation via Tools Style Guide/Precautions Reference Implementation(s) Backwards Compatibility Postponed Ideas Internationalization Rejected Ideas Restricting Syntax to str.format() Only Additional/Custom String-Prefixes Automated Escaping of Input Variables Environment Access and Command Substitution Acknowledgements References Copyright Abstract PEP 498 : Literal String Interpolation , which proposed “formatted strings” was accepted September 9th, 2015.
Additional background and rationale given during its design phase is detailed below. To recap that PEP, a string prefix was introduced that marks the string as a template to be rendered. These formatted strings may contain one or more expressions built on the existing syntax of str.format() . [10] [11] The formatted string expands at compile-time into a conventional string format operation, with the given expressions from its text extracted and passed instead as positional arguments.
At runtime, the resulting expressions are evaluated to render a string to given specifications: >>> location = 'World' >>> f 'Hello, { location } !' # new prefix: f'' 'Hello, World !' # interpolated result Format-strings may be thought of as merely syntactic sugar to simplify traditional calls to str.format() . PEP Status This PEP was rejected based on its using an opinion-based tone rather than a factual one. This PEP was also deemed not critical as PEP 498 was already written and should be the place to house design decision details.
Motivation Though string formatting and manipulation features are plentiful in Python, one area where it falls short is the lack of a convenient string interpolation syntax. In comparison to other dynamic scripting languages with similar use cases, the amount of code necessary to build similar strings is substantially higher, while at times offering lower readability due to verbosity, dense syntax, or identifier duplication.
These difficulties are described at moderate length in the original post to python-ideas that started the snowball (that became PEP 498 ) rolling. [1] Furthermore, replacement of the print statement with the more consistent print function of Python 3 ( PEP 3105 ) has added one additional minor burden, an additional set of parentheses to type and read. Combined with the verbosity of current string formatting solutions, this puts an otherwise simple language at an unfortunate disadvantage to its peers: echo "Hello, user: $user, id: $id, on host: $hostname" # bash say "Hello, user: $user, id: $id, on host: $hostname" ; # perl puts "Hello, user: # {user} , id: # {id} , on host: # {hostname} \n " # ruby # 80 ch -->| # Python 3, str.format with named parameters print ( 'Hello, user: {user} , id: {id} , on host: {hostname} ' . format ( ** locals ())) # Python 3, worst case print ( 'Hello, user: {user} , id: {id} , on host: {hostname} ' . format ( user = user , id = id , hostname = hostname )) In Python, the formatting and printing of a string with multiple variables in a single line of code of standard width is noticeably harder and more verbose, with indentation exacerbating the issue.
For use cases such as smaller projects, systems programming, shell script replacements, and even one-liners, where message formatting complexity has yet to be encapsulated, this verbosity has likely lead a significant number of developers and administrators to choose other languages over the years. Rationale Goals The design goals of format strings are as follows: Eliminate need to pass variables manually. Eliminate repetition of identifiers and redundant parentheses.
Reduce awkward syntax, punctuation characters, and visual noise. Improve readability and eliminate mismatch errors, by preferring named parameters to positional arguments. Avoid need for locals() and globals() usage, instead parsing the given string for named parameters, then passing them automatically. [2] [3] Limitations In contrast to other languages that take design cues from Unix and its shells, and in common with Javascript, Python specified both single ( ' ) and double ( " ) ASCII quote characters to enclose strings.
It is not reasonable to choose one of them now to enable interpolation, while leaving the other for uninterpolated strings. Other characters, such as the “Backtick” (or grave accent ` ) are also constrained by history as a shortcut for repr() . This leaves a few remaining options for the design of such a feature: An operator, as in printf-style string formatting via % . A class, such as string.Template() . A method or function, such as str.format() .
New syntax, or A new string prefix marker, such as the well-known r'' or u'' . The first three options above are mature. Each has specific use cases and drawbacks, yet also suffer from the verbosity and visual noise mentioned previously. All options are discussed in the next sections. Background Formatted strings build on several existing techniques and proposals and what we’ve collectively learned from them. In keeping with the design goals of readability and error-prevention, the following examples therefore use named, not positional arguments.
Let’s assume we have the following dictionary, and would like to print out its items as an informative string for end users: >>> params = { 'user' : 'nobody' , 'id' : 9 , 'hostname' : 'darkstar' } Printf-style formatting, via operator This venerable technique continues to have its uses, such as with byte-based protocols, simplicity in simple cases, and familiarity to many programmers: >>> 'Hello, user: %(user)s , id: %(id)s , on host: %(hostname)s ' % params 'Hello, user: nobody, id: 9, on host: darkstar' In this form, considering the prerequisite dictionary creation, the technique is verbose, a tad noisy, yet relatively readable.
Additional issues are that an operator can only take one argument besides the original string, meaning multiple parameters must be passed in a tuple or dictionary. Also, it is relatively easy to make an error in the number of arguments passed, the expected type, have a missing key, or forget the trailing type, e.g. ( s or d ). string.Template Class The string.Template class from PEP 292 (Simpler String Substitutions) is a purposely simplified design, using familiar shell interpolation syntax, with safe-substitution feature , that finds its main use cases in shell and internationalization tools: Template ( 'Hello, user: $user, id: $ {id} , on host: $hostname' ) . substitute ( params ) While also verbose, the string itself is readable.
Though functionality is limited, it meets its requirements well. It isn’t powerful enough for many cases, and that helps keep inexperienced users out of trouble, as well as avoiding issues with moderately-trusted input (i18n) from third-parties. It unfortunately takes enough code to discourage its use for ad-hoc string interpolation, unless encapsulated in a convenience library such as flufl.i18n . PEP 215 - String Interpolation PEP 215 was a former proposal of which this one shares a lot in common.
Apparently, the world was not ready for it at the time, but considering recent support in a number of other languages, its day may have come. The large number of dollar sign ( $ ) characters it included may have led it to resemble Python’s arch-nemesis Perl, and likely contrib
The rest of this page is on the live site.