liveBook · Manning

Mutation

As explained in Fast Python: High performance techniques for large datasets

Mutation is a change to an existing value, object, data structure, state, or representation.

Mutation changes an existing array in place. In NumPy, `x *= 2` updates the array, while `x = x * 2` creates a result array and rebinds `x`. The in-place operation avoids temporarily holding both arrays and reduces allocation and garbage-collection work. section 4.2.3

Doubling an image array

Compare `brighter3_arr = brighter3_arr * 2` with `brighter3_arr *= 2`: both double the values, but the latter mutates the existing array. section 4.2.3

brighter3_arr *= 2

View the in-place multiplication expression used to double an array. section 4.2.3

How Fast Python: High performance techniques for large datasets covers it

How deep it goes
Compares new-array and in-place multiplication, explaining temporary memory and garbage-collection costs.
Examples
Uses doubling an image array to contrast reassignment with in-place multiplication.
What it assumes
Assumes familiarity with NumPy array operations rather than teaching them from zero.
What you can do after
Apply in-place multiplication with NumPy to avoid creating a separate result array.
How it connects
Connects mutation to NumPy and array programming.

Also explained in

Compare how 12 books explain mutation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast Python: High performance techniques for large datasets. The section links point to the parts of the book it draws on.