# Matplotlib compatibility patch for Pyodide
import matplotlib
if not hasattr(matplotlib.RcParams, "_get"):
matplotlib.RcParams._get = dict.get
Attribution
This page originates from TeachBooks/learn-programming.git, version: mude-2025
5.1. Classes and Object-Oriented Programming in Python#
Object-Oriented Programming (OOP) is a way of programming that organizes code into reusable blocks of code called classes, which bundle data (attributes) and behaviors (methods) together. When you want to use a class in one of your programs, you make an object from that class, which is where the phrase “object-oriented” comes from.
OOP promotes:
modularity: making multiple modules or functions and then combining them to form a complete system
reusability: shared attributes and functions can be reused without the need of rewriting code
better code organization: less code and more reuse
OOP is defined by 3 main concepts:
Encapsulation: concept of bundling data and methods that operate on that data into a single unit called a class. Its purpose is to help in organizing and protecting an object’s internal state and behavior;
Inheritance: mechanism that allows a new class to inherit attributes and methods from an existing class. Its purpose is to promote code reusability, extensibility, and the creation of class hierarchies, where subclasses can build upon the functionality of their parent classes;
Polymorphism: ability of different objects to respond to the same method name in a way that is specific to their class. It simplifies code by enabling flexibility and dynamic behavior.
These concepts may seem abstract for now, but don’t be concerned because you have already been using them quite a lot: everything you do in Python uses objects! This should become obvious as you go through this chapter, after which you should be able to:
Understand the fundamental concepts of classes and object-oriented programming (OOP) in Python.
Comprehend the key principles of encapsulation, inheritance, and polymorphism in OOP.
Define and create classes in Python.
Create objects from classes and understand the relationship between classes and objects.