4 Advanced Tricks With Python Functions You Might Not Know
Do you know how to force keyword arguments, create a function decorator, create anonymous functions, or unpack an array or dictionary into a function’s arguments? Here are four advanced tricks regarding Python functions.
1. Forced Keyword Arguments
Keyword arguments have several advantages:
- You’re not forced into a particular order in which you supply your arguments. The name matters — not the position.
- Keyword arguments provide clarity. Without looking up the function itself, you can often guess what the argument is used for by looking at the names.
That’s nice, but you probably already knew these things. What you might not know is that you can also force keyword arguments. The details are described in PEP 3202, but it comes down to using an asterisk before the arguments you want to force as keyword arguments. Or, before everything, forcing all arguments to be keyword arguments:
2. Using * and ** for Function Argument Unpacking
Some functions require a long list of arguments. Although this should be avoided altogether (e.g., using data classes), it’s not always up to you. In such cases, the second-best option is to create a dictionary with all the named arguments and pass that to the function instead. It will generally make your code more readable.
You can unpack a dictionary for use with named keywords by using the
Similarly, we can use a single
* to unpack an array and feed its content as positional arguments to a function:
3. Decorating Your Functions
Decorators are wrappers around a function that modify the behavior of the function in a certain way. There are many use cases for decorators, and you may have used them before when working with frameworks like Flask.
Let’s create our own decorator. It’s simpler than you might expect and might come in handy someday:
print_argument, we define a wrapper function. This function prints the argument and the name of the called function. Next, it executes the actual function and returns its result as if the function was called regularly.
@print_argument, we apply our decorator to a function. Perhaps this is unnecessary to say, but this decorator can be reused for other functions too.
The output of our little script will be:
Argument for add_one is 1
4. Anonymous Functions
Sometimes, naming a function is not worth the trouble. An example is when you’re sure the function will only be used once. For such cases, Python offers us anonymous functions (also called lambda functions).
A lambda function can be assigned to a variable, creating a concise way of defining a function:
>>> add_one = lambda x: x + 1
It gets more interesting when you need to use a function as an argument. In such cases, the function is often used only once. As you may know,
map applies a function to all elements of an iterable object. We can use a lambda when calling
>>> numbers = [1, 2, 3, 4]
>>> times_two = map(lambda x: x * 2, numbers)
[2, 4, 6, 8]
In fact, this is a pattern that you’ll see often. When you need to apply a relatively simple operation on each element of an iterable object, using
map() in combination with a lambda function is concise and efficient.
Originally published on Python Land’s deep dive on functions.