Chapter 9: Functions
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
- Function Bodies and Calls. Describe how indentation marks a Python function’s body and how putting the factorial calculation in a function lets code call it for a number.
- Return Values and Execution. Explain what an explicit return does, what a function returns without one, and whether callers must use the returned value.
- Positional argument matching. Explain that Python matches arguments in a function call to parameter names in the function definition by their order, and that a call with the wrong number of arguments raises a TypeError when no defaults cover the missing arguments.
- Default parameter values. Describe how to declare a default in a function definition, when Python uses it, and why parameters with defaults must come after parameters without them.
- Keyword argument matching. Explain that keyword passing associates each argument with the function parameter of the same name, so named arguments need not appear in parameter order.
- Excess positional arguments. Explain how a final parameter prefixed with * receives positional arguments not assigned to other parameters.
- Unmatched keyword arguments. Explain how a final parameter prefixed with ** collects keyword arguments whose names do not match defined parameters.
- Mixed argument passing order. Explain that Python function argument-passing techniques can be combined and state the general order: positional arguments, named arguments, an indefinite positional argument with *, and an indefinite keyword argument with **.
- Mutation versus parameter reassignment. Explain that a function parameter is a new reference to an argument’s object: changing a mutable object can be visible outside the function, but reassigning the parameter is not. Trace why the example changes y but not x or z.
- Persistent mutable default objects. Explain why a function that appends to a default list can produce a different result when called again without an explicit list.
- Local variables in functions. Explain that parameters and variables created by assignment inside a function are local to that function call, so assigning to them does not change same-named variables outside the function.
- Global and nonlocal assignment targets. Distinguish how global targets a top-level variable and nonlocal targets a previously bound variable in the closest enclosing scope when assigning from within a function.
- Outside variable reads and assignments. Explain why a function can read a name from an enclosing or global scope without a declaration, but must declare it nonlocal or global to assign to that outside variable; describe the author’s preference for explicit global declarations and limited use of globals.
- Functions assigned to variables. Explain that a function can be assigned to a variable and called through that variable, which calls whichever function the variable currently refers to.
- Dictionary-based function selection. Describe how storing functions in a dictionary lets a string key select which function to call, providing an alternative to a switch structure.
- Inline anonymous functions. Explain that lambda expressions define small anonymous functions inline, such as a function passed as a list sort key or stored as a dictionary value instead of being defined separately.
- Generator values and state. Explain how calling a generator function produces an iterator and how successive yields produce values while preserving local variables.
- Decorator syntax and effect. Describe how to define and apply a decorator, and explain how @ syntax relates to explicit function wrapping in the section’s example.
- Focused word-counting functions. Describe how to refactor the earlier text-file cleaning and word-counting code so most of its logic lives in functions, with each function doing one thing and avoiding side effects that carry over outside it.
- Function-focused AI refactoring prompt. Describe how to ask an AI tool to improve the structure of existing working code: provide that code to the current session and clearly request a rewrite using functions that do most of the data processing.
What it builds on
- Statements, blocks, and indentation (chapter 8) · Indentation marks blocks of Python code.
- What is a dictionary? (chapter 7) · Dictionaries and their keys
- The for loop (chapter 8) · The for loop provides a familiar way to use an iterator.
- The while loop (chapter 8) · The while loop provides the section’s example of limiting iterations.
- List membership with the in operator (chapter 5) · Earlier use of in introduces membership checks.
- Preprocessing text (chapter 6) · Earlier text-preprocessing work supplies code to revisit.
- Word counting (chapter 7) · Earlier word-counting work supplies code to revisit.
- Solving the problem with AI-generated code (chapter 8) · The earlier exercise asked AI tools to refine existing code at the line level.
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.
- Function Bodies and Calls: Describe how indentation marks a Python function’s body and how putting the factorial calculation in a function lets code call it for a number.
Why it matters: This is the basic connection between defining a function and using it. - Return Values and Execution: Explain what an explicit return does, what a function returns without one, and whether callers must use the returned value.
Why it matters: It explains both what a call produces and why a caller may or may not keep that result. - Docstrings and Comments: Distinguish a function’s docstring from comments, including where the docstring goes, what it describes, and how to access it.
Why it matters: It helps readers explain what a function does without confusing its public description with notes about its implementation.
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.
- Positional argument matching: Explain that Python matches arguments in a function call to parameter names in the function definition by their order, and that a call with the wrong number of arguments raises a TypeError when no defaults cover the missing arguments.
Why it matters: Argument order determines which value each parameter receives. - Default parameter values: Describe how to declare a default in a function definition, when Python uses it, and why parameters with defaults must come after parameters without them.
Why it matters: Defaults let a function accept shorter calls while keeping positional argument matching clear.
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.
- Keyword argument matching: Explain that keyword passing associates each argument with the function parameter of the same name, so named arguments need not appear in parameter order.
Why it matters: You can read a call correctly even when its named arguments are out of order. - Selective optional argument use: Explain how keyword passing works with defaults to let callers select only the options they want to change, as in requesting file size and modification date but not creation date.
Why it matters: Named options make calls to functions with many optional settings easier to express.
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.
- Excess positional arguments: Explain how a final parameter prefixed with * receives positional arguments not assigned to other parameters.
Why it matters: This lets a function accept an indefinite number of positional arguments. - Unmatched keyword arguments: Explain how a final parameter prefixed with ** collects keyword arguments whose names do not match defined parameters.
Why it matters: This lets a function accept named arguments that are not individually declared as parameters.
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.
- Mixed argument passing order: Explain that Python function argument-passing techniques can be combined and state the general order: positional arguments, named arguments, an indefinite positional argument with *, and an indefinite keyword argument with **.
Why it matters: The order helps you describe mixed argument passing without confusing the different kinds of arguments.
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.
- Mutation versus parameter reassignment: Explain that a function parameter is a new reference to an argument’s object: changing a mutable object can be visible outside the function, but reassigning the parameter is not. Trace why the example changes y but not x or z.
Why it matters: It helps you predict whether changes made inside a function will be visible to its caller.
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.
- Persistent mutable default objects: Explain why a function that appends to a default list can produce a different result when called again without an explicit list.
Why it matters: It explains why a function can unexpectedly carry results from one call into the next. - Side effects on passed objects: Describe why changing a mutable object passed as a parameter can have an effect outside the function.
Why it matters: It helps you recognize when a function call may change data the caller still uses.
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.
- Local variables in functions: Explain that parameters and variables created by assignment inside a function are local to that function call, so assigning to them does not change same-named variables outside the function.
Why it matters: It helps you predict whether a function’s assignment will change a variable outside it. - Global and nonlocal assignment targets: Distinguish how global targets a top-level variable and nonlocal targets a previously bound variable in the closest enclosing scope when assigning from within a function.
Why it matters: Choosing the declaration determines which outside variable an assignment changes. - Outside variable reads and assignments: Explain why a function can read a name from an enclosing or global scope without a declaration, but must declare it nonlocal or global to assign to that outside variable; describe the author’s preference for explicit global declarations and limited use of globals.
Why it matters: The read-versus-assignment distinction prevents surprising assumptions about which variable a function 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.
- Functions assigned to variables: Explain that a function can be assigned to a variable and called through that variable, which calls whichever function the variable currently refers to.
Why it matters: It lets the same call site use different functions without changing the call. - Dictionary-based function selection: Describe how storing functions in a dictionary lets a string key select which function to call, providing an alternative to a switch structure.
Why it matters: A dictionary can organize several possible actions around the string that selects each one.
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.
- Inline anonymous functions: Explain that lambda expressions define small anonymous functions inline, such as a function passed as a list sort key or stored as a dictionary value instead of being defined separately.
Why it matters: You can recognize why a short function might appear directly where it is needed. - Automatic expression return: State how a lambda expression produces its return value without a return statement.
Why it matters: This explains how a lambda supplies a result despite having no return statement.
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.
- Generator values and state: Explain how calling a generator function produces an iterator and how successive yields produce values while preserving local variables.
Why it matters: This explains how a for loop can obtain successive values without the function starting over each time. - Generator stopping conditions: Describe when a generator stops producing values and why a generator used in a loop may need a condition that halts it.
Why it matters: A loop consuming a generator needs a way to finish. - Yield from delegation: Explain what yield from does in relation to a subgenerator and why the section presents it as useful for refactoring.
Why it matters: It lets a generator hand off part of its value-producing work to another generator. - Generator series membership: State how the in operator can be used with a generator function’s output.
Why it matters: Generator-produced values can be checked for membership as well as used in a for loop.
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.
- Functions as wrappers: Explain how a function can accept another function, wrap it with related behavior, and return a function to use in its place.
Why it matters: This is the function-wrapping process that decorator syntax makes easier to write. - Decorator syntax and effect: Describe how to define and apply a decorator, and explain how @ syntax relates to explicit function wrapping in the section’s example.
Why it matters: Decorator syntax makes function wrapping cleaner while preserving how you call the function.
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.
- Focused word-counting functions: Describe how to refactor the earlier text-file cleaning and word-counting code so most of its logic lives in functions, with each function doing one thing and avoiding side effects that carry over outside it.
Why it matters: These constraints give you a way to judge whether your refactoring has produced useful 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.
- Function-focused AI refactoring prompt: Describe how to ask an AI tool to improve the structure of existing working code: provide that code to the current session and clearly request a rewrite using functions that do most of the data processing.
Why it matters: A specific prompt tells the AI tool what structural change to make to the code you provide.
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
- Python scoping rules and namespaces (chapter 10) · The fuller treatment of Python scoping rules builds on the distinction between local and outside variables.
- Sorting (chapter 21) · Lambda expressions used as short functions.
- Using @property for more flexible instance variables (chapter 15) · Recognizing @ syntax helps when encountering @property.
- Creating a module (chapter 10) · The functions created at the end of chapter 9 are the material to package.
Concepts explained in this chapter
- functionA function is a callable program unit that performs an operation, may accept inputs, and can produce a result or side effect.
- global variableA global variable is a name bound at module or program level that can be accessed and modified by functions that explicitly declare it global.
- return valueA return value is the result supplied by a function to its caller when the function completes execution.
- commentA comment is source text ignored during program execution that explains or annotates code for human readers.
- docstringA docstring is a string literal attached to a module, function, method, or class to provide documentation accessible to development tools and runtime introspection.
- default valueA default parameter value is a value assigned to a function parameter and used when the corresponding argument is omitted from a call.
- refactoringRefactoring is the systematic restructuring of existing source code to improve its internal qualities without changing its externally observable behavior.
- scopeA scope is a region of program execution in which name bindings can be resolved and accessed according to Python's lookup rules.
- NoneNone is Python's singleton object representing the absence of a value.
- indentationWhitespace placed at the beginning of source lines to visually align code and, in Python, delimit nested blocks.
- positional argumentA positional parameter is a function argument identified by its ordinal position in the argument list.
- lambdaA lambda expression is an anonymous function expression that evaluates to a function value and returns the value of its expression body.
- command-line argumentA command-line argument is a value supplied to a program when it is invoked through a command-line interface.
- immutable collectionImmutability is the property of an object or data structure that prevents its contents from being changed after creation.
- referenceAn object reference is a binding or pointer-like association through which a name identifies a runtime object.
- MapA map is an associative data structure that stores key-value pairs and supports retrieving values by key.
- bindingA binding is an association between an identifier and an object in a namespace.
- promptA prompt is an instruction or input supplied to an AI system to elicit a particular response or generated output.
- data cleaningData cleaning is the process of identifying and correcting, removing, or otherwise handling irregular, invalid, missing, or inconvenient data values and formats.
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.
