Mercurial/Python - 下划线函数有什么作用?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看第45行:
这是国际化包中常用的缩写gettext,也可能是其他包也适用于返回其参数到程序当前运行语言的翻译的函数。为了方便起见,它缩写为
_
,因为它几乎用于向用户显示的每条消息。看起来 Mercurial 将其包装在自己的模块中。 (“i18n”代表“国际化”,因为“i”和“n”之间有18个字母。)
Look on line 45:
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".)
由于
gettext
的先例,_
(单个下划线的函数名)通常与国际化联系在一起,这是一种 GNU 方法,也在 Python 标准库中找到了一席之地(相同的架构,完全不同的实现)--根据模块的文档< /a>,正如 @ptomato 提到的,Mercurial 遵循了这一传统,将
_
命名为它们自己的等效函数,用于相同的国际化目的。还有一个单独的传统,即使用
_
作为“我不在乎”标识符,例如,但当然,您最好不要在同一代码中同时使用这两种传统;-)
_
(a function name of a single underscore) is often associated with internationalization, due to the precedent ofgettext
, a GNU approach that has also found a place in Python's standard library (same architecture, completely different implementation) -- per the module's docs,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 inbut of course you'd better not be using both traditions at once in the same code;-)