9. OOP#
Object-oriented programming is a paradigm for organizing data and behavior together in reusable types.
Classes as blueprints for objects
Instances as concrete objects with their own state
Methods as behavior attached to objects
Encapsulation, inheritance, polymorphism, and abstraction
Advanced class features for Pythonic designs
This chapter starts with foundational ideas, works through practical examples, and builds toward more advanced OOP techniques.
Chapter Video
This overview introduces classes, instances, attributes, and methods in Python.
Python OOP Tutorial 1: Classes and Instances (15:24) by Corey Schafer
Hosted on YouTube; property of its original creator, embedded here for educational reference only.
Learning Objectives
By the end of this chapter, you will be able to:
Explain what object-oriented programming is, distinguish a class from an instance, and recognize built-in values as instances of classes
Define a class with
__init__, attributes, and methods, and give its objects readable__str__and__repr__representationsProtect an object’s state with naming conventions and
@propertyBuild a class hierarchy with inheritance,
super(), and method overriding, and use polymorphism to call the same method on different classesDefine an abstract base class with
abcthat requires its subclasses to implement certain methodsImplement comparison and arithmetic special methods, and choose among
@dataclass,namedtuple,@staticmethod, and@classmethodfor records and class-level behavior
Chapter Flow
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
Object-oriented programming |
A programming style that organizes data and behavior into objects. |
2 |
Class |
A blueprint for creating objects with shared attributes and methods. |
3 |
Instance |
A specific object created from a class. |
4 |
Attribute |
Data stored on an object or class. |
5 |
Method |
A function defined inside a class and called on an object or class. |
6 |
|
The conventional name for the current instance inside an instance method. |
7 |
Constructor |
Code that initializes a new object, commonly written with |
8 |
Dunder method |
A special method with double underscores, such as |
9 |
Encapsulation |
Keeping related data and behavior together behind a clear interface. |
10 |
Inheritance |
Defining a class from another class to reuse or specialize behavior. |
11 |
Polymorphism |
Using a shared interface with objects of different types. |
12 |
Abstraction |
Focusing on what an object does while hiding unnecessary implementation details. |
13 |
Method overriding |
Redefining an inherited method in a child class. |
14 |
|
A function used to call behavior from a parent class. |
15 |
Dataclass |
A class created with |
16 |
|
An immutable tuple-like object whose fields can be accessed by name. |
17 |
Class variable |
A variable stored on the class and shared by its instances. |
18 |
Instance variable |
A variable stored separately on each instance. |
19 |
Static method |
A method placed in a class namespace that does not receive |
20 |
Class method |
A method that receives the class as its first argument, conventionally named |
21 |
Abstract base class |
A class that cannot be instantiated and declares methods its subclasses must implement, created with the |
22 |
|
A decorator that lets a method be read like an attribute, used to control access to an object’s state. |
23 |
Name mangling |
Python renames an attribute that starts with two underscores, such as |
24 |
Operator overloading |
Defining special methods such as |
25 |
Frozen dataclass |
A dataclass created with |
26 |
Bound method |
A method accessed through an object without calling it, which remembers that object and can be stored in a variable and called later. |