Last active
October 2, 2017 13:04
-
-
Save GaryLee/ae1e2fee40181be53ecde23d2af1fd54 to your computer and use it in GitHub Desktop.
Format string with caller's variable scope.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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