4 Advanced Tricks With Python Functions You Might Not Know

From forcing keyword arguments to anonymous functions

Erik van Baaren
Nov 20, 2020 · 3 min read
Time lapse of a road by the forest
Time lapse of a road by the forest
Photo by Jake Leonard on Unsplash.

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.

For more cool Python tricks, visit the author's website: Python.land

1. Forced Keyword Arguments

Keyword arguments have a number of 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. by 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 ** prefix:

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:

Inside 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.

With @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
2

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
>>> add_one(3)
4

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 map:

>>> numbers = [1, 2, 3, 4]
>>> times_two = map(lambda x: x * 2, numbers)
>>> list(times_two)
[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.

Conclusion

That’s it! I hope you learned something new today. Feel free to leave a comment if you have something to add to this list.

Originally published on Python Land’s guide on functions.

Better Programming

Advice for programmers.

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Thanks to Zack Shapiro

Erik van Baaren

Written by

Software developer by day, writer at night. Webmaster at https://python.land

Better Programming

Advice for programmers.

Erik van Baaren

Written by

Software developer by day, writer at night. Webmaster at https://python.land

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store