4

I'd like to defer the rendering of one so that I could do something like this:

TEMPLATE = '{variable}'

variable = 2
print(f(TEMPLATE))  # 2

variable = 4
print(f(TEMPLATE))  # 4

My use-case is actually somewhat more complex than this, but being able to template out my strings at a high-level and then format them later is useful I know I can TEMPLATE.format({'variable': 2}) but I'm after something a bit more... magic.

How do f-strings actually work? How do they pass the local scope into format()?

Oli
  • 235,628
  • 64
  • 220
  • 299
  • can't you define template as f-string - `TEMPLATE = f"{variable}"` ? – furas Dec 18 '17 at 13:43
  • @furas the `f"{variable}"` would complain about `variable` not being in scope/defined. – wkl Dec 18 '17 at 13:45
  • 3
    From a previous post: https://stackoverflow.com/questions/42497625/can-i-postpone-the-evaluation-of-f-strings - it's currently not possible to lazy eval f-strings due to their implementation. – wkl Dec 18 '17 at 13:47

0 Answers0