捕获字符串格式中的 **vars() 模式
我经常发现自己使用以下模式进行字符串格式化。
a = 3
b = 'foo'
c = dict(mykey='myval')
#prints a is 3, b is foo, mykey is myval
print('a is {a}, b is {b}, mykey is {c[mykey]}'.format(**vars()))
也就是说,我经常需要在本地命名空间中打印值,通过调用 vars() 来表示。然而,当我查看我的代码时,不断重复 .format(**vars())
模式似乎非常不符合 Python 风格。
我想创建一个函数来捕获这种模式。它会像下面这样。
# doesn't work
def lfmt(s):
"""
lfmt (local format) will format the string using variables
in the caller's local namespace.
"""
return s.format(**vars())
只是当我进入 lfmt 命名空间时,vars() 不再是我想要的了。
如何编写 lfmt 以便它在调用者的命名空间中执行 vars() ,以便以下代码可以像上面的示例一样工作?
print(lfmt('a is {a}, b is {b}, mykey is {c[mykey]}'))
I frequently find myself using the following pattern for string formatting.
a = 3
b = 'foo'
c = dict(mykey='myval')
#prints a is 3, b is foo, mykey is myval
print('a is {a}, b is {b}, mykey is {c[mykey]}'.format(**vars()))
That is, I often have the values I need to print in the local namespace, represented by a call to vars(). As I look over my code, however, it seems awfully unpythonic to be constantly repeating the .format(**vars())
pattern.
I'd like to create a function that will capture this pattern. It would be something like the following.
# doesn't work
def lfmt(s):
"""
lfmt (local format) will format the string using variables
in the caller's local namespace.
"""
return s.format(**vars())
Except that by the time I'm in the lfmt
namespace, vars() is no longer what I want.
How can I write lfmt so that it executes vars() in the caller's namespace such that the following code would work as the example above?
print(lfmt('a is {a}, b is {b}, mykey is {c[mykey]}'))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑:为了使
lfmt
在从不同命名空间调用时正常工作,您需要inspect
模块。请注意,正如文档警告,inspect 模块可能不适合生产代码,因为它可能不适用于 Python 的所有实现
Edit: In order for
lfmt
to work when called from different namespaces, you'll need theinspect
module. Note, as the documentation warns, theinspect
module may not be suitable for production code since it may not work with all implementations of Python您必须检查调用框架中的变量。
这将帮助您开始:
You have to inspect the variables from the calling frames.
This will get you started:
在这里:
它有效的事实并不意味着您应该使用它。这就是开发人员所说的“重大黑客攻击”,通常附带注释“XXX 修复我 XXX”。
Here you are:
The fact that it works doesn't mean you should use it. This is what developers call "major hack", usually shipped with a comment "XXX fix me XXX".
每次调用函数时都输入
,vars
是不是很糟糕?Is it so bad to type
,vars
each time you call the function?您也可以使用
sys
代替inspect
,但我不知道它是否与inspect
的不同实现存在相同的问题。据我所知: Python 字符串插值实现
You could also use
sys
instead ofinspect
, but I don't know if it has the same problem with different implementations asinspect
has.This is as far as I got: Python string interpolation implementation