liveBook · Manning

Side effect

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

A side effect is an observable change or external action caused by a function in addition to producing its return value.

A side effect is an observable change or external action produced by a function in addition to its return value. Functions may print, modify data, send information over a network, or change system state while performing their main calculation. section 3.1.3 section 3.2.7

Manage side effects deliberately:

  • Make side effects intentional; unexpected global-variable changes can affect unrelated functionality. section 3.2.7
  • Release resources promptly by closing files and database connections when they are no longer needed. section 3.2.7
  • Watch for mutations through function arguments: a function can alter an external list when both names reference the same object. section 3.2.7

Mutating input versus working on a copy

A total(values, new_value) function that appends directly to values returns 10 but changes [1, 2, 3] into [1, 2, 3, 4]. A better_total function copies the list first, returns the same total, and leaves the original list unchanged. section 3.2.7

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Explains intentional effects, resource cleanup, reference-based mutation, copying, and idempotence through worked examples.
Examples
Uses printing, network communication, resource handling, and total functions that mutate or copy input lists.
What it assumes
Assumes familiarity with functions, parameters, return values, lists, and objects while clarifying reference-based argument behavior.
What you can do after
Diagnose/debug with it by identifying unintended mutations and rewriting functions to copy inputs or release resources.
How it connects
Connects API output to return values, exceptions, global variables, resource management, and idempotence.

Also explained in

Compare how 24 books explain side effect →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.