Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Using functools.Placeholder

00:00 In the previous lesson, I showed you some small changes to numbers in Python. In this lesson, I’ll be discussing the functools partial() function, and the new placeholder mechanism.

00:10 Python allows you to create an alias to a function, specifying some keyword arguments. When you call the alias, the function behind it gets called, including those keywords you specified.

00:21 You create such an alias using the partial() function in the functools library. One limitation of this is you can only bind keyword arguments, not positional ones.

00:32 And if I pause for a beat here, you’ll probably assume that’s changed in Python 3.14. There’s a new object called a Placeholder, which holds a spot in the call to partial, allowing you to fill positional arguments.

00:45 Let’s go look at how these work. Before I show you placeholders, let me explain partials on their own. I’ll use an example straight out of the documentation.

00:55 Consider the int() constructor, which you can use to convert strings into integers. When doing so, the base keyword argument allows you to specify the base of the number in the string.

01:07 (101, base=two) is decimal five. Using a partial(), you can create an alias for this, which automatically populates the base= 2 keyword part.

01:25 The first argument to partial() is a reference to the callable being aliased, which in this case is int. All subsequent arguments are keywords, which get passed to the alias function when the alias gets invoked.

01:40 When I called basetwo, the alias function calls the referenced callable, passing the argument to basetwo, as well as the keyword arguments specified in partial().

01:51 It turns out that the base keyword argument is also the second argument to int, meaning I can call it positionally.

02:00 See, I didn’t have to specify base=, but before Python 3.14, I couldn’t have used partial() this way. The arguments to partial() could only be the keyword arguments to the callable.

02:12 Whatever positional arguments you gave to the alias get passed along, but there’s no way in the call to partial() to say, reserve a spot for an argument, until now, of course.

02:29 The placeholder object is exactly that, a reservation spot. I’ve redefined our basetwo alias telling it to hold the spot for our string argument, then pass int as a positional argument, which in this case is the base parameter.

02:46 And there you go. Our new alias works without using keyword arguments. If you’re asking yourself, where does all this get used? Let me give you an example.

02:55 The Playwright library is a headless browser interface that you can use to programmatically access web pages. Inside of it, you can register a callback function so that when certain kinds of content is found, your code gets run.

03:08 I’ve got code that does this for each image found on a page. What Playwright’s API doesn’t have is a way to pass arguments along to your callback. It only takes a function reference. By using a partial(), with or without a placeholder, you can construct a new alias with your arguments and use that alias as the callback. Problem solved.

03:30 Next up, f-string’s new cousin, the t-string.

Become a Member to join the conversation.