Inheritance
A class can be built on top of another: it inherits everything the base class has, and you only add or change what you need.
Used in 45 lessons of the course, from 01.01.
When you write class Child(Base):, the name in parentheses is the base class. The new class receives its methods and attributes without you copying them.
If you define a method with the same name in the child class, it replaces the base one. This is called an override. Python looks in the child first, then in the base.
In the course you see it in class ConfigSpy(BaseCallbackHandler):. LangChain already ships a base class with empty methods for reacting to events. You inherit from it and override only the methods you care about. That way your class fits wherever LangChain expects a handler, without writing everything from scratch.
class Child(Base):
...
Reach for it when: a class is a more specific version of another, or when a framework asks you to subclass its base class to plug into it.
Examples
Common mistake
class Base:
def __init__(self, name):
self.name = name
class Child(Base):
def __init__(self):
self.extra = 1
c = Child()
print(c.name)Why: Defining `__init__` in the child replaced the base one, so `self.name` was never created.
How to write it: Call super().__init__(name) inside the child's __init__ and accept name as a parameter.
