Mercurial/Python - 下划线函数有什么作用?

发布于 2024-09-06 18:19:14 字数 890 浏览 6 评论 0原文

在 Mercurial 中,许多扩展将其帮助/语法字符串包装在对下划线函数的调用中,如下所示:

 _('[OPTION] [QUEUE]')

这让我感到困惑,因为它似乎没有必要(Writing Extensions 说明没有提及它)并且类中似乎没有定义 _ ,所以我想知道这是否是一些我不明白的特殊语法,也许是 lambda 的另一种说法,或者也许是恒等函数?此外,我想知道这种方法(无论它是什么)相对于文档所建议的原始字符串有什么好处。

我在 Python 文档中没有看到任何提到这样的函数,所以我不确定这是否真的是一个 Python 问题,还是一个 Mercurial 问题。

以下是使用此结构的两个示例(查看文件底部附近的 cmdtable 字典)

In Mercurial, many of the extensions wrap their help/syntax string in a call to an underscore function, like so:

 _('[OPTION] [QUEUE]')

This confuses me, because it does not seem necessary (the Writing Extensions instructions don't mention it) and there doesn't seem to be a _ defined in the class, so I'm wondering if this is some special syntax that I don't understand, perhaps another way to say lambda, or maybe the identity function? Additionally I'm wondering what the benefit of this methodology (whatever it is) is over just the raw string like the documentation suggests.

Nothing I've seen in the Python documentation mentions such a function, so I'm not sure if this is really a Python question, or a Mercurial question.

Here are two examples that use this structure (look at the cmdtable dictionary near the bottom of the file)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

維他命╮ 2024-09-13 18:19:14

看第45行:

from mercurial.i18n import _

这是国际化包中常用的缩写gettext,也可能是其他包也适用于返回其参数到程序当前运行语言的翻译的函数。为了方便起见,它缩写为 _,因为它几乎用于向用户显示的每条消息。

看起来 Mercurial 将其包装在自己的模块中。 (“i18n”代表“国际化”,因为“i”和“n”之间有18个字母。)

Look on line 45:

from mercurial.i18n import _

This is the usual abbreviation in the internationalization package gettext, and possibly other packages too, for the function that returns a translation of its argument to the language the program is currently running in. It's abbreviated to _ for convenience, since it's used for just about every message displayed to the user.

Looks like Mercurial wraps it in their own module. ("i18n" stands for "internationalization" because there are 18 letters in between "i" and "n".)

岁月苍老的讽刺 2024-09-13 18:19:14

由于 gettext 的先例,_(单个下划线的函数名)通常与国际化联系在一起,这是一种 GNU 方法,也在 Python 标准库中找到了一席之地(相同的架构,完全不同的实现)--根据模块的文档< /a>,

gettext.install(domain[, localedir[, unicode[, codeset[, names]]]])

这会将函数 _() 安装在
Python 的内置命名空间,基于
域、localedir 和代码集
被传递给函数
翻译()。 unicode 标志是
传递给结果翻译
对象的 install() 方法。

names参数请参见
翻译的描述
对象的 install() 方法。

如下所示,您通常标记
您的应用程序中的字符串是
翻译候选人,由
将它们包装在对 _() 的调用中
函数,像这样:

print _('This string will be translated.') 

为了方便起见,您
想要安装 _() 函数
在Python的内置命名空间中,所以它
可以在所有模块中轻松访问
您的应用程序。

正如 @ptomato 提到的,Mercurial 遵循了这一传统,将 _ 命名为它们自己的等效函数,用于相同的国际化目的。

还有一个单独的传统,即使用 _ 作为“我不在乎”标识符,例如,

fee, fie, _, _, foo, _, fum = thesevenitemstuple

但当然,您最好不要在同一代码中同时使用这两种传统;-)

_ (a function name of a single underscore) is often associated with internationalization, due to the precedent of gettext, a GNU approach that has also found a place in Python's standard library (same architecture, completely different implementation) -- per the module's docs,

gettext.install(domain[, localedir[, unicode[, codeset[, names]]]])

This installs the function _() in
Python’s builtins namespace, based on
domain, localedir, and codeset which
are passed to the function
translation(). The unicode flag is
passed to the resulting translation
object’s install() method.

For the names parameter, please see
the description of the translation
object’s install() method.

As seen below, you usually mark the
strings in your application that are
candidates for translation, by
wrapping them in a call to the _()
function, like this:

print _('This string will be translated.') 

For convenience, you
want the _() function to be installed
in Python’s builtins namespace, so it
is easily accessible in all modules of
your application.

As @ptomato mentions, Mercurial has followed this tradition by naming _ the equivalent function of their own that they use for the same internationalization purposes.

There is also a separate tradition to use _ as the "I don't care" identifier, as in

fee, fie, _, _, foo, _, fum = thesevenitemstuple

but of course you'd better not be using both traditions at once in the same code;-)

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