I’m reading PEP 8 and notice that it does not say anything on maximum levels of indentation. Of course the 79 character limit puts a limit to levels of indentation, but is there a recommended maximum indentation level in Python? What’s your take on this? Do you have company level policies?
As an example from somewhere else, Linus Torvals states in the Linux kernel Style Guide that
[…] The answer to that is that if you need more than 3 levels of
indentation, you’re screwed anyway, and should fix your program.
Readability is the most important thing here. 4 or 5 levels of indentation over a dozen lines isn’t really as much of a problem to read as 3 levels in a function that runs over 4 or 5 pages/screens
If you are wondering if you have too much indentation, you probably do. Can you refactor your code to use some functions? list/dict/set comprehensions? Maybe look at itertools to see if something there can help? …