Archive for October, 2008

What’s new in Python 2.6

Full release notes are here. Here are something that catch my eyes.

  • with statement with is a statement that simplifies the task of elegantly acquiring and releasing resources, and moves the task of handling exception to the resource manager author. For example, the file object now supports with statement, and when using this statement, the job of closing the file on regularly releasing the object or on an exception will be the task of the file object itself. It also makes locking/unlocking a resource amazingly intuitive, by moving the real (and repeated) job to the resource manager author.
  • per-user site-packages. Without this, I have always been making my own per-user site-pacakages. Handling different versions of packages for different Python versions is not an easy task. Now it’s automatically done. No need to sudo when installing a package!
  • multiprocessing package. I have had some very bad experience with Python’s threading module. Python threading is useless in many cases when your code enters a chunk of time-consuming C code: even when the C code is blocked by I/O, the other Python threads will not be waken up if the C code is not Python-aware. This issue is made worse by the infamous GIL, which means your program will always be executed serially no matter how nice your multi-threading code skill is or how many CPUs you have. Multiprocessing is way out of this, though it consumes a bit more resources. It also has the advantage of being transparently distributed: you can run multiple processes across a cluster of computer very easily.
  • Advanced string formatting. Now you can have many nice features found in Django‘s template engine.
  • print is now a function. This allows you disable all prints in your program by redefining it. I was typically too lazy to use the logging module for debugging purpose, and now this change to print saves me some time from removing all debugging print statements. While, logging is still better.
  • New I/O library, which looks so similar to Java’s I/O library.
  • Abstract Classes. Now if you want to build some extensible/customizable software, you have an easy way to specify which methods must be defined instead of just saying it in the document.
  • fractions module. I like Python’s support for long integer, decimal and now fraction data type. double is evil in many cases despite its efficiency.
  • json module is now part of Python standard library.
  • Now both md5 and sha modules have been removed. Use hashlib instead.


Read Full Post »