Using functools.partial to join paths in Python

Sat 23 March 2013 by Matthew Scott

While writing a new test module earlier today, I had to repeatedly load files from a directory of test data.

The Functional Programming with Python given at US PyCon 2013 by Mike Müller of Python Academy inspired me to brush up on the functools module in the Python standard library, which provides some useful functional programming tools.

In this case, I used functools.partial to create a function that would take a filename and return a full pathname to that file within my test data. A partial function wraps another function (or callable), along with arguments you set when you create the partial.

Let’s import os.path.join and functools.partial, and set up a base path to work from:

In [1]:
from functools import partial
from os.path import join

BASE_PATH = '/etc'

Now create a partial function etc_filename that, when called, will call join with the following arguments:

  • The value of BASE_PATH when we passed that into the call to partial
  • Any arguments passed to etc_filename
In [2]:
BASE_PATH = '/etc'
etc_filename = partial(join, BASE_PATH)

I’ll use it here in the context of finding out how many lines are in my /etc/passwd file:

In [3]:
with open(etc_filename('passwd'), 'rb') as f:
    print(len(list(f)), 'lines')
83 lines

This is how you’d write the same thing in Python 2.4 or earlier, before functools was available:

In [4]:
def etc_filename_old_skool(*args, **kw):
    return join(BASE_PATH, *args, **kw)
In [5]:
etc_filename('hosts')
Out[5]:
'/etc/hosts'
In [6]:
etc_filename_old_skool('hosts')
Out[6]:
'/etc/hosts'

As long as the reader of your code understands what partial functions (and other functional programming constructs) I think it’s useful to use them when you notice that you’re applying a functional pattern. They help your code convey more meaning.

Functional programming is getting more and more focus these days, and it’s worthwhile for those with a procedural/OO background to get used to “thinking in functions”. It’s here to stay!

What functional programming idioms do you find yourself using often?


Comments

Do you like podcasts? Check out Fanscribed. It helps podcasts use crowdsourcing to create high-quality transcripts.

Fanscribed is created and maintained by Elevencraft Inc.


I'm Matthew Scott, and my company Elevencraft Inc. exists to help you wisely use tech to solve problems:

  • "Full stack" development
  • Pair-programming
  • Python and Django support
  • Modern dev tools, like git

Ready to find out how we can work together? Email me at matthew@11craft.com


Elevencraft Inc.
Springfield, MO, USA
matthew@11craft.com
+1 360 389-2512