Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Python require exactly four spaces per indentation level?

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

like image 488
knorv Avatar asked Jun 03 '10 13:06

knorv


People also ask

Does Python only accepts 4 space per indentation?

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.

Does Python require 4 spaces?

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.

How many spaces are required per indentation level in Python?

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.

Why do blocks in Python need to be indented at the same level?

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.


1 Answers

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

like image 94
Daniel Stutzbach Avatar answered Oct 04 '22 17:10

Daniel Stutzbach