3 ms·
The thing which annoys me most about f strings is I can't build them at runtime, which means I can't just learn/teach f strings. While I know why building f st
by CJefferson 6y ago
The thing which annoys me most about f strings is I can't build them at runtime, which means I can't just learn/teach f strings.
While I know why building f strings is forbidden (they can run arbitrary code), I'm happy to take the risk.
- wodenokoto 6y agoDoesn't calling .format on a string do the exact same thing as an f-string?
- CJefferson 6y agoNo, because f strings can run arbitrary python code, you can write f"{X} - {y} = {X - y}"
- throwaway894345 6y agoI mean it's Python, so you can do anything: >>> fstring="Hello, {\"world\"}" >>> eval(f"print(f'{fstring}')") Hello, world
- CJefferson 6y agoThat doesn't work (easily) in a function as eval only has the global context, not access to local variables (although you can do some hackery to make it work)
- epage 6y agoDelayed formatting was brought up in the PEP but was rejected. That made me a little sad.