Instance Attributes and Shared Class Attributes

An instance attribute supplies one object's value. Access may fall back to a class attribute when the particular attribute lookup rules allow it.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Trace a shared list
  2. Decide what you intended to share

Trace a shared list

Give a class a list called labels, and create two instances with no instance-level labels attribute. Appending through the first instance can change the list seen by the second. Mutable values explain why both observations use the same list.

Decide what you intended to share

A constant category name may suit a class-wide attribute. Per-instance notes should usually be initialized for each object. A fresh fixture with two live instances makes mistaken sharing visible.[1]

References

  1. Python Software Foundation, Python Tutorial: Classes ↩