f-string

Ned Batchelder ned at nedbatchelder.com
Tue Dec 5 19:37:32 EST 2017


On 12/5/17 7:16 PM, Steve D'Aprano wrote:
> compile('f"{spam} {eggs}"', '', 'single')

    $ python3.6
    Python 3.6.3 (default, Oct  4 2017, 06:03:25)
    [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
     >>> compile('f"{spam} {eggs}"', '', 'single')
    <code object <module> at 0x105e79660, file "", line 1>
     >>> co = _
     >>> spam = 17
     >>> eggs = 34
     >>> eval(co)
    '17 34'
     >>> dis.dis(co)
       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


--Ned.



More information about the Python-list mailing list