可重复使用的F字符串
f-yeah的Python项目详细描述
是的!
可重复使用的F字符串
从代码中删除所有过时的格式代码。 用f-是的,只要加上括号就行了。
用法
不再围绕f字符串进行复制
保持模板干燥,而不还原为旧格式样式。
defaction1(value):assertisinstance(value,int),'Expected value to be an integer, got {type(value)} instead'returnvalue*valuedefaction2(value):assertisinstance(value,int),'Expected value to be an integer, got {type(value)} instead'returnvalue**value
只需编写一次模板,就可以得到保持同步的一致字符串。
fromfyeahimportfbad_check='Expected value to be an integer, got {type(value)} instead'defaction1(value):assertisinstance(value,int),f(bad_check)returnvalue*valuedefaction2(value):assertisinstance(value,int),f(bad_check)returnvalue**value
永远不要再打格式电话了!
为所有模板(本地或全局)合并f-string样式格式。
bad_check='expected value to be an integer, got {type(value)} instead'defaction1(value):assertisinstance(value,int),bad_check.format(value=value)returnvalue*valuedefaction2(value):assertisinstance(value,int),bad_check.format(value=value)returnvalue**value
只需使用与可重用f字符串相同的格式字符串。
fromfyeahimportfbad_check='Expected value to be an integer, got {type(value)} instead'defaction1(value):assertisinstance(value,int),f(bad_check)returnvalue*valuedefaction2(value):assertisinstance(value,int),f(bad_check)returnvalue**value