liveBook · Manning

Instance variable

As explained in Software Design for Python Programmers

An instance variable is state associated with an individual object instance.

An instance variable is state associated with an individual object. A class defines the possible structure, and each object receives its own attribute values at runtime. The current combination of those values characterizes the object’s state; changing one or more values produces a state transition. Methods operate on instance variables and help determine object behavior. section 1.8

Instance variables support several design decisions:

  • Encapsulate object state inside a class, exposing public attributes through its interface while restricting private attributes to class methods. section 1.8
  • Represent a class’s owned relationships: Catalogue has private _booklist containing zero or more Book objects, whereas method parameters can create only transient dependencies. section 4.3.1
  • Translate requirements into object state: Catalogue stores _booklist, Book stores _attributes, Attributes stores _dictionary, and Form stores individual input values. section 3.8.1
  • Record the values that drive explicit state machines, such as a ticket machine’s count, card_inserted, and card_validity variables. section 13.1

Private state is not automatically immutable

An Employee object can store _employee_id, _name, and _birthdate as private instance variables. However, assigning the caller’s Date object directly to _birthdate lets the caller change the employee’s state; returning that same object through birthdate creates another mutation path. Copy the Date object when constructing Employee and when returning birthdate to protect the embedded state. section 5.7

Table 3.2 The initial classes of the application

Class

State

Instance variables

Catalogue

List of books

_booklist (list of Book objects)

Book

Book attributes

_attributes (the book’s attributes)

Attributes

Attribute values

_dictionary (dictionary of key–value pairs)

Form

Input values

Individual instance variables for the book attributes

Use the class-state table to compare how instance variables represent a catalogue, book, attribute collection, and form. section 3.8.1

Figure 4.6 Diagrams that show more design detail. Class Catalogue depends on class Book via its private booklist instance variable. The multiplicity indicators show that a single (1) Catalogue object can contain zero or more (*) Book objects.
A close-up of a book list

Description automatically generated

Use the UML figure to see a private instance variable expressing Catalogue’s dependency on Book and its zero-or-more multiplicity. section 4.3.1

How Software Design for Python Programmers covers it

How deep it goes
Explains state, visibility, dependencies, multiplicity, and defensive copying, but stops short of general language-level attribute mechanics.
Examples
Uses catalogue objects, Employee birthdates, and an automatic ticket machine to demonstrate storage, protection, and state changes.
What it assumes
Assumes basic familiarity with classes and objects while reviewing encapsulation, abstraction, inheritance, and polymorphism.
What you can do after
Evaluate/understand how object state, ownership, visibility, and mutation paths affect class design.
How it connects
Connects instance variables to classes, objects, methods, encapsulation, inheritance, dependency relationships, and state transitions.

Also explained in

Compare how 4 books explain instance variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Design for Python Programmers. The section links point to the parts of the book it draws on.