4 ms·
I don't want to be that guy, but why in the world are f-strings (formatted string literals) called literals? They are clearly dynamically calculated expression
by SeanLuke 4y ago
I don't want to be that guy, but why in the world are f-strings (formatted string literals) called literals? They are clearly dynamically calculated expressions.
- digisign 4y agoThey're a weird combo of compile-time parsing and run-time expressions, with custom opcode. Early on they didn't have expressions but worked like .format(). Then they added expressions and the PEP title needed differentiation from it. Not entirely accurate title is now set in stone. I would have called them "interpolated strings" or even e-string but the f-string moniker had already caught on and there was no stopping it.
- zarzavat 4y agoThere’s two definitions of “literal” in widespread use. The first definition is as you say: an expression that has a constant value. The second definition is: an expression that is the primary syntactic form to construct a type. For example: “array literals” construct arrays, but may contain arbitrary expressions within. The first definition is more common in low-level languages where there is a place in the compiled executable to put constant data. These languages might call the second form an initializer rather than a literal. But in a dynamic language such as Python the distinction is less important.
- SeanLuke 4y agoLisp uses the first definition. And it's as dynamic as they come. Java also uses the first definition. I suspect a more proper description would be: #1 is the correct definition, formally used for 80 years now. #2 is incorrect, and is being abused by people in JS and Python who should know better.