I'm documenting my Python code using Sphinx, and read in the Python developer's guide (and I think elsewhere as well) that reST files use an indentation of 3 spaces:
All reST files use an indentation of 3 spaces; no tabs are allowed.
This is the case for the example I copied for my index file, and some other files where my IDE picked up the 3-space indentation and used it for the whole page. The sphinx-apidoc
extension also uses 3 spaces for the modules.rst
file it builds.
On the other hand, because Python uses 4-space indentation, all my docstrings are indented with 4 spaces. Moreover the .. automodule::
directives generated by sphinx-apidox
are indented with 4 spaces.
The point is, it all still works! So I'm left wondering whether the 3-space indentation thing is a requirement, or if it's good practice, but only in terms of style? (And if so, why, when all things Python are 4-space indented?)
Or are there cases where not having 3-space indentation will break my build?
As in Python, indentation is significant in reST, so all lines of the same paragraph must be left-aligned to the same level of indentation.
I'm beginning to think the Python developer's guide might be the anomaly, rather than everything else, especially since in all my searching I've come across basically no discussion of the "3-or-4 space problem" when working with Sphinx and Python.
The minimum number of spaces at each indentation should be 3, and many programmers use a tab mark (typically 8 spaces). The other common style of indentation is based on the keyword. For example, a for loop would indent one more character than an if statement.
For example if you start off using four spaces for an indent, then you should always use four spaces. In the example below, four spaces have been used for the first indent, but only two for the second, and you can see that as a result the code doesn't “line up”. phrase = input("Talk to me > ")
Python uses four spaces as default indentation spaces. However, the number of spaces can be anything; it is up to the user. But a minimum of one space is needed to indent a statement. The first line of python code cannot have an indentation.
We add space in string in python by using rjust(), ljust(), center() method. To add space between variables in python we can use print() and list the variables separate them by using a comma or by using the format() function.
As you have found through your research of the authoritative source and elsewhere, there is no definitive indentation specification, except a minimum of 2 spaces for option lists, and a minimum of 3 spaces for footnotes. See the specification on indentation for reStructuredText.
That said, there are some recommendations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With