python* 中字符串插值的实现有什么问题

发布于 2024-11-29 23:51:29 字数 441 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木有鱼丸 2024-12-06 23:51:29

不确定你到底想要实现什么,但这有效吗?

I = '{}\n'.format
I(some_func())

def I(func):
    return "%x\n" % func()
I(some_func())

使用评论中的示例,

I([x*2 for x in [1,2,3]])

效果很好(尽管我不知道您希望输出是什么样子),

I(''.join((self.name, ' has ', self.number_of_children)))

但您实际上应该只是做

'{} has {}'.format(self.name, self.number_of_children)

一行。

Not sure exactly what you're trying to accomplish, but does this work?

I = '{}\n'.format
I(some_func())

or

def I(func):
    return "%x\n" % func()
I(some_func())

Using your example from the comment,

I([x*2 for x in [1,2,3]])

works fine (though I don't know what it is you want the output to look like), as does

I(''.join((self.name, ' has ', self.number_of_children)))

but you should really just be doing

'{} has {}'.format(self.name, self.number_of_children)

which is still one line.

恰似旧人归 2024-12-06 23:51:29

这就是我想出来的。

在 my_print.py 中:

import sys

def mprint(string='', dictionary=None):
    if dictionary is None:            
        caller = sys._getframe(1)
        dictionary = caller.f_locals
    print string.format(**dictionary)

示例:

>>> from my_print import mprint
>>> name = 'Ismael'
>>> mprint('Hi! My name is {name}.')
Hi! My name is Ismael.
>>> new_dict = dict(country='Mars', name='Marvin',
...                 job='space monkey', likes='aliens')
>>> mprint("Hi! My name is {name} and I'm from {country}."
...     " Isn't {name} the best name?!\nDo you know any other {name}?", new_dict)
Hi! My name is Marvin and I'm from Mars. Isn't Marvin the best name?!
Do you know any other Marvin?

请参阅:

Python字符串插值实现

This is what I came up with.

in my_print.py:

import sys

def mprint(string='', dictionary=None):
    if dictionary is None:            
        caller = sys._getframe(1)
        dictionary = caller.f_locals
    print string.format(**dictionary)

example:

>>> from my_print import mprint
>>> name = 'Ismael'
>>> mprint('Hi! My name is {name}.')
Hi! My name is Ismael.
>>> new_dict = dict(country='Mars', name='Marvin',
...                 job='space monkey', likes='aliens')
>>> mprint("Hi! My name is {name} and I'm from {country}."
...     " Isn't {name} the best name?!\nDo you know any other {name}?", new_dict)
Hi! My name is Marvin and I'm from Mars. Isn't Marvin the best name?!
Do you know any other Marvin?

See:

Python string interpolation implementation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文