Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

Exciting stuff in Python 2.5

Python 2.5 alpha 1 is out, and as usual the What’s New in Python 2.5 document provides a pleasant overview of the new features. There are some real treats in there. While I’m hoping that the syntax for conditional expressions will grow on me, I’m looking forward to Partial function application becoming a common Python idiom. Relative imports are going to make Django applications a lot easier to redistribute, and I can’t wait to see all the crazy hacks that result from the introduction of coroutines.

I’ve seen David Heinemeier Hansson speak a few times, and he often uses ActiveRecord’s transaction support to illustrate the elegance that well-written Ruby has to offer:

Account.transaction(account1, account2) do
  account1.withdraw(100)
  account2.deposit(100)
end

Here a Ruby block is being used as syntax for a database transaction, guaranteeing that some code will be run before and after the withdraw and deposit lines. The new with statement makes something similar possible with an imaginary Python ORM:

with Account.transaction(account1, account2):
    account1.withdraw(100)
    account2.deposit(100)

The highlight of the release might well be the modules that have been added to the standard library—tucked away in section 13 of the document. ctypes, ElementTree, a new hashlib with support for SHA-224 through 512 and sqlite3 (a renamed pysqlite) are all included. ctypes is particularly interesting as it lets you call functions in compiled DLLs and shared libraries without having to compile a Python wrapper.

The final Python 2.5 release appears to be scheduled for June-July this year, which should give Apple ample time to incorporate it in to OS X Leopard.

This is Exciting stuff in Python 2.5 by Simon Willison, posted on 6th April 2006.

Tagged , , ,

View blog reactions

Next: Speaking gigs

Previous: Naked day

8 comments

  1. It's too bad the partial-application syntax is so verbose. One of the joys of programming in languages such as Haskell is that partial application is virtually free. Frequent use is thus encouraged (or at least not penalized) and combines with inexpensive function composition to form a thing of remarkable power and beauty.

    Cheers
    Tom

    Tom Moertel - 6th April 2006 23:53 - #

  2. Can someone enlighten me what those partial things for? Taking an example from "What's new":

    import functional
    
    def log (message, subsystem):
        print '%s: %s' % (subsystem, message)
    
    server_log = functional.partial(log, subsystem='server')
    server_log('Unable to open socket')

    How is this different from:

    def log (message, subsystem):
        print '%s: %s' % (subsystem, message)
    
    def server_log(message):
         log(message, subsystem='server')
    
    server_log('Unable to open socket')

    Huh?

    Ivan Sagalaev - 7th April 2006 05:56 - #

  3. Ivan, imagine the code that specifies subsystem='server' (and creates the partial) is in a piece of code encapsulated from the line that uses server_log.

    Jeremy Dunck - 7th April 2006 06:18 - #

  4. "How is this different from"

    Only slightly: the partial variant binds to the callable object (log, in your case) rather than the name, but is otherwise equivalent. Binding to objects is of course easy also with the spelled-out function form:

    def log (message, subsystem):
        print '%s: %s' % (subsystem, message)
    
    def server_log(message, log=log):
        log(message, subsystem='server')
    
    server_log('Unable to open socket')
    

    but that will misbehave if you call server_log with two arguments.

    But all in all, it's definitely a "if you think you need it, you probably won't" addition.

    "imagine the code that specifies subsystem='server' (and creates the partial) is in a piece of code encapsulated from the line that uses server_log"

    And? Functions are objects in Python, so passing around Ivan's server_log is no harder than passing around the partial.

    Fredrik - 7th April 2006 08:18 - #

  5. This may well help with Django application mobility. Adrian Holovaty has just finished a reverse URL lookup implementation that looks really neat. http://groups.google.com/group/django-developers/b rowse_frm/thread/a5d12bc4fb073f24/# Cheers, Tone

    Tony - 7th April 2006 09:06 - #

  6. I was surprised to see that the full SQLite library wasn't included though. It's so small and it would be a real boon to not have to have it as a prerequisite.

    -Dom

    Dominic Mitchell - 7th April 2006 19:16 - #

  7. Dom: I got the impression that the full library is included in the binary packages - you only have to install in separately if you are compiling from source.

    Simon Willison - 7th April 2006 20:40 - #

  8. It looks like we 'might' get the path module too (http://www.jorendorff.com/articles/python/path/). I've been using it for a while and although it seems 'trivial' it really streamlines working with files..

    Tim Parkin - 22nd April 2006 16:26 - #

Comments are closed.

Previously hosted at http://simon.incutio.com/archive/2006/04/06/python25

A django site