OOP

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:

  1. Explain what object-oriented programming is, distinguish a class from an instance, and recognize built-in values as instances of classes

  2. Define a class with __init__, attributes, and methods, and give its objects readable __str__ and __repr__ representations

  3. Protect an object’s state with naming conventions and @property

  4. Build a class hierarchy with inheritance, super(), and method overriding, and use polymorphism to call the same method on different classes

  5. Define an abstract base class with abc that requires its subclasses to implement certain methods

  6. Implement comparison and arithmetic special methods, and choose among @dataclass, namedtuple, @staticmethod, and @classmethod for 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

self

The conventional name for the current instance inside an instance method.

7

Constructor

Code that initializes a new object, commonly written with __init__.

8

Dunder method

A special method with double underscores, such as __str__ or __lt__.

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

super

A function used to call behavior from a parent class.

15

Dataclass

A class created with @dataclass to reduce boilerplate for data objects.

16

namedtuple

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 self or cls.

20

Class method

A method that receives the class as its first argument, conventionally named cls.

21

Abstract base class

A class that cannot be instantiated and declares methods its subclasses must implement, created with the abc module.

22

@property

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 __pin, to _ClassName__pin, which discourages access from outside the class.

24

Operator overloading

Defining special methods such as __add__ or __lt__ so that operators like + and < work with your objects.

25

Frozen dataclass

A dataclass created with frozen=True, whose fields cannot be changed after the object is created.

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.

Chapter Overview Slides