3 Function Definitions

Function definitions are a way to decompose a large problem into smaller problems. Mathematicians have been doing this for centuries. It’s a way to package our Python programming into intellectually manageable chunks.

We’ll look at a number of function definition techniques in these recipes. This will include ways to handle flexible parameters and ways to organize the parameters based on some higher-level design principles.

We’ll also look at the typing module and how we can create more formal type hints for our functions. Using type hints will prepare our code so we can use tools like mypy to confirm the data types are used properly throughout a program. Type hints aren’t required, but they often identify potential inconsistencies, ...

Get Modern Python Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.