7 Using functions beyond the basics

 

This chapter covers

  • Using lambda functions for a small job
  • Working with higher-order functions
  • Creating and using decorators
  • Using generators to obtain data
  • Creating partial functions

You may have realized that in every project, the greatest amount of time that you spend in development is devoted to writing functions. In chapter 6, we focused on the fundamentals of writing and using functions. After covering these topics, you’re able to write user-friendly functions to serve your work needs. Python knows the integral role of functions in any project; thus, it has advanced features that you can take advantage of to make functions serve your work better.

In this chapter, you’ll learn about more-advanced function topics. You’ll find that the pertinent concepts may sound advanced, but the pragmatic techniques are not hard to apply to your daily coding work.

7.1 How do I use lambda functions for small jobs?

When we define functions, we use the def keyword and then give the name to the function, which serves as the identifier for the function. Although the terminology isn’t common, we can refer to these functions as named functions because they have associated identifiers.

7.1.1 Creating a lambda function

7.1.2 Using lambdas to perform a small one-time job

7.1.3 Avoiding pitfalls when using lambda functions

7.1.4 Discussion

7.1.5 Challenge

7.2 What are the implications of functions as objects?

7.2.1 Storing functions in a data container

7.2.2 Sending functions as arguments to higher-order functions

7.2.3 Using functions as a return value

7.2.4 Discussion

7.2.5 Challenge

7.3 How do I check functions’ performance with decorators?

7.3.1 Decorating a function to show its performance