from inspect import currentframe,getargvalues
def varfmt(target,*argv):
return target.format(**getargvalues(currentframe().f_back).locals)
[* Addition *] @shiracamus commented that python3.6 or later can be written in the following way. Thank you!
def hoge_aa_print(hoge, aaa):
print(f"{hoge} {aaa}")
hoge_aa_print(10, 20)
[reference] https://qiita.com/AnchorBlues/items/f7725ba87ce349cb0382 https://docs.python.org/ja/3/library/inspect.html
example
from inspect import currentframe,getargvalues
def varfmt(target,*argv):
return target.format(**getargvalues(currentframe().f_back).locals)
def hoge_aa_print(hoge,aaa):
print(varfmt("{hoge} {aaa}",hoge,aaa))
hoge_aa_print(10,20)
In this way, you can format the character string using the variable name as the key.
If you have any mistakes, Masakari, complaints, dismay, etc., please comment.
Recommended Posts