What about..
def template(name, age):
return f"My name is {name} and I am {age} .."
formatted = template('Amy', 32)
?[Edit: I only started learning Python recently, and this is how I'd think of doing it come from JS:
const template = (name, age) => `My name is ${name} and I am ${age}`;
const formatted = template('Amy', 32);
]
Re:Python 3.6.0 released
by mixmastamyk on Friday December 23, 2016 @03:16PM
(#13246989)
Check string.Template from the docs.
You can always put your f-string in a lambda if you don't want to evaluate it immediately. Pulling values from the scope of the function you call your apply() method from, though, is some scary magic. I wouldn't be confident that I hadn't screwed that up.
Yeah, using templates like that is something you can do with format (and then just do template.format(arg_name1 = something1, arg_name2 = something2) and it seems like usecase important enough to not use f-string exclusively.
Re:Python 3.6.0 released
by mixmastamyk on Friday December 23, 2016 @02:34PM
(#13246766)
Cool.
string.Template might be what you are looking for.
Though I think it would be cool if you could have a deferred format string, like so:
So that way you could store the template as a constant somewhere and apply it after receiving input from the source of your choice.You can kind of do that with [0], but it doesn't evaluate expressions (so the age + 1 thing won't work).