f-string

MRAB python at mrabarnett.plus.com
Tue Dec 5 19:41:46 EST 2017


On 2017-12-06 00:16, Steve D'Aprano 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.
> 
> 
> Thanks in advance.
> 
> 
Microsoft Windows [Version 10.0.15063]
(c) 2017 Microsoft Corporation. All rights reserved.

C:\Users\MRAB>py -3.6
Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit 
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> spam = 'SPAM'
 >>> eggs = 'EGGS'
 >>> c = compile('f"{spam} {eggs}"', '', 'single')
 >>> c
<code object <module> at 0x00000269E2020C00, file "", line 1>
 >>> eval(c)
'SPAM EGGS'
 >>>



More information about the Python-list mailing list