Skip to content

Instantly share code, notes, and snippets.

@GaryLee
Last active October 2, 2017 13:04
Show Gist options
  • Save GaryLee/ae1e2fee40181be53ecde23d2af1fd54 to your computer and use it in GitHub Desktop.
Save GaryLee/ae1e2fee40181be53ecde23d2af1fd54 to your computer and use it in GitHub Desktop.
Format string with caller's variable scope.
def fmt(spec):
"""Format string with caller's variable scope."""
return spec.format(**sys._getframe(1).f_locals)
# fmt() can be also written in lambda form.
# fmt = lambda s: s.format(**sys._getframe(1).f_locals)
a = 10
b = 20
c = 30
print fmt('{a} {b} {c}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment