3 ms·
For those that want something close today, there's https://pypi.python.org/pypi/say https://pypi.python.org/pypi/say fmt("Hello, {name}! You have {len(msgs
by jonathaneunice 11y ago
For those that want something close today, there's https://pypi.python.org/pypi/say https://pypi.python.org/pypi/say
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.
- RubyPinch 11y agosay looks nice, but it still suffers from exactly what this pep is trying to fix >>> def test(x=1):return lambda:say.say("{x}") >>> test() NameError: name 'x' is not defined https://github.com/syrusakbary/interpy https://github.com/syrusakbary/interpy seems like a closer solution, could be modified to also support format specs
- jonathaneunice 11y agoI think you're right that Interpy and PEP 498 have a different, earlier binding strategy than say does. I've found the late / local binding of say() and fmt() convenient. Are there use cases where that earlier binding is valuable or critical? Your 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'
- kazinator 11y ago> * fmt("Hello, {name}! You have {len(msgs)} waiting.")* 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.
- jonathaneunice 11y agoYes indeed. That's exactly how it works. Introspection. 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?