by jonathaneunice · 11y ago · 0 comments
fmt("Hello, {name}! You have {len(msgs)} waiting.")
Interpolates local variables and expressions. It uses the format method, and has all of format's output formatting. >>> def test(x=1):return lambda:say.say("{x}")
>>> test()
NameError: name 'x' is not defined
https://github.com/syrusakbary/interpy seems like a closer solution, could be modified to also support format specsYour example highlights the binding strategy, but more typical would be:
>>> def test(x=1):
... return fmt("{x}")
...
>>> test()
'1'
>>> test(12)
'12'
>>> test('woobers')
'woobers'Yikes!
Please don't tell me that's a function which peeks at the caller's lexical variables, at run time, by name?
I see "inspect.currentframe().f_back" hacks in the code, good grief.
Given a language that doesn't support templated strings inherently, how else would you provide that feature?
Though technically, `fmt` is not a function per se. It's an object with a `__call__` method. That doesn't improve the situation for you, does it?