Evaluation of variable as f-string

Thomas Passin list1 at tompassin.net
Sun Jan 29 13:52:02 EST 2023


On 1/29/2023 6:09 AM, Christian Gollwitzer wrote:
> Am 28.01.23 um 02:56 schrieb Thomas Passin:
>> On 1/27/2023 5:10 PM, Christian Gollwitzer wrote:
>>> Am 27.01.23 um 21:43 schrieb Johannes Bauer:
>>>> I don't understand why you fully ignore literally the FIRST example 
>>>> I gave in my original post and angrily claim that you solution works 
>>>> when it does not:
>>>>
>>>> x = { "y": "z" }
>>>> s = "-> {x['y']}"
>>>> print(s.format(x = x))
>>>> Traceback (most recent call last):
>>>>    File "<stdin>", line 1, in <module>
>>>> KeyError: "'y'"
>>>>
>>>> This. Does. Not. Work.
>>>
>>> It's because "you're holding it wrong!". Notice the error message; it 
>>> says that the key 'y' does not exist.
>>>
>>>
>>> (base) Apfelkiste:Abschlussmeeting chris$ ipython
>>> Python 3.8.8 (default, Apr 13 2021, 12:59:45)
>>> Type 'copyright', 'credits' or 'license' for more information
>>> IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.
>>>
>>> In [1]: x = { "y": "z" }
>>>
>>> In [2]: s = "-> {x[y]}"
>>>
>>> In [3]: print(s.format(x = x))
>>> -> z
>>>
>>> In [4]:
>>>
>>>      Christian
>>
>> Oops, that's not quite what he wrote.
>>
>> You: s = "-> {x[y]}"    # Works
>> Him: s = "-> {x['y']}"  # Fails
>>
> You might want to reconsider why I could have possibly written this 
> message....

I might .. or I might wish you had actually said what you wanted to 
convey ...



More information about the Python-list mailing list