f-string

Chris Angelico rosuav at gmail.com
Tue Dec 5 18:33:40 EST 2017


On Wed, Dec 6, 2017 at 11:16 AM, Steve D'Aprano
<steve+python at pearwood.info> wrote:
> Anyone got a handy copy of Python 3.6 available to test something for me?
>
> What does compile('f"{spam} {eggs}"', '', 'single') return?
>
> What does eval()'ing the above compiled object do? If necessary, you may have
> to define spam and eggs first.
>
>

In 3.6a4+, which is the only 3.6 I have handy, it returns a code object.

>>> spam = "((spam))"
>>> eggs = "!!eggs!!"
>>> compile('f"{spam} {eggs}"', '', 'single')
<code object <module> at 0x7f0f82bf7db0, file "", line 1>
>>> eval(_)
'((spam)) !!eggs!!'
>>> dis.dis(compile('f"{spam} {eggs}"', '', 'single'))
  1           0 LOAD_NAME                0 (spam)
              2 FORMAT_VALUE             0
              4 LOAD_CONST               0 (' ')
              6 LOAD_NAME                1 (eggs)
              8 FORMAT_VALUE             0
             10 BUILD_STRING             3
             12 PRINT_EXPR
             14 LOAD_CONST               1 (None)
             16 RETURN_VALUE

Same is true in 3.7 alphas.

ChrisA




More information about the Python-list mailing list