liveBook · Manning

State

As explained in Get Programming: Learn to code with Python

State is information or a configuration describing the condition of a system, entity, or computation at a particular time and potentially influencing subsequent behavior or decisions.

State is what an object contains right now. A grocery list, for example, has a different state after you add bread. In Python, a mutable object can change its contents without becoming a new object. Lists and dictionaries are mutable; strings, numbers, Booleans, and tuples are not. section 24.2 section 24.3 section 25.1

Change the same list

Start with `a = ["milk", "eggs"]`, then run `a.append("bread")`. The list’s state is now `["milk", "eggs", "bread"]`: `a` still refers to the same object. By contrast, assigning `a = ["milk", "eggs", "bread"]` binds `a` to a new list instead. Both leave you with the same visible items, but they do not do the same thing to the original object. section 24.2

Figure 24.3. On the left, you have a grocery list at a certain memory location. On the right, you add another item to your grocery list, and the object at the same memory location is directly modified.

The grocery-list diagram shows an item being added while the list remains at the same memory location. section 24.2

A list’s current state affects the next operation. For example, after `colors = ["red", "blue", "yellow"]`, the command `colors[0] = "orange"` replaces the first item. The list is now `["orange", "blue", "yellow"]`; later operations work with that changed list. You can also add, remove, reorder, or sort list items without reassigning the list variable. section 25.6 section 26.0

One important surprise: two variable names can refer to the same mutable object. If the object changes through either name, both names lead to its new state. To keep independent lists, make a copy—for example, `painters = artists.copy()`. Changing `painters` then leaves `artists` unchanged. Likewise, `sorted(ages)` produces a sorted copy, whereas `ages.sort()` changes the existing list. section 28.0 section 28.2

Quick check: if `L = [1, 2, 3]` and then `L[0] = 4`, what does `L` contain? It contains `[4, 2, 3]`. For a changing menu or grocery list, ask whether you want to update the existing collection or preserve its earlier state in a separate copy. section 25.6 section 25.7 section 28.2

How Get Programming: Learn to code with Python covers it

How deep it goes
Explains in-place change, rebinding, aliases, and copies, including their effect on object identity.
Examples
Uses grocery lists, indexed color replacements, artist-list copies, and a changing menu.
What it assumes
Builds on variable binding and previously used tuples while defining mutable objects directly.
What you can do after
Diagnose/debug with it when a list changes unexpectedly, and choose an in-place update or an independent copy.
How it connects
Connects state to mutability, variable binding, lists, dictionaries, tuples, aliasing, and copying.

Also explained in

Compare how 39 books explain state →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.