Am 27.01.23 um 23:10 schrieb Christian Gollwitzer:
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.

Ah, that is neat! I didn't know that. Thanks for the info.

In my case, I do also however want to have some functionality that actually does math or even calls functions. That would be possible with templates or f-strings, but not format:


x = { "t": 12345 }
s = "{x['t'] // 60:02d}:{x['t'] % 60:02d}"
print(s.format(x = x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: "'t'"

and

s = "{x[t] // 60:02d}:{x[t] % 60:02d}"
print(s.format(x = x))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Only '.' or '[' may follow ']' in format field specifier

but of course:

print(f"{x['t'] // 60:02d}:{x['t'] % 60:02d}")
205:45

Best,
Johannes
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to