Skip to content

OOP & the Data Model

Python is a fully object-oriented language: integers, strings, functions, modules — every value is an object with an identity, a type, and a value. A class is the blueprint; an object (or instance) is what you create from that blueprint with ClassName(...).

x = 42
print(type(x)) # <class 'int'>
print(id(x)) # unique memory address

Python exposes nearly every built-in operation through dunder methods (double-underscore methods like __init__, __repr__, __add__). When you write a + b, Python calls a.__add__(b). When you write len(obj), Python calls obj.__len__(). When you write repr(obj), Python calls obj.__repr__().

This protocol is called the Python Data Model — it lets your own classes integrate seamlessly with built-in syntax and standard-library functions.

class Point:
def __init__(self, x: float, y: float) -> None:
self.x = x
self.y = y
def __repr__(self) -> str:
return f"Point({self.x}, {self.y})"
  • __init__ is the initialiser — it runs right after the object is created and sets up instance attributes.
  • __repr__ gives the object a developer-friendly string representation.
LessonTopic
1. Classesclass, __init__, instance vs class attributes, inheritance
2. Dunder methods__repr__, __eq__, __len__, __getitem__, __add__
3. Properties@property, computed attributes, @classmethod, @staticmethod
4. Dataclasses@dataclass, defaults, field, frozen=True
class Point:
def __init__(self, x: float, y: float) -> None:
self.x = x
self.y = y
def __repr__(self) -> str:
return f"Point({self.x}, {self.y})"
def __add__(self, other: "Point") -> "Point":
return Point(self.x + other.x, self.y + other.y)
p1 = Point(1, 2)
p2 = Point(3, 4)
print(p1)
print(p1 + p2)
print(type(p1))
print(isinstance(p1, Point))
Which dunder method runs when you write `obj = MyClass(1, 2)`?
What does Python call internally when you write `len(my_list)`?
What is the term for the system of dunder methods that integrates classes with Python syntax?
Which statement about Python objects is true?