Reputation: 2510
I'd like to build a tool where fstring formats are stored in a configuration file.
config = load_config()
def build_fstring(str):
return ... # <-- issue is there
chosen_format = config.get("chosen_format") # returns '{k},{v}'
fstring = build_fstring(chosen_format) # may return something like 'f"{k},{v}"'
for (k,v) in d.items():
print(fstring) # fstring is evaluated here
My issue is that fstring is compiled before variables are known.
Is there a way to do it ?
Upvotes: 3
Views: 1083
Reputation: 107124
According to PEP-498, f-strings are meant to "provide a way to embed expressions inside string literals", which means that f-strings are first and foremost string literals, and that trying to evaluate the value of a variable as an f-string defeats its very purpose.
For your purpose of using a variable as a string formatting template, it would be easier to use the str.format
method instead:
k, v = 1, 2
chosen_format = '{k},{v}'
print(chosen_format.format(**locals()))
This outputs:
1,2
Upvotes: 6
Reputation: 2510
I found a trick using eval
:
config = load_config()
chosen_format = config.get("chosen_format") # returns '{k},{v}'
for (k,v) in d.items():
print(eval('f"'+chosen_format+'"'))
But I not very satisfied by the cleanness of the code - print should not know how to build the content - and the performance of evaluating code for each record.
Upvotes: 0