更新时间:2021-11-12 16:26:34
coverpage
Mastering Object-oriented Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files eBooks discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Note
Tip
Reader feedback
Customer support
Downloading the example code for this book
Errata
Piracy
Questions
Some Preliminaries
About casino Blackjack
Playing the game
Blackjack player strategies
Object design for simulating Blackjack
Performance – the timeit module
Testing – unittest and doctest
Unit testing and technology spikes
Docstrings – RST markup and documentation tools
The IDE question
About special method names
Summary
Part 1. Pythonic Classes via Special Methods
Pythonic Classes via Special Methods
Chapter 1. The __init__() Method
The implicit superclass – object
The base class object __init__() method
Implementing __init__() in a superclass
Using __init__() to create manifest constants
Leveraging __init__() via a factory function
Faulty factory design and the vague else clause
Simplicity and consistency using elif sequences
Simplicity using mapping and class objects
Implementing __init__() in each subclass
Simple composite objects
Wrapping a collection class
Extending a collection class
More requirements and another design
Complex composite objects
Complete composite object initialization
Stateless objects without __init__()
Some additional class definitions
Multi-strategy __init__()
More complex initialization alternatives
Initializing static methods
Yet more __init__() techniques
Initialization with type validation
Initialization encapsulation and privacy
Chapter 2. Integrating Seamlessly with Python Basic Special Methods
The __repr__() and __str__() methods
Non collection __str__() and __repr__()
Collection __str__() and __repr__()
The __format__() method
Nested formatting specifications
Collections and delegating format specifications
The __hash__() method
Deciding what to hash
Inheriting definitions for immutable objects
Overriding definitions for immutable objects
Overriding definitions for mutable objects
Making a frozen hand from a mutable hand
The __bool__() method
The __bytes__() method
The comparison operator methods
Designing comparisons
Implementation of comparison for objects of the same class
Implementation of comparison for objects of mixed classes
Hard totals soft totals and polymorphism
A mixed class comparison example
The __del__() method
The reference count and destruction
Circular references and garbage collection
Circular references and the weakref module
The __del__() and close() methods
The __new__() method and immutable objects