On 5/20/2021 5:41 PM, Stephen J. Turnbull wrote:
Matt del Valle writes:
> Fully agreed on the sentiment that we shouldn't treat compile-time
> literals differently from runtime operations.
But as you just pointed out, we do. Literals are evaluated at compile
time, operations at runtime. "This" and f"This" generate very
different code!
This will be unhelpful for this discussion, but to be technically
correct (the best kind of correctness!), those two expressions generate
identical code:
>>> dis.dis("'This'")
1 0 LOAD_CONST 0 ('This')
2 RETURN_VALUE
>>> dis.dis("f'This'")
1 0 LOAD_CONST 0 ('This')
2 RETURN_VALUE
It's only when the f-string has internal expressions that the generated
code is different:
>>> dis.dis("""f'{"Th"}{"is"}'""")
1 0 LOAD_CONST 0 ('Th')
2 FORMAT_VALUE 0
4 LOAD_CONST 1 ('is')
6 FORMAT_VALUE 0
8 BUILD_STRING 2
10 RETURN_VALUE
Eric
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/POJVEOZDGRJJRYU3SFAOHNG7ZPSERGQ4/
Code of Conduct: http://python.org/psf/codeofconduct/