I discovered Python in 2005. I had heard a lot of good things about it, and most of it is well deserved. I think I have a good understanding of it now, after coding several scripts and projects that use xml, multithreading, rpc, regexp, etc. I really enjoy coding in Python. For me, it won’t replace a language like Java to create large projects that require good structure and organization, but it is ideal for scripts, quick projects, or projects that benefit from a very flexible language.

I created two scripts to learn Python, that I’d like to share. They also have a useful purpose :) .

  • modImage is a command line tool to modify images.
  • bgChanger is a script that rotates the desktop background on gnome.

I list below my likes and dislikes of the language.

I like:

  • It has powerful built-in types that are part of the language syntax and thus easy to use. Two obvious ones are dictionnaries and lists.
  • It is object-oriented which is a “must-have” in order to have a good project structure. It supports multiple inheritance.
  • It is faster than most scripting language, since there is a sort of “compilation” when a script is first run. Also a couple of features like the generators allow for some nice optimizations in certain cases.
  • Code can be executed from a file, but also from the python prompt. It makes it easy to test a couple of lines of code and creates a sort of “shell extension” that can become extremely powerful the more you know Python.
  • Everything (variables, instances, functions, …) in Python is organized in dictionnaries, which allows easy and natural language introspection. If you need a very dynamic object with attributes created on the fly, that’s great.
  • Basic documentation is very easy to create, which makes you want to actually document your project.
  • It is very easy to wrap C functions with Python. Thus a lot of useful C libraries include Python wrappers.
  • The ability to use function pointers can make things so easy and clean in certain cases. If you have objects with various states that have different behavior, it can be very useful.
  • The re module gives you regex as powerful as what Perl can give you.
  • The threading module, modeled after Java threading, gives you good multithreading capabilities.
  • Python has some nice modern features like decorators. The use of libraries that use them, like D-Bus, is surprisingly natural and compact.

That’s a good amount of positive points. But as often, some advantages can become disadvantages in certain circumstances. I dislike:

  • Exceptions are not checked. That’s a big problem. It means that, basically, a function you call might be throwing an exception, but you are not warned about it, and unless you surround your call with “try/except” the exception will crash your program. It might be better not to have an exception system at all… When you have done some java dev you are used to the ide automatically adding the needed exception handling, sometimes with dozens of different exceptions, giving you the ability to handle every problem nicely. C# has the same problem, its designer talks about it here, and I don’t think his arguments are valid.
  • It lacks a good editor/ide. vim is good for quick hacks and eclipse with the Pydev plugin seems to be the best option. It offers basic completion for only one dereference and can integrate pylint.
  • Definitely slower than C++ or Java, even with Psyco. Benchmarks put it approximately 5 to 10 times slower.
  • Not truly portable, since Python can differ between platforms. Jython can be a solution for this problem. Some common Python modules are not strictly organized and their distribution may vary between linux distributions. I think Perl did a better job at searching and managing standard modules. Also, the GUI library uses GTK, so it won’t work on all platforms…
  • Then there is a more implicit problem. Basically you tend to have fewer bugs in your program since your code can be more compact and clean, but working with dictionnaries and dynamic typing makes it impossible for the compiler to detect a lot of mistakes in the code. It is very frustrating to see your program crash on a stupid error or typo. It also means that some stupid errors will show up only when the code is executed, and it’s difficult and time consuming to test every little case in your program. Fortunately Pychecker and Pylint can be run to detect a lot of mistakes, but won’t say anything past one dereference, in case of cast error, or if you have a typo in the key of a dictionnary.
  • Be ready to type the word “self” (equivalent of “this”) everywhere, since it is not implicit… Ok, you can shorten it to “s” and some people find it safer.
  • The hierarchy for organizing your project can become very unintuitive and difficult, especially when you are used to Java’s simple and clean system for packages and imports. First you have to differentiate between folders, packages, modules and classes. Each of them have different names and the practices are not consistent even over Python’s standard library. Also, statements for importing are more complex “from … import … as …” and create confusion. Last, the decision was made to make every “import” absolute. It means that you can access the standard packages directly (good for quick scripting) but you have to work harder to access modules in the working folder of your own project!
Please see the sidebar for other sub pages.

Comments are closed.

Trackback this Post |