[Python-Dev] PEP 498 (interpolated f-string) tweak (original) (raw)
Serhiy Storchaka storchaka at gmail.com
Sat Sep 19 21:22:32 CEST 2015
- Previous message (by thread): [Python-Dev] PEP 498 (interpolated f-string) tweak
- Next message (by thread): [Python-Dev] PEP 498 (interpolated f-string) tweak
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 19.09.15 14:03, Eric V. Smith wrote:
While finishing up the implementation of PEP 498, I realized that the PEP has an error. It says that this code:
f'abc{expr1:spec1}{expr2!r:spec2}def{expr3:!s}ghi' Is equivalent to: 'abc' + expr1.format(spec1) + repr(expr2).format(spec2) + 'def' + str(expr3).format('') + 'ghi' But that's not correct. The right way to call format is: type(expr1).format(expr1, spec1) That is, the lookup of format is done on the type, not the instance. Instead of calling format, I've changed the code generator to call format(expr1, spec1). As an optimization, I might add special opcodes to deal with this and string concatenation, but that's for another day (if ever).
Concatenating many strings is not efficient. More efficient way is to use string formatting. Why not translate f-string to
'abc%s%sdef%sghi' % (format(expr1, spec1),
format(repr(expr2), spec2), expr3)
or even to
'abc{:spec1}{!r:spec2}def{!s}ghi'.format(expr1, expr2, expr3)
?
- Previous message (by thread): [Python-Dev] PEP 498 (interpolated f-string) tweak
- Next message (by thread): [Python-Dev] PEP 498 (interpolated f-string) tweak
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]