People have been pondering about Python’s use of whitespaces for a long time, and the debate is still going on. Many people believe that there isn’t any definitive way to do it well, so they leave it up to each individual programmer. In this article, I’m going to share my thoughts on the matter, list all existing options and try to point out their pros and cons.
I don’t claim to be an expert in Python indentation at all (there aren’t many experts around), but I’ve learned a lot throughout the years while trying different styles and discussing them with other programmers. My main objective is to open your eyes towards other available choices you might want to take into consideration before making your final decision regarding this matter.
Of course, the choice is entirely up to you, but I firmly believe it’s worth spending some time on this matter in order to avoid making a terrible decision that will be hard to revert later on.
Here are 8 Ridiculous Rules about PYTHON’s Indentation:
1. Follow PEP 8 guidelines
Some of you might be surprised to see this one on top of the list, but there are many people out there who obey this rule religiously. If you don’t know what PEP 8 is, here’s a short explanation: it’s a coding standard for Python created by Guido himself, so you’d better follow it!
2. Tab or no tab? That is the question…
Most people believe that using tabs for indentation is bad because they don’t align properly when code is being viewed at different resolutions. Instead, spaces should be used according to popular belief. What most people don’t know though, is that there isn’t any hard rule regarding this matter (except for whitespace at the end of).
3. Use a single space character for indentation
Even though the original PEP 8 draft recommended a tab size of 4 spaces, they decided to go with a single space instead. This is because the majority of IDEs nowadays are using either 2 or 4 spaces as default tab size, so using a different size would make code harder to read on them. In addition, some people believe that horizontal white space should be minimized in order to make code more compact and readable. You can decide which one you want to use though – it’s still up to you!
4. Never mix tabs and spaces
This means that if you’re going to use spaces for indentation then everything should consistently use that number – no mixing whatsoever! I’m not even going to start about this one because it’s ridiculous. Just don’t.
5. Put opening braces on the same line as the function declaration/class definition/etc
Some people put their opening braces on a separate line above their functions
Which style you choose is completely up to you, but I personally prefer the first one because I find that other way messier and harder to read. However, once again – there isn’t any hard rule regarding this, so you should choose the style that best suits your needs.
6. Put a new line between functions
This is a very popular way of writing Python code and it’s being used by many programmers around the globe. It’s also the only style where rules 5 and 6 apply – i.e., there should always be a new line between functions! This rule may seem silly but there are still people out there following it religiously. In my opinion, if you’re going to break any of these rules then at least try not to go against this one 🙂
7. Always pass an empty list as the last argument in a function call
In Python, when calling multiple-argument functions, you can specify default values for some arguments, which are used if the caller omits values for them. One of these arguments must be named “args”, and it should always be the last one in order to make this syntax valid:
That’s why some people put an empty list as the last argument for functions with multiple ones, even though that works too. There is no need to do that though!
8. Use one import per line
Some people like using multiple imports on a single line, separated by semicolons. This can make code more readable depending on your coding style I guess, but there are also other ways of importing modules (e.g., from …import *), which are much better when it comes to maintaining code quality in larger projects because they don’t pollute the global namespace.
There you have it – 8 popular (and not-so-popular) Python coding style conventions that are being followed by many programmers out there. Do I recommend using all of them? No, absolutely not! Some of them are just ridiculous IMO, but if you’re programming in a team then at least try to be consistent about their usage. Now that we’ve got that cleared up let’s proceed with more interesting stuff.