Book Image

Python Object-Oriented Programming - Fourth Edition

By : Steven F. Lott, Dusty Phillips
2 (1)
Book Image

Python Object-Oriented Programming - Fourth Edition

2 (1)
By: Steven F. Lott, Dusty Phillips

Overview of this book

Object-oriented programming (OOP) is a popular design paradigm in which data and behaviors are encapsulated in such a way that they can be manipulated together. Python Object-Oriented Programming, Fourth Edition dives deep into the various aspects of OOP, Python as an OOP language, common and advanced design patterns, and hands-on data manipulation and testing of more complex OOP systems. These concepts are consolidated by open-ended exercises, as well as a real-world case study at the end of every chapter, newly written for this edition. All example code is now compatible with Python 3.9+ syntax and has been updated with type hints for ease of learning. Steven and Dusty provide a comprehensive, illustrative tour of important OOP concepts, such as inheritance, composition, and polymorphism, and explain how they work together with Python’s classes and data structures to facilitate good design. In addition, the book also features an in-depth look at Python’s exception handling and how functional programming intersects with OOP. Two very powerful automated testing systems, unittest and pytest, are introduced. The final chapter provides a detailed discussion of Python's concurrent programming ecosystem. By the end of the book, you will have a thorough understanding of how to think about and apply object-oriented principles using Python syntax and be able to confidently create robust and reliable programs.
Table of Contents (17 chapters)
15
Other Books You May Enjoy
16
Index

What this book covers

This book is divided into four overall sections. The first six chapters provide the core principles and concepts of object-oriented programming and how these are implemented in Python. The next three chapters take a close look at Python built-in features through the lens of object-oriented programming. Chapters 10, 11, and 12 look at a number of common design patterns and how these can be handled in Python. The final section covers two additional topics: testing and concurrency.

Chapter 1, Object-Oriented Design, introduces the core concepts underlying object-oriented design. This provides a road map through the ideas of state and behavior, attributes and methods, and how objects are grouped into classes. This chapter also looks an encapsulation, inheritance, and composition. The case study for this chapter introduces the machine learning problem, which is an implementation of a k-nearest neighbors (k-NN) classifier.

Chapter 2, Objects in Python, shows how class definitions work in Python. This will include the type annotations, called type hints, class definitions, modules, and packages. We'll talk about practical considerations for class definition and encapsulation. The case study will begin to implement some of the classes for the k-NN classifier.

Chapter 3, When Objects Are Alike, addresses how classes are related to each other. This will include how to make use of inheritance and multiple inheritance. We'll look at the concept of polymorphism among the classes in a class hierarchy. The case study will look at alternative designs for the distance computations used to find the nearest neighbors.

Chapter 4, Expecting the Unexpected, looks closely at Python's exceptions and exception handling. We'll look at the built-in exception hierarchy. We'll also look at how unique exceptions can be defined to reflect a unique problem domain or application. In the case study, we'll apply exceptions to data validation.

Chapter 5, When to Use Object-Oriented Programming, dives more deeply into design techniques. This chapter will look at how attributes can be implemented via Python's properties. We'll also look at the general concept of a manager for working with collections of objects. The case study will apply these ideas to expand on the k-NN classifier implementation.

Chapter 6, Abstract Base Classes and Operator Overloading, is a deep dive into the idea of abstraction, and how Python supports abstract base classes. This will involve comparing duck typing with more formal methods of Protocol definition. It will include the techniques for overloading Python's built-in operators. It will also look at metaclasses and how these can be used to modify class construction. The case study will redefine some of the existing classes to show how abstraction must be used carefully to lead to simplification of a design.

Chapter 7, Python Data Structures, examines a number of Python's built-in collections. This chapter examines tuples, dictionaries, lists, and sets. It also looks at how dataclasses and named tuples can simplify a design by providing a number of common features of a class. The case study will revise some earlier class definitions to make use of these new techniques.

Chapter 8, The Intersection of Object-Oriented and Functional Programming, looks at Python constructs that aren't simply class definitions. While all of Python is object-oriented, function definitions allow us to create callable objects without the clutter of a class definition. We'll also look at Python's context manager construct and the with statement. In the case study, we'll look at alternative designs that avoid some class clutter.

Chapter 9, Strings, Serialization, and File Paths, covers the way objects are serialized as strings and how strings can be parsed to create objects. We'll look at several physical formats, including Pickle, JSON, and CSV. The case study will revisit how sample data is loaded and processed by the k-NN classifier.

Chapter 10, The Iterator Pattern, describes the ubiquitous concept of iteration in Python. All of the built-in collections are iterable, and this design pattern is central to a great deal of how Python works. We'll look at Python comprehensions and generator functions, also. The case study will revisit some earlier designs using generator expressions and list comprehensions to partition samples for testing and training.

Chapter 11, Common Design Patterns, looks at some common object-oriented design. This will include the Decorator, Observer, Strategy, Command, State, and Singleton design patterns.

Chapter 12, Advanced Design Patterns, looks at some more advanced object-oriented design. This will include the Adapter, Façade, Flyweight, Abstract Factory, Composite, and Template patterns.

Chapter 13, Testing Object-Oriented Programs, shows how to use unittest and pytest to provide an automated unit test suite for a Python application. This will look at some more advanced testing techniques, like using mock objects to isolate the unit under test. The case study will show how to create test cases for the distance computations covered in Chapter 3.

Chapter 14, Concurrency, looks at how we can make use of multi-core and multi-processor computer systems to do computations rapidly and write software that is responsive to external events. We'll look at threads and multiprocessing, as well as Python's asyncio module. The case study will show how to use these techniques to do hyperparameter tuning on the k-NN model.