実装した関数
from inspect import currentframe,getargvalues
def varfmt(target,*argv):
return target.format(**getargvalues(currentframe().f_back).locals)
【※追記※】@shiracamus さんより、python3.6以降なら以下の書き方でOKとのコメントを頂きました。
ありがとうございます!
def hoge_aa_print(hoge, aaa):
print(f"{hoge} {aaa}")
hoge_aa_print(10, 20)
[参考]
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)
このように、変数名をキーとして文字列のformatを行うことが出来ます。
補足
誤り、マサカリ、苦情、鬱憤晴らし等有りましたらコメントお願いします。