简体   繁体   English

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

[英]reStructuredText in Sphinx and Docstrings: single vs. double back-quotes or back-ticks difference

From the documentation, it appears that the double back-quote is used for literals, while the single back-quote is used when there is code text to be intepreted. 从文档中可以看出,双引号用于文字,而单引号则在有代码文本被解释时使用。

This would lead me to to write the docstring for method f() below: 这将导致我为下面的方法f()编写docstring:

class A(B):

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

As: 如:

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``.

Would this be correct? 这是正确的吗?

In many code examples, Sphinx and otherwise, I see the equivalent of B and NiceClass wrapped in double back-quotes ("``B``" and "``NiceClass``"). 在许多代码示例中,Sphinx和其他方面,我看到相当于BNiceClass包含在双引号中(“``B``”和“``NiceClass``”)。

From the Sphinx documentation : Sphinx文档

The default role (`content`) has no special meaning by default. 默认情况下,默认角色(`content`)没有特殊含义。 You are free to use it for anything you like, eg variable names; 您可以随意使用它,例如变量名称; use the default_role config value to set it to a known role. 使用default_role配置值将其设置为已知角色。

As a matter of personal preference, when writing Python docstrings, I use interpreted text (single backquotes) for Python names and dotted paths, whether or not they are in scope at the location of the docstring. 作为个人偏好,在编写Python文档字符串时,我使用解释文本(单个反引号)来表示Python名称和虚线路径,无论它们是否在docstring位置的范围内。 So in your case I would put arg1 , arg2 , NiceClass , B and B.something all in single backquotes, optionally adding the appropriate :class: , :mod: etc. roles. 所以在你的情况下,我会将arg1arg2NiceClassBB.something全部放在单个反引号中,可选择添加相应的:class::mod: etc.角色。

Just a reminder, not to be confused with Markdown's backtick string for inline code span. 提醒一下,不要与Markdown的内联代码跨度的反引号字符串混淆。

In Markdown, according to the CommonMark Spec , these are equivalent: 根据CommonMark规范 ,在Markdown中,这些是等价的:

  • Plain text view --> Render results 纯文本视图 - >渲染结果
  • `inline literal` --> inline literal `inline literal` - > inline literal
  • ``inline literal`` --> inline literal ``inline literal`` - > inline literal
  • ```inline literal``` --> inline literal ```inline literal``` - > inline literal
  • ... ...

    This is because they are all seen as the backtick string : 这是因为它们都被视为反引号字符串

    A backtick string is a string of one or more backtick characters (`) that is neither preceded nor followed by a backtick. 反引号字符串是一个由一个或多个反引号字符(`)组成的字符串,它既不是反引号,也不是反引号。


While in reStructuredText, single backtick surround and double backticks surround are different : 在reStructuredText中,单个反键环绕和双反键环绕是不同的

  • `interpreted text` --> the render result is depended on different definitions. `interpreted text` - >渲染结果取决于不同的定义。

    The rendering and meaning of interpreted text is domain- or application-dependent. 解释文本的呈现和含义取决于域或应用程序。 It can be used for things like index entries or explicit descriptive markup (like program identifiers). 它可以用于索引条目或显式描述性标记(如程序标识符)之类的东西。

  • ``inline literal`` --> inline literal ``inline literal`` - > inline literal

    Normally rendered as monospaced text. 通常呈现为等宽文本。 Spaces should be preserved, but line breaks will not be. 应该保留空格,但不会有换行符。

So in general, reStructuredText's double backticks surround ``code`` is somewhat equivalent to Markdown's single backtick surround `code` . 所以一般来说,reStructuredText的双重反引号``code``在某种程度上等同于Markdown的单个反引号环绕`code`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM