Search code examples
pythonpython-sphinxrestructuredtextdocstring

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


Solution

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