liveBook · Manning

Chapter 9: Functions

What you will learn in chapter 9 of The Quick Python Book, Fourth Edition

This introduction previews the chapter’s function topics and states that readers are expected to know basic function concepts from another language.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction previews the chapter’s function topics and states that readers are expected to know basic function concepts from another language.

9.1 Basic function definitions

This section explains how an indented function body becomes callable, what happens to a function’s return value, and how docstrings document a function.

Key terms: function definition, function body, indentation, fact, return, None, return value, procedure, docstring, comments

9.2 Function parameter options

Introduces Python’s flexible function parameter definitions and previews three options explained in the following subsections.

9.2.1 Positional parameters

This subsection explains how Python matches positional arguments to function parameters and how default values allow some arguments to be omitted.

Key terms: positional parameters, arguments, Type-Error exception, default values, parameter list, positional basis

9.2.2 Passing arguments by parameter name

This subsection explains how to pass arguments by parameter name and why that is useful for functions with many optional arguments.

Key terms: keyword passing, parameter, keyword argument passing, default argument

9.2.3 Variable numbers of arguments

This subsection explains how function parameters collect excess positional arguments and unmatched keyword arguments.

Key terms: positional arguments, *args, tuple, keyword-passed arguments, **kwargs, dictionary

9.2.4 Mixing argument-passing techniques

This subsection gives the general ordering rule for mixing Python function argument-passing techniques.

Key terms: positional arguments, named arguments, indefinite positional argument, indefinite keyword argument

9.3 Mutable objects as arguments

This section explains how passing an object reference lets a function change a caller-visible mutable object without letting it reassign the caller’s variable.

Key terms: object reference, parameter, mutable object, immutable object

9.3.1 Mutable objects as default values

This section explains why mutating a mutable default parameter can make later function calls produce unexpected results.

Key terms: mutable object, default parameter value, mutable objects, parameter values, side effect

9.4 Local, nonlocal, and global variables

This section explains how local, global, and nonlocal declarations determine which variables a function reads or changes.

Key terms: local variables, parameter list, assignment, global, nonlocal, top-level variable, closest enclosing scope, local function scope, enclosing scopes, global scope

9.5 Assigning functions to variables

This section shows that Python functions can be stored in variables and collections, allowing code to select which function to call.

Key terms: functions, variables, lists, tuples, dictionaries, strings used as keys, switch structure

9.6 lambda expressions

This section introduces lambda expressions as short, anonymous functions defined inline, often for use where another function expects a function.

Key terms: lambda expressions, anonymous, inline, key function, return statement

9.7 Generator functions

This section explains how generator functions produce iterator values, when they stop, and how yield from delegates to another generator.

Key terms: generator function, generator object, iterator, yield, local variables, empty return statement, while loop, endless loop, yield from, subgenerator

9.8 Decorators

Explains how Python decorators use first-class functions to wrap another function with concise @ syntax.

Key terms: first-class objects, wrap, decorator, syntactic sugar, @decorate, wrapped function

9.9 Useful functions

This section sets a refactoring exercise: organize text-file cleaning and word counting into focused functions.

Key terms: refactor, functions, parameters, side effects

9.9.1 Solving the problem with AI-generated code

This subsection explains how to prompt an AI tool to refactor existing working code into functions rather than make line-level changes.

Key terms: AI generators, prompt, existing, working code, functions, data processing

9.9.2 Solutions and discussion

9.10 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 8: Control flow Chapter 10: Modules and scoping rules →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.