New submission from Arcadiy Ivanov <[email protected]>:
I'd like to start a discussion on/gauge interest for introducing an enhancement
to PEP-498 in a form of delayed/lazy/lambda f-string.
The proposed change is, conceptually, minor and would not differ from PEP-498
syntactically at all except for string prefix.
E.g.
extra = f'{extra},waiters:{len(self._waiters)}'
becomes
extra = fl'{extra},waiters:{len(self._waiters)}'
The proposal would produce a lambda-like object x('format'), where x.__str__()
== f'format'.
This should come extremely useful in all cases where delayed or conditional
string formatting and concatenation are desired, such as in cases of logging.
As an example, currently, the
logger.debug("Format %s string", value)
cannot be used with an f-string as follows
logger.debug(f"Format {value} string")
without an unconditional evaluation of all parameters due to current
compilation prior to logging checking whether it's even enabled for debug:
>>> b = 1
>>> def a(x):
... return f"Foo {x} bar {b}"
...
>>> dis.dis(a)
2 0 LOAD_CONST 1 ('Foo ')
2 LOAD_FAST 0 (x)
4 FORMAT_VALUE 0
6 LOAD_CONST 2 (' bar ')
8 LOAD_GLOBAL 0 (b)
10 FORMAT_VALUE 0
12 BUILD_STRING 4
14 RETURN_VALUE
Additional great optimizations may be rendered by introducing an a fl-string is
a case where
foo = "foo"
s1 = fl"S1 value ${foo}"
s2 = fl"S2 value of ${s1}"
print(s2)
may produce only one BUILD_STRING instruction, potentially dramatically
increasing performance in heavy string-concat based applications.
Even when a compiler will not be able to statically prove that a particular
value is in fact an fl-string, an interpreter-level check or a JIT-based Python
implementation may be able to optimize such concats ad-nauseam (with trap-based
deopt), allowing to collapse an extremely long chains of formats into a single
BUILD_STRING op without any intermediary string allocation/concatenation (very
useful in cases such as web servers, templating engines etc).
I'll be happy to produce patches against 3.7/3.8 for this if a general concept
is found useful/desirable by the community.
----------
components: Library (Lib)
messages: 312909
nosy: arcivanov
priority: normal
severity: normal
status: open
title: Lazy Literal String Interpolation (PEP-498-based fl-strings)
type: enhancement
versions: Python 3.7, Python 3.8
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue32954>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com