This is a post I’ve been trying to write for about 2 weeks now. As some of you might know, I’ve spent the previous weeks studying python and writing small scripts and I’ve decided to write a blog entry about it. As a matter of fact, I’ve also looked over the Pylons framework, but I’ll write about it in a another post. So here it is, my opinion about python alone:
What I like about python
Well, I loooooooooove the indentation. I really do. Python made it impossible for lamers to write ugly “one liners”. Everything must be indented and in its place or it won’t even compile (compiling aka no syntax errors as python is an interpreted language). After years of dealing with ugly sources with no braces, no indentation and so on, this feature is like a gift from heavens for me. I really hope it will catch on and be implemented in other languages.
I also like the for in iteration over…well…everything. This code:
for item in collection:
do_stuff(item)
…works in most cases, even when collection is a file. In which case the loop iterates over the file’s lines. Tuples, dictionaries and lists are cool features.
What I don’t like about python
Of course, there are some things I dislike about this programming language. The first thing is that sometimes is too verbose. Python doesn’t have an post/pre increment operator. You can’t write i++ or ++i, although this code compiles. Further more, it compiles and does nothing, taking the act of debugging to a whole new level of annoyance.
You always have to write i += 1. It also doesn’t have a ternary operator. If you write a = (condition) ? b : c it will give you an compiling error.
Another weak point is its OOP capabilities. Object orientated programming is very strangely implemented in python. A class example in python looks something like this:
class MyClass:
def __init__(self):
self.attribute = 'default value'
def custom_method(self, attribute):
self.attribute = attribute
def print_data(self):
print self.attribute
obj = MyClass()
obj.custom_method('wassabi')
obj.print_data()
As you can see, there are no access modifiers (private, protected, public), no instantiation operator (new), the this keyword is replaced by self, and you must write it every single time you define a new method in the class. And python also allows multiple inheritance, which does one thing: annoys people.
Conclusion
Apart from some really annoying “features”, I’m starting to like python. It provides a quick way and pretty clean way to do get things done. And in the end, this is all that matters…Python is cool!
Hi,
You’ll find that Python does have a ternary operator, it’s just different in syntax.
You can do:
if you haven’t come across list comprehensions yet then you have more goodies in store.
- Paddy.
Hi, regarding the ternary operator you can also “short-circuit” boolean evaluation
so
pretty cool eh?
logan> cool trick, but it only works when b doesn’t evaluate to false.
if I were to write the same snippet in php using the ternary operator, it would look like:
Tudor, Logan;
Now that Python has its own ternary operator we should use it. Before the ternary operator we /had/ to rely on short-circuit evaluation, now we should let it recede to its remaining uses.
- Paddy.
Paddy> what do you mean by now? Python 3.x? In my console things look like this:
tudor@tudor:/$ python Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> print sys.version 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) [GCC 4.3.3] >>> a = 5 >>> b = 3 >>> c = (1 == 1)? b : c File "<stdin>", line 1 c = (1 == 1)? b : c ^ SyntaxError: invalid syntaxIf they’ve added the ternary operator to the new version of the language, this is great news.
But I think the lack of the pre/post increment operator is even more annoying, as it doesn’t throw any errors. It just doesn’t work. Yes, I know why it doesn’t throw any errors, but I still find annoying :p
Regarding the post/pre increment, I got the impression those are not present because they’re too susceptible to misuse which results in bugs. Many of the features of Python seem to be designed intentionally to prevent people from making common mistakes, like the lack of an ‘unless’ statement which I’ve seen lead people to more boolean logic errors in Perl than I care for.
For your class, you should have it inherit from object, otherwise you’re using old-style classes, and not the new-style classes from Python 2.2 and on. I.e.:
class MyClass(object): passAs Paddy mentioned, the syntax for ternary is different in Python, it was added in Python 2.5:
The syntax and keywords used in the Python ternary operator are not those of C. you would write:
Pythons designers looked at common cases of errors in C programming and found that ++ and — can be very confusing. Their is nothing stopping people writing C code with ++ and — that can be both implementation dependant in its output and confusing to read. for example, what does this mean?
for similar reasons, Python does not allow assignment as an expression – this means that you cannot assign to a name in an if statements condition.
It is all part of Pythons wish to be easy to read and so maintain. Some of the ommisions might be irksome at first, but when you read other peoples code, it should pay off.
- Paddy.
I remember that when I have started learning how to use python classes I have found a way to make attributes someway private. They weren’t private in the sense that Java, C++ or C# handles them, but it was a little hack that wouldn’t allow you to handle their values directly.
When I’ll have the time I’ll dig in and show you how to do it if you don’t discover it by then.
It’s fairly easy to obtain getters/setters behind attributes in python, which can then handle access to them. One would just have to use property()
In my opinion access modifiers don’t mean a language has better OOP capabilities. PHP5 introduced these, but were there any advantages? I believe not. On the other hand, magic methods like __get/__set and __call really improved the language.
It’s the same with python. You don’t get access modifiers, but you get tons of other things that are impossible in PHP or Java.
The only thing I don’t like about python is its lambda form. Anyway, lambdas should not be abused as it makes for a harder to test/extend API.
Oh, and the strange join() method of strings. I’m used to it being on sequences rather than strings.