Whitespace is signification in Python in that code blocks are defined by their indentation.
Furthermore, Guido van Rossum recommends using four spaces per indentation level (see PEP 8: Style Guide for Python Code).
What was the reasoning behind not requiring exactly four spaces per indentation level as well? Are there any technical reasons?
It seems like all the arguments that can be made for making whitespace define code blocks can also be used to argument for setting an exact whitespace length for one indentation level (say four spaces).
Python doesn't care, as long as you're consistent. So if you start using four spaces for your indent, you always need to use four spaces. For example, in this snippet of code, we can see that the first indent has four spaces, but the second indent has only two. And you can see that the code doesn't line up.
Note: Python uses 4 spaces as indentation by default. However, the number of spaces is up to you, but a minimum of 1 space has to be used.
Python Indentation Rules 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.
Not only does Python require this indentation so that it can recognize the code blocks, but the consistent indentation makes it easy for people to recognize the if/elif-conditions and their corresponding code blocks. As with a regular if-statement, the else-block is optional.
There are no technical reasons. It would not be too hard to modify the Python interpreter to require exactly four spaces per indentation level.
Here is one use case for other indentation levels: when typing into the interactive interpreter, it's very handy to use one-space indentations. It saves on typing, it's easier to count the number of spaces correctly, and readability is not a major concern (since the code isn't even saved in a file).
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