python* 中字符串插值的实现有什么问题
import re
r = re.compile("#{([^}]*)}")
def I(string):
def eval_str_match(m):
return str(eval(m.group(1)))
return r.sub(eval_str_match,string)
* 除了 python 的品味/风格/标准
有没有比单字母方法更简洁的方式来调用它?
正则表达式是否会遗漏任何内容?
我应该使用 repr 而不是 str 吗?
我知道 eval 可能很危险,但我不明白为什么
I("#{some_func()}\n")
更糟糕
"%s\n" % str(some_func())
import re
r = re.compile("#{([^}]*)}")
def I(string):
def eval_str_match(m):
return str(eval(m.group(1)))
return r.sub(eval_str_match,string)
* besides python taste/style/standards
Is there a nicer succinct way to call it then a single letter method?
Is there anything the regex could miss?
should I use repr instead of str ?
I know that eval can be dangerous but I can't see why
I("#{some_func()}\n")
is worse then
"%s\n" % str(some_func())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定你到底想要实现什么,但这有效吗?
或
使用评论中的示例,
效果很好(尽管我不知道您希望输出是什么样子),
但您实际上应该只是做
一行。
Not sure exactly what you're trying to accomplish, but does this work?
or
Using your example from the comment,
works fine (though I don't know what it is you want the output to look like), as does
but you should really just be doing
which is still one line.
这就是我想出来的。
在 my_print.py 中:
示例:
请参阅:
Python字符串插值实现
This is what I came up with.
in my_print.py:
example:
See:
Python string interpolation implementation