liveBook · Manning

Chapter 17: Data types as objects

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

The introduction explains that Python’s dynamic typing makes types relevant at runtime, not just the objects they describe.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains that Python’s dynamic typing makes types relevant at runtime, not just the objects they describe.

Key terms: dynamically typed, runtime, compile time, types of objects

17.1 Types are objects too

This section uses the built-in type function to show that Python types and classes are themselves objects.

Key terms: type function, int, list, type object, class, <class 'type'>

17.2 Using types

This section shows that Python type objects can be compared to check whether values have the same type.

Key terms: type objects, type checking

17.3 Types and user-defined classes

This section shows how to inspect the class of an object and its inheritance relationships, then use isinstance and issubclass for common type checks.

Key terms: class object, __class__, __name__, __bases__, isinstance, issubclass, inheritance

17.4 Duck typing

This section explains duck typing as using objects according to the behavior they provide rather than their inheritance, and weighs its flexibility against the risk of undetected bugs.

Key terms: duck typing, interface, inheritance hierarchy, iterator, type-checking, type hints, type checker

17.5 What is a special method attribute?

The section explains how Python automatically calls specially named class methods and uses __str__ to show how a class can hook into built-in behavior.

Key terms: special method attribute, dunder methods, magic methods, __str__, user-readable string representation, print

17.6 Making an object behave like a list

The section motivates list-like access to a large text file by contrasting whole-file loading with reading one line at a time.

Key terms: text file, list of lines, for loop, one line at a time

17.7 The __getitem__ special method attribute

The section shows how `__getitem__` lets a `LineReader` act like a list-like iterable and explains why wrapping file-reading details in a class can be useful.

Key terms: __getitem__, LineReader, IndexError, iterable, myutils module, one-record-per-line

17.7.1 it works

This subsection explains how `__getitem__` lets a file-reading object act like a sequence in a `for` loop, and why the example is limited.

Key terms: __getitem__, index, for loop, IndexError, LineReader, __init__, fileobject

17.7.2 Implementing full list functionality

This section explains how LineReader could enforce sequential access and how special methods can give user-defined objects more list-like behavior.

Key terms: LineReader, __getitem__, index, special method attributes, __setitem__, __add__, list emulation

17.8 Giving an object full list capability

The section develops a TypedList example to show how special methods can give a type-restricted object list-like behavior.

Key terms: typed list, homogeneity, TypedList, example_element, __init__, type, isinstance, __getitem__, __setitem__, __check

17.9 Subclassing from built-in types

This section contrasts the work of building a list-like class from scratch with subclassing an existing list type.

Key terms: list-like class, list type, UserList type, subclassing

17.9.1 Subclassing list

This section shows how subclassing Python’s list can create a typed list by changing only the behavior that needs a type check.

Key terms: subclass, list type, __setitem__, built-in type

17.9.2 Subclassing UserList

This section presents subclassing collections.UserList as an alternative way to create a variation on a list, emphasizing access to its underlying list through the data attribute.

Key terms: UserList, collections module, data attribute

17.10 When to use special method attributes

This section gives practical guidelines for deciding when to add special method attributes to a class and when subclassing a built-in type may be preferable.

Key terms: special method attributes, built-in type, subclass, indexing notation, __str__, print(instance), debugging

17.11 Creating a string-only key-value dictionary

This section outlines how to subclass dict to allow only string keys and values, including when the dictionary is initialized.

Key terms: dict, __setitem__(), __init__(), UserDict

17.11.1 Solving the problem with AI-generated code

This subsection advises how to prompt an AI to generate a dictionary subclass and identifies two methods the solution must handle.

Key terms: subclass dict, prompt, __setitem__(), __init__()

17.11.2 Solutions and discussion

17.12 Summary

Concepts explained in this chapter

← Chapter 16: Regular expressions Chapter 18: Packages →

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.