liveBook · Manning

Chapter 15: Classes and object-oriented programming

What you will learn in chapter 15 of The Quick Python Book, Fourth Edition

The chapter introduces Python classes as constructs that can hold both data and code, while leaving the details to later sections.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces Python classes as constructs that can hold both data and code, while leaving the details to later sections.

Key terms: Python classes, data, code

15.1 Defining classes

This section introduces classes as Python data types and shows the basic shape of a class definition and how to create an instance.

Key terms: class, data type, instance, class statement, body, pass, CapCase

15.1.1 Using a class instance as a structure or record

This section shows how class instances can hold record-like fields, how `__init__` initializes those fields, and how initialization differs from object creation.

Key terms: class instance, structures or records, fields, dot notation, __init__, initialization method, self, radius field, __new__, uninitialized object

15.2 Instance variables

This section explains how instance variables belong to individual objects, how assignment creates them, and why Python requires explicit instance-qualified access.

Key terms: instance variable, Circle, radius, instance.variable, instance variables, __init__, local variable

15.3 Methods

This section explains how Python methods are defined, called with an instance and arguments, and resolved during invocation.

Key terms: method, bound method invocation, self, __init__, self.radius, radius, default argument values, instance namespace, class, superclasses

15.4 Class variables

The section explains how class variables are defined and accessed, including how a method can refer to one without hardcoding the class name.

Key terms: class variable, class body, instance, Circle.pi, Circle.area, __class__, self.__class__.pi

15.4.1 An oddity with class variables

Explains how class variables provide fallback values for instance lookups and why assigning through an instance does not change the class variable.

Key terms: instance variable, class variable, default values, Circle.pi, c1.pi, c2.pi

15.5 Static methods and class methods

Introduces static methods and class methods as topics for the following subsections.

15.5.1 Static methods

This subsection explains how to define a static method and call it without creating a class instance.

Key terms: static methods, class instance, @staticmethod decorator

15.5.2 Class methods

This section explains how class methods receive their class automatically and why that helps methods work with subclasses.

Key terms: class method, @classmethod, cls, static method, subclasses, total_area

15.6 Inheritance

This section uses shapes to show how Python inheritance shares instance data and methods, and why subclasses must explicitly initialize their base class.

Key terms: inheritance, Shape, Circle, Square, x and y variables, super, __init__, super().__init__(x,y), Shape.__init__(self, x, y), inheritance hierarchy

15.7 Inheritance with class and instance variables

This section explains how inherited methods share an instance’s variables and how assignments through a subclass or instance can shadow an inherited class variable.

Key terms: inheritance, instance variable, superclass, subclass, class variable, derived class, name clashes

15.8 Recap: Basics of Python classes

This section brings together basic class features in a Shape and Circle example and describes how Circle instances behave.

Key terms: Circle, __init__, default parameters, instance, Shape, move(), inheritance hierarchy, class variable, total_area(), class method

15.9 Private variables and private methods

This section explains why classes use private names, how Python marks them, and why name mangling discourages accidental rather than deliberate access.

Key terms: private variable, private method, inheritance, interface, double underscore, private, instance variable, method, name mangling, _classname

15.10 Using @property for more flexible instance variables

This section explains how properties preserve dot-notation access to instance variables while letting methods control the values read and written.

Key terms: property, instance variables, getter and setter methods, dot notation, property decorator, .setter, read-only, getter method, setter, temp

15.11 Scoping rules and namespaces for class instances

This section explains which namespaces a class method can access directly or through self, and how inheritance and private names affect that access.

Key terms: local namespace, global namespace, built-in namespace, self variable, instance’s namespace, class’s namespace, superclass’s namespace, private superclass instance variables, private superclass methods, private superclass class variables

15.12 Destructors and memory management

This section explains why Python classes rarely need destructors and recommends context managers for releasing external resources.

Key terms: destructor, automatic memory management, reference-counting mechanism, external resource, context manager

15.13 Multiple inheritance

This section explains how Python classes can inherit from multiple parents, how a simple hierarchy resolves a shared method name, and why complex hierarchies require care.

Key terms: multiple inheritance, parent class, ancestor, base classes, method invocation, super, mixin, addin, name clashes, inheritance hierarchies

15.14 HTML classes

This lab defines a simplified HTML document model and specifies how its classes should produce the complete document as a string.

Key terms: element, subelement, <html>, <body>, <p>, __str__(), str()

15.14.1 Solving the problem with AI-generated code

This subsection explains why the HTML classes problem worked particularly well with AI-generated code.

Key terms: AI solution, sample code and output

15.14.2 Solutions and discussion

15.15 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 14: Exceptions Chapter 16: Regular expressions →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.