Book description
Gain comprehensive insights into programming practices, and code portability and reuse to build flexible and maintainable apps using object-oriented principles
Key Features
- Extend core OOP techniques to increase integration of classes created with Python
- Explore various Python libraries for handling persistence and object serialization
- Learn alternative approaches for solving programming problems with different attributes to address your problem domain
Book Description
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.
What you will learn
- Explore a variety of different design patterns for the __init__() method
- Learn to use Flask to build a RESTful web service
- Discover SOLID design patterns and principles
- Use the features of Python 3's abstract base
- Create classes for your own applications
- Design testable code using pytest and fixtures
- Understand how to design context managers that leverage the 'with' statement
- Create a new type of collection using standard library and design techniques
- Develop new number types above and beyond the built-in classes of numbers
Who this book is for
This book is for developers who want to use Python to create efficient programs. A good understanding of Python programming is required to make the most out of this book. Knowledge of concepts related to object-oriented design patterns will also be useful.
Table of contents
- Title Page
- Copyright and Credits
- About Packt
- Contributors
- Preface
- Section 1: Tighter Integration Via Special Methods
-
Preliminaries, Tools, and Techniques
- Technical requirements
- About the Blackjack game
- The Python runtime and special methods
- Interaction, scripting, and tools
- Selecting an IDE
- Consistency and style
- Type hints and the mypy program
- Performance – the timeit module
- Testing – unittest and doctest
- Documentation – sphinx and RST markup
- Installing components
- Summary
-
The __init__() Method
- Technical requirements
- The implicit superclass – object
- The base class object __init__() method
- Implementing __init__() in a superclass
- Creating enumerated constants
- Leveraging __init__() via a factory function
- Implementing __init__() in each subclass
- Composite objects
- Complex composite objects
- Stateless objects without __init__()
- Some additional class definitions
- Multi-strategy __init__()
- Yet more __init__() techniques
- Summary
- Integrating Seamlessly - Basic Special Methods
- Attribute Access, Properties, and Descriptors
- The ABCs of Consistent Design
- Using Callables and Contexts
-
Creating Containers and Collections
- Technical requirements
- ABCs of collections
- Examples of special methods
- Using the standard library extensions
- Creating new kinds of collections
- Narrowing a collection's type
- Defining a new kind of sequence
- Creating a new kind of mapping
- Creating a new kind of set
- Design considerations and tradeoffs
- Summary
- Creating Numbers
- Decorators and Mixins - Cross-Cutting Aspects
- Section 2: Object Serialization and Persistence
-
Serializing and Saving - JSON, YAML, Pickle, CSV, and XML
- Technical requirements
- Understanding persistence, class, state, and representation
- Filesystem and network considerations
- Defining classes to support persistence
- Dumping and loading with JSON
- Dumping and loading with YAML
- Dumping and loading with pickle
- Dumping and loading with CSV
- Dumping and loading with XML
- Summary
-
Storing and Retrieving Objects via Shelve
- Technical requirements
- Analyzing persistent object use cases
- Creating a shelf
- Designing shelvable objects
- Searching, scanning, and querying
- Designing an access layer for shelve
- Creating indexes to improve efficiency
- Adding yet more index maintenance
- The writeback alternative to index updates
- Summary
-
Storing and Retrieving Objects via SQLite
- Technical requirements
- SQL databases, persistence, and objects
- Processing application data with SQL
- Mapping Python objects to SQLite BLOB columns
- Mapping Python objects to database rows manually
- Improving performance with indices
- Adding an ORM layer
- Querying posts that are given a tag
- Defining indices in the ORM layer
- Summary
-
Transmitting and Sharing Objects
- Technical requirements
- Class, state, and representation
- Using HTTP and REST to transmit objects
- Using Flask to build a RESTful web service
- Handling stateful REST services
- Creating a secure REST service
- Implementing REST with a web application framework
- Using a message queue to transmit objects
- Summary
-
Configuration Files and Persistence
- Technical requirements
- Configuration file use cases
- Representation, persistence, state, and usability
- Storing the configuration in INI files
- Handling more literals via the eval() variants
- Storing the configuration in PY files
- Why exec() is a non-problem
- Using ChainMap for defaults and overrides
- Storing the configuration in JSON or YAML files
- Storing the configuration in properties files
- Using XML files – PLIST and others
- Summary
- Section 3: Object-Oriented Testing and Debugging
- Design Principles and Patterns
- The Logging and Warning Modules
- Designing for Testability
-
Coping with the Command Line
- Technical requirements
- The OS interface and the command line
- Using the pathlib module
- Parsing the command line with argparse
- Integrating command-line options and environment variables
- Customizing the help output
- Creating a top-level main() function
- Programming in the large
- Additional composite Command design patterns
- Integrating with other applications
- Summary
- Module and Package Design
-
Quality and Documentation
- Technical requirements
- Writing docstrings for the help() function
- Using pydoc for documentation
- Better output via RST markup
- Writing effective docstrings
- Writing file-level docstrings, including modules and packages
- More sophisticated markup techniques
- Using Sphinx to produce the documentation
- Writing the documentation
- Literate programming
- Summary
- Other Books You May Enjoy
Product information
- Title: Mastering Object-Oriented Python - Second Edition
- Author(s):
- Release date: June 2019
- Publisher(s): Packt Publishing
- ISBN: 9781789531367
You might also like
book
Mastering Object-oriented Python
For those who know the basics of object-oriented Python this book is a must-have. With 750 …
book
Python Object-Oriented Programming - Fourth Edition
A comprehensive guide to exploring modern Python through data structures, design patterns, and effective object-oriented techniques …
book
Effective Python: 90 Specific Ways to Write Better Python, 2nd Edition
Updated and Expanded for Python 3 It’s easy to start developing programs with Python, which is …
book
Learning Python, 5th Edition
Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on …