Using functools.partial to join paths in PythonSat 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
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.
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_PATHwhen we passed that into the call to
- Any arguments passed to
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
with open(etc_filename('passwd'), 'rb') as f: print(len(list(f)), 'lines')
This is how you’d write the same thing in Python 2.4 or earlier,
functools was available:
def etc_filename_old_skool(*args, **kw): return join(BASE_PATH, *args, **kw)
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?
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
- Python and Django support
- Modern dev tools, like git
Ready to find out how we can work together? Email me at firstname.lastname@example.org
Springfield, MO, USA
+1 360 389-2512