Object-Oriented Programming

9. Object-Oriented Programming#

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

Reading scope: the three content notebooks contain the required route. Detailed name mangling in 9.2 and derived ordering, hashing, frozen records, and shared mutable class-state examples in 9.3 are labeled optional enrichment. The core demonstrations and core practice stand on their own.

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

Initializer (__init__)

The method called on a new instance to set its initial state; it returns None.

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 (optional)

Python renames an attribute that starts with two underscores, such as __pin, to _ClassName__pin, which reduces accidental name collisions in subclasses; it does not make the attribute private.

24

Operator overloading

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

25

Frozen dataclass (optional)

A dataclass that rejects normal field reassignment; referenced mutable objects are not frozen.

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.

27

Invariant

A condition that public operations preserve, such as a nonnegative account balance.

28

NotImplemented

A special return value telling Python that an operation does not support the other operand type.

29

Attribute shadowing

An instance attribute hides the same-named class attribute for that instance.

30

Constructor call

Calling a class creates an object and invokes its initializer with the supplied arguments.

31

Duck typing

Calling a method on any object that has it, whatever its class; polymorphism without a shared parent class.

32

Public interface

The methods and properties callers are meant to use, as opposed to internal attributes such as _balance.

33

Superclass

The parent class that a subclass inherits from; a subclass is the child class.

34

Comparison key

A value, often a tuple, that decides how objects are ordered; Python compares its elements from left to right.

35

Hashable (optional)

Having a hash value that never changes, so the object can be a set element or dictionary key; equal objects must have equal hashes.

Chapter Overview Slides