liveBook · Manning

Attribute lookup

As explained in Python Workout, Second Edition

Attribute lookup is Python's process for resolving an attribute name through an object's instance, class, inheritance hierarchy, and ultimate base object.

Attribute lookup resolves a Python attribute name through an object's instance, class, inheritance hierarchy, and ultimate base object. This explains why an object can use its own attributes as well as behavior inherited from its classes. section 10.12

Use attribute lookup to reason about:

  • Instance attributes assigned through self, such as x. section 10.12
  • Methods found on a class or inherited from a parent class. section 10.12
  • Class-level declarations used with instances, including dataclass fields. section 10.12

Inherited method lookup

Bar inherits from Foo. A Bar instance therefore uses Foo's x2 method and Bar's x3 method; both methods resolve x through the instance.

How Python Workout, Second Edition covers it

How deep it goes
Defines lookup across instances, classes, inheritance, and the ultimate base object, with examples of inherited methods.
Examples
Uses Foo and Bar inheritance, instance attribute x, inherited methods, and dataclass fields as concrete examples.
What it assumes
Assumes familiarity with Python objects, classes, instances, and methods while illustrating inheritance relationships.
What you can do after
Evaluate how Python resolves an attribute on an instance and identify whether inheritance supplies the requested behavior.
How it connects
Connects attributes with objects, classes, instances, inheritance, methods, and dataclasses.

Also explained in

Compare how 2 books explain attribute lookup →

Related concepts

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