Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

like image 903
Jeet Avatar asked Mar 07 '14 17:03

Jeet


People also ask

What is Sphinx rst?

RST is a powerful and straightforward markup language that, in combination with Sphinx, provides a wide range of facilities for intelligent and appealing documentation creation. It uses simple and implicit syntax to introduce a variety of content elements such as titles, code blocks, vertical lists, and many others.

Are Docstrings important?

Docstrings are not necessary for non-public methods, but you should have a comment that describes what the method does. This comment should appear after the "def" line.

What makes a good Docstring?

Best practices. All modules, classes, methods, and functions, including the __init__ constructor in packages should have docstrings. Descriptions are capitalized and have end-of-sentence punctuation. Always use """Triple double quotes.""" around docstrings.


2 Answers

From the Sphinx documentation:

The default role (`content`) has no special meaning by default. You are free to use it for anything you like, e.g. variable names; use the default_role config value to set it to a known 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. 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.

like image 182
Vasiliy Faronov Avatar answered Sep 22 '22 12:09

Vasiliy Faronov


Just a reminder, not to be confused with Markdown's backtick string for inline code span.

In Markdown, according to the CommonMark Spec, these are equivalent:

  • Plain text view --> Render results
  • `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:

  • `interpreted text` --> the render result is depended on different definitions.

    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

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

like image 25
YaOzI Avatar answered Sep 24 '22 12:09

YaOzI