liveBook · Manning

Static field

As explained in The Quick Python Book, Fourth Edition

A class variable is an attribute defined on a class and shared through that class's inheritance structure unless shadowed or otherwise restricted.

A Python class variable holds a value on the class rather than on each instance. Define it with an assignment in the class body, outside __init__. Instances can read it, and it exists even before any instances are created. It suits information shared at the class level, such as a constant or a collection of created instances. section 15.4 section 15.8

Define and read a class variable

class Circle: pi = 3.14159 def __init__(self, radius): self.radius = radius def area(self): return self.radius * self.radius * Circle.pi Circle.pi is available before creating a Circle. After c = Circle(3), c.area() returns 28.27431. A method can also use self.__class__.pi instead of spelling out Circle. section 15.4

An instance lookup checks the instance before falling back to its class. Thus c.pi can return Circle.pi even though c has no pi of its own. Assigning c.pi = 3.14 changes the situation: it creates an instance variable that shadows Circle.pi. Other instances still see the class value. To change the class variable, assign through the class, as in Circle.pi = 3.14. section 15.4.1

One assignment does not update every instance

Given Circle.pi = 3.14159, create c1 = Circle(1) and c2 = Circle(2). After c1.pi = 3.14, the values are c1.pi == 3.14, c2.pi == 3.14159, and Circle.pi == 3.14159. Reading through an instance and assigning through it have different effects. section 15.4.1

Inheritance adds another place a name can be found. A subclass and its instances can read an inherited class variable. Assigning that name on the subclass creates a subclass variable without changing the superclass variable; assigning it on an instance creates a further shadow. In ordinary instance lookup, the relevant order is instance, class, then superclass. Names beginning with two underscores are private and cannot easily be seen by subclasses. section 15.7 section 15.11

Three values behind one name

With class P: z = "Hello" and class C(P): pass, let c = C(). Initially c.z, C.z, and P.z all read "Hello". After C.z = "Bonjour", c.z and C.z read "Bonjour", while P.z remains "Hello". After c.z = "Ciao", each reads a different value: "Ciao", "Bonjour", and "Hello", respectively. section 15.7

A class variable is not a static method. For example, a class can keep an all_circles list as a class variable, use a class method to calculate an aggregate from it, and use a static method for a calculation that receives neither an instance nor a class. Keep those roles separate when choosing where a value or operation belongs. section 15.8 section 15.15

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Works through class-body definition, instance lookup, assignment shadowing, and inheritance without examining implementation internals.
Examples
Shows a circle-area constant, two instances with different pi lookups, inherited values shadowed at two levels, and an instance collection.
What it assumes
Assumes general programming familiarity but not prior knowledge of a particular object-oriented language or paradigm.
What you can do after
Build it from scratch with a class-body assignment, then update the class rather than an instance when the shared value must change.
How it connects
Connects class variables to instance variables, attribute lookup, inheritance, private names, class methods, and static methods.

Also explained in

Compare how 7 books explain static field →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.