Skip to content

Instantly share code, notes, and snippets.

@carymrobbins
Last active December 21, 2015 21:49
Show Gist options
  • Save carymrobbins/6371267 to your computer and use it in GitHub Desktop.
Save carymrobbins/6371267 to your computer and use it in GitHub Desktop.
String interpolation in Python.
import inspect
def iformat(string):
frame = inspect.currentframe()
result = string.format(**dict(frame.f_back.f_globals, **frame.f_back.f_locals))
del frame
return result
class Object(object): pass
SOME_CONST = 12
def main():
o = Object()
o.foo = "hey!"
shoe = dict(a=1, b="cat")
print iformat('{SOME_CONST} - {o.foo} - {shoe[b]}')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment