Book Image

Mastering Object-Oriented Python - Second Edition

By : Steven F. Lott
Book Image

Mastering Object-Oriented Python - Second Edition

By: Steven F. Lott

Overview of this book

Object-oriented programming (OOP) is a relatively complex discipline to master, and it can be difficult to see how general principles apply to each language's unique features. With the help of the latest edition of Mastering Objected-Oriented Python, you'll be shown how to effectively implement OOP in Python, and even explore Python 3.x. Complete with practical examples, the book guides you through the advanced concepts of OOP in Python, and demonstrates how you can apply them to solve complex problems in OOP. You will learn how to create high-quality Python programs by exploring design alternatives and determining which design offers the best performance. Next, you'll work through special methods for handling simple object conversions and also learn about hashing and comparison of objects. As you cover later chapters, you'll discover how essential it is to locate the best algorithms and optimal data structures for developing robust solutions to programming problems with minimal computer processing. Finally, the book will assist you in leveraging various Python features by implementing object-oriented designs in your programs. By the end of this book, you will have learned a number of alternate approaches with different attributes to confidently solve programming problems in Python.
Table of Contents (25 chapters)
Free Chapter
1
Section 1: Tighter Integration Via Special Methods
11
Section 2: Object Serialization and Persistence
17
Section 3: Object-Oriented Testing and Debugging

What this book covers

In this book, we'll cover three broad areas of advanced Python topics. Each topic will be broken into a series of chapters examining a variety of details.

Section 1, Tighter Integration via Special Methods, looks at object-oriented programming (OOP) techniques in depth and how we can more tightly integrate the class definitions of our applications with Python's built-in features. This section consists of nine chapters, which are as follows:

  • Chapter 1, Preliminaries, Tools, and Techniques, covers some preliminary topics, such as unittest, doctest, docstring, and some special method names.
  • Chapter 2, The _init_() Method, provides us with a detailed description and implementation of the _init_() method. We will examine different forms of initialization for simple objects. Following this, we can explore more complex objects that involve collections and containers.
  • Chapter 3, Integrating Seamlessly – Basic Special Methods, explains, in detail, how we can expand a simple class definition to add special methods. We'll need to take a look at the default behavior inherited from the object so that we can understand what overrides are required and when they're actually required.
  • Chapter 4, Attribute Access, Properties, and Descriptors, explores how default processing works in some detail. Here, we will learn how to decide where and when to override the default behavior. We will also explore descriptors and gain a much deeper understanding of how Python's internals work.
  • Chapter 5, The ABCs of Consistent Design, examines the abstract base classes in the collections.abc module. In this chapter, we'll look at the general concepts behind the various containers and collections that we might want to revise or extend. Similarly, we'll look at the concepts behind the numbers that we might want to implement.
  • Chapter 6, Using Callables and Contexts, uncovers several ways to create context managers using the tools in contextlib. We'll demonstrate a number of variant designs for callable objects. This will show you why a stateful callable object is sometimes more useful than a simple function. We'll also explore how to use some of the existing Python context managers before we dive in and write our own context manager.
  • Chapter 7, Creating Containers and Collections, focuses on the basics of container classes. We'll review the variety of special methods that are involved in creating a container and the various features that containers offer. We'll address extending built-in containers to add features. We'll also look at wrapping built-in containers and delegating methods through the wrapper to the underlying container.
  • Chapter 8, Creating Numbers, covers these essential arithmetic operators: +, -, *, /, //, %, and **. We'll also explore these comparison operators: <, >, <=, >=, ==, and !=. We'll finish by summarizing some of the design considerations that go into extending or creating new numbers.
  • Chapter 9, Decorators and Mixins Cross-Cutting Aspects, covers simple function decorators, function decorators with arguments, class decorators, and method decorators.

Section 2, Object Serialization and Persistence, explores a persistent object that has been serialized to a storage medium; perhaps it's transformed to JSON and written to the filesystem. An ORM layer can store the object in a database. This section examines the alternatives for handling persistence. It contains five chapters, which are as follows:

  • Chapter 10, Serializing and Saving – JSON, YAML, Pickle, CSV, and XML, covers simple persistence using libraries focused on various data representations such as JSON, YAML, pickle, XML, and CSV.
  • Chapter 11, Storing and Retrieving Objects via Shelve, explains basic database operations with Python modules, such as shelve (and dbm).
  • Chapter 12, Storing and Retrieving Objects via SQLite, uncovers the more complex world of SQL and the relational database. Because SQL features don't match OOP features well, we have an impedance mismatch problem. A common solution is to use ORM to allow us to persist a large domain of objects. The SQLAlchemy package will be used as an example of the many ORMs that are available.
  • Chapter 13, Transmitting and Sharing Objects, looks at the HTTP protocol, JSON, YAML, and XML representations to transmit an object.
  • Chapter 14, Configuration Files and Persistence, covers various ways in which a Python application can work with a configuration file.
  • Chapter 15, Design Principles and Patterns, reviews the SOLID design principles. These can help organize high-quality, maintainable Python software by following some best practices.

Section 3, Object-Oriented Testing and Debugging, shows you how to gather data to support and debug your own high-performance programs. It includes information on creating the best possible documentation in order to reduce the confusion and complexity of the support. This section contains the final five chapters, which are as follows:

  • Chapter 16, The Logging and Warning Modules, looks at using the logging and warning modules to create audit information, as well as debugging. Additionally, we'll take a significant step beyond using the print() function.
  • Chapter 17, Designing for Testability, covers designing for testability and demonstrates how to use unittest and doctest.
  • Chapter 18, Coping with the Command Line, looks at using the argparse module to parse options and arguments. We'll take this a step further and use the command design pattern to create program components that can be combined and expanded without resorting to writing shell scripts.
  • Chapter 19, Module and Package Design, covers module and package design. This is a higher-level set of considerations; we'll take a look at related classes in a module and related modules in a package.
  • Chapter 20, Quality and Documentation, explores how we can document our design to create some kind of trust that our software is correct and has been properly implemented.