I was asked in an interview yesterday what I think of Python white space.
Python is unusual (possibly unique?), in that the indentation of your code is significant, and effects the way that your code is compiled / runs.
I know that when I first used Python, I was ambivalent about this feature. But as a seasoned user of Python, I think it is a good feature.
Firstly, the way we lay out code is often designed to make it more readable for humans. But if the way the code runs ignores this layout, then there is a danger that bugs will be introduced through this disconnect.
Secondly, the fact that whitespace is significant allows Python to eliminate braces (or other code grouping statements): so you avoid all the coding standards “discussions” on the correct style of braces, which can occupy huge amounts of (unproductive) time, and adversely impact team morale before you have even started.
Here’s an example in C:
#include <stdio.h>
int main(void)
{
int a = 1;
int b = 2;
if (a == 1) {
printf("a is 1\n");
}
if (a==1)
{
printf("a is 1 still\n");
}
if (a==1)
{
printf("a is 1 yet again\n");
}
if (a==1 && b==3)
printf("a is 1 for the last time\n");
printf("b is 3\n");
}
All three of the bracing styles that I have encountered are displayed here (I won’t inflame the discussion by stating my preference, only to say that the example above is inconsistent in its use of braces, and that is not desirable. The final section is potentially misleading to a developer, although the compiler will not complain.
The common point of all three of the styles of braces is that the code within the braces is indented. Which the Python approach follows as well. Here is the equivalent Python example:
a = 1
b = 2
if a == 1:
print("a is 1")
if a==1:
print("a is 1 still")
if a==1:
print("a is 1 yet again")
if a==1 and b==3:
print("a is 1 for the last time")
print("b is 3")
Note that, because whitespace is significant, it is essential that everyone on the team ensures that tabs are converted to spaces, or their tab indent is set to the same value. In my experience, the former is preferable.