Python 2.6 was release yesterday. I’ve had a chance to run most of my code through this latest version and haven’t run in to any issues. Looking over the features list I did notice a couple things you might be interested to know.
- json module added to standard library
- multiprocessing module was also added
- with keyword – cleans up some of those try/finally blocks
- as keyword – more important than people realize
- print as a method (future imports, standard in 3.0)
- user defined maps can be used with ** for passing in keyword args
All this and more can be found on the What’s New in Python 2.6 page. But if you’re too lazy to read that, at least take the time to look at this example of using as, this was a much needed new keyword in Python and really cleans up code.
I’ve seen the following a lot, as mentioned on the python.org site. People think this is catching either KeyError or ValueError but they are wrong. It is only catching KeyError and storing that exception object in the ValueError variable.
try:
print my_dict['nokey']
except KeyError, ValueError:
# handle the exception(s)
Those exceptions should be in a tuple, then a comma for the variable to store the exception object in. The new method uses the as keyword and removes that ambiguous comma and makes for a much cleaner exception block when reading.
try:
print my_dict['nokey']
except (KeyError, ValueError) as e:
print e
Leave a Reply
You must be logged in to post a comment.