Research indexBook chapter
Python Custom Classes
Campesato
Chapman & Hall/CRC eBooks21 Apr 2026
NobleIDark:/48914/w1/20261105/5E47CFD7DOI10.1201/9781003676980-4
Python Custom Classes
Campesato · Chapman & Hall/CRC eBooks · 2026 · Subscription
NobleIDark:/48914/w1/20261105/5E47CFD7DOI10.1201/9781003676980-4
The full text is at the publisher
You can read the abstract here, or open it at the publisher.
Abstract
This chapter provides a comprehensive introduction to object-oriented programming (OOP) in Python , focusing on creating and managing custom classes. It begins by explaining Python&s;s accessibility conventions, noting that while everything in Python is public, conventions like single underscore (_) for protected and double underscore (__) for private variables and methods are used, though not strictly enforced. The chapter introduces three ways to define a class (e.g., class MyClass, class MyClass(), class MyClass(object)), emphasizing that all Python 3 classes implicitly inherit from the object class. Key concepts like instance variables (unique to each object) versus class variables (shared across instances) are illustrated with examples like the Company class. The chapter also covers magic methods (e.g., __init__, __str__, __repr__), which enable custom behavior for object initialization, string representation, and comparisons, as shown in code snippets like MyPoint3D.py and ComparePoint3D.py.This chapter then provides examples of custom Python iterable classes, such as the Cube class, which implements __iter__ and __next__ for iteration, and highlights errors in invalid iterator definitions (e.g., WrongIter.py). It explores advanced OOP concepts like encapsulation, demonstrated in encapsulation1.py, where private methods handle internal logic, and inheritance, both single (e.g., SingleInherit1.py) and multiple (e.g., MultipleInherit1.py). Polymorphism is explained through examples like Polymorphism1.py, showcasing how methods can operate on different class instances via duck-typing. Practical applications include managing collections of objects, such as in the Employees class, and data structures like linked lists (LLAndList.py, LLAndDict.py) and priority queues (PriorityQueue.py), which use custom classes to organize data efficiently. This chapter also addresses advanced topics like abstract base classes (via the abc module) for enforcing method implementation and the use of inspect for examining class hierarchies (e.g., display_base_classes.py). It contrasts Python&s;s flexible OOP approach with stricter languages like Java, noting features like virtual methods and the absence of interfaces. The document concludes with a summary reinforcing these concepts, encouraging further exploration of OOP for robust Python programming. Each code example, such as FoodPreferences.py for inheritance and Counter1.py for iterators, provides practical demonstrations of these principles, making the chapter a solid foundation for understanding Python&s;s OOP capabilities.