4 ms·
I wrote a version of this some time ago: https://github.com/ekimekim/pylibs/blob/master/libs/interpolate.py https://github.com/ekimekim/pylibs/blob/master/libs
by ekimekim 12y ago
I wrote a version of this some time ago:
https://github.com/ekimekim/pylibs/blob/master/libs/interpolate.py https://github.com/ekimekim/pylibs/blob/master/libs/interpol...
It's not quite as natural as your one:
def foo(x):
print interpolate("Hello, {x}")
Though I do actually prefer having the explicit formatting call there so I know when the interpolation is being performed. Side effects and all that. In a perfect world, this is the syntax I'd prefer:
def foo(x):
print "Hello, {x}".format()
ie. a format() without args defaults to "all variables accessible in the current scope".
I wouldn't actually want it to support arbitrary python the way ruby does, I find the .format() syntax flexible enough.
(Also, my current implementation is for locals only. It wouldn't be hard to extend to globals, but would suffer the "nonlocals won't be captured" problem described in other comments here no matter what)
(EDIT: Also, it relies on sys._getframe, which is CPython specific)