'with' statement and conditional expressions are handy

When I do personal Python coding, I always work off of a build of Python from a pristine svn checkout. This means that I get to start coding with new features immediately. The unfortunate side-effect of this is that I tend to take for granted new features since I end up using them for a while before the general public does. It also means I forget what is new in a version and what isn't since it is all just one big version to me.

But I still notice on occasion new features that I use constantly. Two of those features that were introduced in Python 2.5 is the 'with' statement and conditional expressions.

The 'with' statement gives one a nice syntax for common ``try/finally`` blocks that one might have (e.g., closing an object just like contextlib.closing gives you). You can think of it as a syntactic version of C++'s RAII (Resource Allocation Is Initialization). It is really handy and clean when you have something you need to make sure that an object have some cleanup performed. I have used it from making sure files close to implementing a basic stdout printer that does nested indenting as needed. It's a really nice feature.

The other feature, conditional expressions, I am kind of surprised that I like so much. While it should only be used in situations where the code is still clear to read and it fits on a single line, when it does meet those requirements it is really handy. It's nice to have a clearly visible statement that shows the possible two values when using the conditional expression in an assignment statement. I always figured I would think that the statement was just too much syntactic sugar, but I have found myself using it in my __init__ methods when there is a default value that is different from the default arguments of the method.

Anyway, if you have not taken a look at either of the new features (especially 'with'), then you missing out on some cool features that 2.5 gives you.