问题 Sphinx和Docstrings中的RestructuredText:单引号与双引号或反向引号差异


从文档中可以看出,双引号用于 文字,而当有代码文本被解释时使用单个反引号。

这将导致我为方法编写docstring f() 下面:

class A(B):

    def f(arg1, arg2):
        return B(arg1 + arg2 + self.index)

如:

Takes two arguments, ``arg1` and ``arg2``, which are assumed to be objects
of type (or duck-type) `NiceClass`, and returns a new object of class `B`
with `B.something` assigned some hash of ``arg1`` and ``arg2``.

这是正确的吗?

在许多代码示例中,Sphinx和其他方面,我看到相当于 B 和 NiceClass 用双引号括起来(“``B``”和“``NiceClass``”)。


2968
2018-03-07 17:40


起源



答案:


来自 Sphinx文档

默认情况下,默认角色(`content`)没有特殊含义。您可以随意使用它,例如:变量名;使用 default_role config值将其设置为已知角色。

作为个人喜好,在编写Python文档字符串时,我使用解释性文本(单个反引号)来表示Python名称和虚线路径,无论它们是否在docstring位置的范围内。所以在你的情况下我会放 arg1arg2NiceClassB 和 B.something 所有在单个反引号中,可选择添加适当的 :class::mod: 等角色。


9
2018-03-07 17:54