3 ms·
Am I missing something, or is this a fancier string.Template [0]? Don't get me wrong, it looks very useful, especially the literals. [0]: https://docs.python.o
by sgarland 2y ago
Am I missing something, or is this a fancier string.Template [0]? Don't get me wrong, it looks very useful, especially the literals.
[0]: https://docs.python.org/3/library/string.html#template-strings https://docs.python.org/3/library/string.html#template-strin...
- zahlman 2y agostring.Template does fundamentally the same sort of thing as str.format, or str.__mod__ (i.e. the % operator on strings). It was in a sense a prototype for str.format, introduced way back in Python 2.4 (https://peps.python.org/pep-0292/ https://peps.python.org/pep-0292/). It does create an instance of a separate type, but it neither pre-parses the string (the substitution is implemented using regexes that reprocess the original string each time) nor eagerly evaluates the values to substitute. Further, in order to change how the formatting is done, you'd have to subclass (with the new system, you just write a function that accepts the Template as a parameter).
- sgarland 2y agoThank you for the historical context and details!