My Impression with Python
The last week end, I try to port my Project EasyBackup to Python. I read about the language. I have a book on it. I can read the code quite well I think :p. I’ve seen python code from a few open source projects. But this is the first time I am actually write a useful program in Python.
My first impression about the language (after actually use it) is “I don’t like it”. There are number of reasons but many of them may be because I am not yet familiar with the language so I don’t want to talk much about them (as I don’t want to blame the language for my ignorance). But there is one thing that I cannot really take it and I think it is not about the familiarity. The thing is the indentation.
In Python, the indentation is not only used to make code easy to read. It is also used to represent stack or block level of the code. And that is the problem. We are playing with the invisible here. Indentation is not very visible. Is that a tab or spaces. What is considered as an indent? I need to also indent an empty line? If I have a long line how should I write (do I need to indent the wrapped line)?
Different tools manage white space differently. And White spaces are what they are … White. and cannot be seen directly. This make code difficult to manage. Some editors erase tail spaces. They can cause so much problem to Python developers as empty lines will become a different indentation.
I know that using indentation to indicate block make parsing Python code very easy and fast. It also allow a greater dynamicality to the language as some declaration does not need to be visible to the runtime environment until it actually run it. Like in the following case,
01| Do somthing 02| if Condition: 03| def F1(): 04| ... 05| Do more thing
F1 may not be defined if the condition is False. The interpreter can simple see the indentation on line 03 and 04 and skip to line 05. Again, this may writing python interpreter very easy. If this is language with { … } the interpreter must go through all character to ensure the right pair of { and } governing the block.
The problem is that indentation should be use for Human sake as we are the one who will need to make sense of the code in order to understand it or at least should get in the way of doing so. You may write or rewrite the code a few times but you will likely to need to read it many more times. Make the code easy to human eyes is much more important than to the interpreter.
In python, I can’t do this,
01| IsEndWithBreak = false;
02| while(...) {
03| if(Condition) { IsEndWithBreak = true; break; } // Grouping less important statement
04| Do something repeitive and much more important;
05| }
The break-if-condition-is-true part can be made less visible (and get it out of the way for the more important code after) in language with { … } as the indication of block.
AND – How about this:
01| if(... A loooooooooooooooooooooooooooooooooooooooong condition ...) 02| I = 10*f1(10, -10) + f2(50*x+2); 03| else if(A short Condition) I = 10*f3(10, 10) + f4(50*y+2); 03| else I = 10*f5(10, 10) + f6(50*z+2);
As you can see, the alignment of I emphasizes as a visual clue that I will get a different value depending on the condition. The different of the two values can be easily spotted as it lie next to each other. Even if we don’t care what the values are. We can easily recognize that the value of I will change one way or another at this point. It is also easy to see that these lines of code are all about changing the value of I not anything else (so if you are not looking for the changing value of I, you can ignore these group of lines).
Compare that to what you may write in Python.
01| if ... A loooooooooooooooooooooooooooooooooooooooong condition ...: 02| I = 10*f1(10, -10) + f2(50*x+2); 03| else if(Another Cond): 04| I = 10*f3(10, 10) + f4(50*y+2); 05| else: 06| I = 10*f5(10, 10) + f6(50*z+2);
OR
01| if ... A loooooooooooooooooooooooooooooooooooooooong condition ...: 02| I = 10*f1(10, -10) + f2(50*x+2); 03| else if(Another Cond): I = 10*f3(10, 10) + f4(50*y+2); 03| else: I = 10*f5(10, 10) + f6(50*z+2);
The visual clue is not very clear as the one above.
Since people like things differently (one spacing style may be easy to read for one and not so easy another), it is simply better to have options. Restricting the use of white spaces to a certain format does not provide such options.
To conclude, my first impression with Python is not very good. Mostly because Python force the use of white spaces in a restricted way. Managing the invisible whitespace is hard and mistake prone. Although not difficult to solve but it can be quite a difficult to detect. White spaces are supposed to be a tools for developers to make the code easy for them to read. They should be able to use white spaces in anyway they like formulating what ease they eyes and what feels more organized.


Recent Comments