Reading preferences
Concept

Classes, Objects, and Inheritance

Follow object creation, attribute lookup, and a simple inherited method.

python, classes, objects, inheritance, methods

From a blueprint to an object

A class describes behaviour and can supply shared attributes. An object is a particular value; an instance is an object associated with a class. An instance method receives the instance as its first parameter, conventionally named self. Calling item.describe() passes item to that parameter.

Python illustration

class Note:
    category = "general"

    def __init__(self, title):
        self.title = title

    def describe(self):
        return f"{self.category}: {self.title}"

class StudyNote(Note):
    category = "study"

first = Note("Read")
second = StudyNote("Practice")
print(first.describe())
print(second.describe())

Prepared output — not live execution:

general: Read
study: Practice

Constructing each object calls the inherited initialiser, which assigns its own title. StudyNote inherits describe from Note, while its class attribute supplies a different category.

See the reasoning

Follow the example one step at a time

Follow construction and method lookup for the two note objects. This is a prepared teaching demonstration, not live code execution.

Define classes

Note: category = general; describe
StudyNote → Note; category = study

StudyNote inherits from Note and supplies its own class attribute.

Instance and class state

Instance attributes such as self.title belong to a particular object. A class attribute such as category can be looked up through an instance if the instance has no attribute with that name. Assigning first.category creates or changes an instance attribute; it does not update Note.category. Mutable class attributes are shared unless deliberately replaced, so use an instance attribute for per-object lists.

__init__ initialises an object after creation; object creation itself is handled separately by __new__. A subclass can extend a parent and call super().__init__(...) when its own initialiser needs the parent's setup. Inheritance is one option, not a requirement for modelling every relationship.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. What is an instance?
2. What does __init__ normally do?
3. Where is a class variable normally stored?
4. What can a subclass do with a base class method?

References and further reading