chapter six
6 Defining user-friendly functions
This chapter covers
- Setting proper default arguments for a function
- Setting and using the return value for a function
- Applying type hints to the parameters and the return value
- Defining functions with a variable number of positional and keyword arguments
- Creating proper docstrings for a function
In previous chapters, you’ve seen several examples of functions. Broadly speaking, no matter what our applications are about, we define a wide range of functions to perform various operations, such as calculations and formatting strings. When you work in a team environment, you often need to define functions that allow your team members to reuse your code. When you publish a Python package, the package should include well-defined functions for the users, just like how we use the built-in functions provided by the standard Python library. Thus, it’s an essential skill for you to define functions that are user-friendly — even if you work on your own, you don't want the functions are hard to use.