Log inStart free
LESSON 1.04 · 6 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

Classes and attribute lookup

Watch Python search an instance, its class and the bases for an attribute.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 2 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 1.04 · CLASS BAY5 CLASSES · 5 BASE LINKS
01
Stage 1 of 6 · Define classes
Each class statement makes a class object with its own dict. D has two bases: B, then C.
Explain it simply

A class is an object too, and its box lists its dict of attributes. The arrows show bases, the classes each one builds on, ending at object on the right. D builds on B, then C, and both build on A, so A can be reached two ways. The next stage puts all five classes in one search order.

Five class objects. D has bases B and C. B and C have base A. A has base object. Each box lists its dict.

nameobject, dict (pale: after the stop)reference: name, base, __class__, __func__, __self__new row, search hit, resultdict read, no matchAttributeError
1class A … D(B, C)2D.__mro__3x = D()4x.kind5x.hello()6x.kind = 'x'DBCAobjectclass D(B, C)__init__functionclass B(A)hellofunctionclass C(A)kind'C'hellofunctionclass Akind'A'level0hellofunctionobject__init____repr__…1class A … D(B, C)2D.__mro__3x = D()4x.kind5x.hello()6x.kind = 'x'DBCAobjectclass D(B, C)__init__functionclass B(A)hellofunctionclass C(A)kind'C'hellofunctionclass Akind'A'level0hellofunctionobject__init____repr__…
Toy: 4 classes + object · 1 instance · 6 statements · no property (1.06) · ids are stand-ins
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
D=type('D', bases, dict)\sy{cls}{ink}{D} = \mathrm{type}(\htmlClass{pyq}{\mathtt{\char"27}\texttt{D}\mathtt{\char"27}},\ \sy{bases}{param}{\mathrm{bases}},\ \sy{ns}{data}{\mathrm{dict}})
D=type('D', (B, C), {'__init__':function})D = \mathrm{type}(\htmlClass{pyq}{\mathtt{\char"27}\texttt{D}\mathtt{\char"27}},\ (B,\ C),\ \{\htmlClass{pyq}{\mathtt{\char"27}\texttt{\_\_init\_\_}\mathtt{\char"27}}: \mathrm{function}\})

A class statement calls type with a name, the bases and a dict.

Common mix-ups

MYTHPython searches B and all of B's bases before C.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01With class D(C, B), whose hello does x.hello() run?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 2 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.