We have:
what = "answer"
value = 42
f"The {what} is {value}."
==> 'The answer is 42.'
And we have:
values = { "what": "answer", "value": 42 }
"The {what} is {value}".format(values)
==> 'The answer is 42.'
We also have:
what = "answer"
value = 42
t"The {what} is {value}."
==> Template(strings=('The ', ' is ', '.'), interpolations=(Interpolation('answer', 'what', None, ''), Interpolation(42, 'value', None, '')))
But I have not been able to find any way to do something like:
values = { "what": "answer", "value": 42 }
"The {what} is {value}".template(values)
==> Template(strings=('The ', ' is ', '.'), interpolations=(Interpolation('answer', 'what', None, ''), Interpolation(42, 'value', None, '')))
The only way around this that has occurred to me is, frankly, pretty ugly (I generally dislike eval):
def template(source, vars):
return eval('t' + repr(source), globals={}, locals=vars)
This all seems like a most un-Pythonic lack of orthogonality. I have done some searching and there does not seem to be any discussion about this (at least, none I could find).
Is there a good reason for not having a str.tempate() method? If not, I would like to propose adding one (and am willing to type up a formal PEP to this end).