查看文章 |
My First Impression of Pythonic
2008-08-18 12:14
Now I settled in python, as "There is obviously ONE way to do it." sounds sweet, and there won't be endless "end"s for me to deal with. Meanwhile, the library support is great compared to the "exotic" languages I play with, especially when I am to do some GUI programming. I'm quite happy learning how python handles the 'value vs reference' and 'equality check' problem. In C and C++, we have stack-based objects and heap-based objects, and references are explicit (in the form of pointers or references in C++). This scheme may be a bit complicated but "I know what I am doing". In languages like Java/C#/ruby, the variables of 'raw' types like int, char etc., will 'store' the value, while those of user-defined types will store the reference. So the question "When does the equal sign mean equality? And which type of 'equality'?" is still a somewhat complicated one, and the implicit nature of the value/reference distinction makes the situation even worse for beginners. And in python, variables ALWAYS store references. And even fixed-length integers are immutable. (Every time a variable is assigned with a integer, it references another int object.) Here, the equal sign ALWAYS performs equality check, and the 'is' keyword ALWAYS performs identity check. No confusion, no exception. And I believe such design choice is one place where the elegance of python really lies, which the python programmers would describe as "pythonic". (Actually, the practice above is commonly found in FPs by default, but seemly not in 'convensional' languages. It's a good thing to have a mathematician as the designer of python, as he realizes the elegance we need here, is not the one we need in literatures.) |
最近读者: