In a recent project, I’ve experimented with “partial()” to simplify object creation. Partially applying a function, similar to currying, is a technique in which a new function is synthesized by pre-filling the arguments to an existing function. Python’s implementation of partial is available in two separate modules: functools and functional. It’s best understood with an example, albeit contrived:

Instead of defining a factory class and a creation method on that class, I define creation function which accepts the dependencies as parameters, partially apply them and use the new function as my “creation function.” I prefer the second example, I enjoyed writing it, but both examples perform the same function with essentially the same number of lines and ultimately my preference is as basic as preferring a functional style of programming over object oriented.

So I’m wondering, how do other developers use currying or partial application in their projects?

Share and Enjoy:
  • del.icio.us
  • Reddit
  • Facebook
  • Identi.ca
  • TwitThis

Related posts:

  1. On Erlang.
  2. Dependency Injection
  3. Language Limbo
  4. Java, .NET and RoR

3 Comments on 'Experimenting with partial application in Python' read them below or add one

Subscribe to comments with RSS or TrackBack to 'Experimenting with partial application in Python'.

  1. Is this the same thing as “currying”?

    Ben Scheirman - 7 Dec 10 at 11:16 am

  2. Technically no, but my functional programming-fu is a weak. I’ll leave it up to the experts: http://srfi.schemers.org/srfi-26/mail-archive/msg00015.html

    Sheheryar Sewani - 7 Dec 10 at 11:55 am

  3. The other day I used partial functions in JavaScript to implement generators.

    Here’s a simple example of a loop that never ends:


    function neverEndingLoop() {
    var i = 0;
    return {
    next: function() {
    return i++;
    }
    };
    }

    var loop = neverEndingLoop();
    // Dump 100 numbers from the "iterator"
    for(var n=0; n < 100; n++) {
    console.log(loop.next());
    }

    Luke Venediger - 9 Jan 11 at 5:06 am

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Search