Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Mastering Object-Oriented Python
Mastering Object-Oriented Python

Mastering Object-Oriented Python: Build powerful applications with reusable code using OOP design patterns and Python 3.7 , Second Edition

eBook
AU$48.99
Paperback
AU$60.99
Subscription
Free Trial
Renews at AU$16.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Mastering Object-Oriented Python

The __init__() Method

The __init__() method is a profound feature of Python class definitions for two reasons. Firstly, initialization is the first big step in an object's life; every object must have its state initialized properly. The second reason is that the argument values for __init__() can take many forms.

Because there are so many ways to provide argument values to __init__(), there is a vast array of use cases for object creation. We'll take a look at several of them. We want to maximize clarity, so we need to define an initialization that characterizes the problem domain and clearly sets the state of the object.

Before we can get to the __init__() method, however, we need to take a look at the implicit class hierarchy in Python, glancing briefly at the class named object. This will set the stage for comparing its default behavior with the different kinds of...

Technical requirements

The implicit superclass – object

Each Python class definition has an implicit superclass: object. It's a very simple class definition that does almost nothing.

We can create instances of object, but we can't do much with them, because many of the special methods simply raise exceptions.

When we define our own class, object is the superclass. The following is an example class definition that simply extends object with a new name:

>>> class X: 
>>>     pass 

The following are some interactions with this tiny class definition:

>>> X.__class__ 
<class 'type'> 
>>> X.__class__.__base__ 
<class 'object'> 

We can see that a class is an object of the class named type and that the base class for our new class is the class named object. As we look at each method, we also take a look at the default behavior...

The base class object __init__() method

Fundamental to the life cycle of an object are its creation, initialization, and destruction. We'll defer creation and destruction to a later chapter on more advanced special methods and focus on initialization. This will set the initial state of the object.

The superclass of all classes, object, has a default implementation of __init__() that amounts to pass. We aren't required to implement __init__(). If we don't implement it, then no instance variables will be created when the object is created. In some cases, this default behavior is acceptable.

We can add attributes to an object that's a subclass of object. Consider the following class, which requires two instance variables, but doesn't initialize them:

class Rectangle: 
    def area(self) -> float: 
        return self.length * self.width 

The Rectangle class...

Implementing __init__() in a superclass

We initialize an object by implementing the __init__() method. When an object is created, Python first creates an empty object and then calls the __init__() method to set the state of the new object. This method generally creates the object's instance variables and performs any other one-time processing.

The following are some example definitions of a Card class hierarchy. We'll define a Card superclass and three subclasses that are variations of the basic theme of Card. We have two instance variables that have been set directly from argument values and two variables that have been calculated using an initialization method:

from typing import Tuple

class
Card:

def __init__(self, rank: str, suit: str) -> None:
self.suit = suit
self.rank = rank
self.hard, self.soft = self._points()

def _points(self) -...

Creating enumerated constants

We can define classes for the suits of our cards. The suits of playing cards are an example of a type with a domain that can be exhaustively enumerated. Some other types with very small domains of values include the None type, where there's only one value, and the bool type, which has only two values.

The suit of a playing card could be thought of as an immutable object: the state should not be changed. Python has one simple formal mechanism for defining an object as immutable. We'll look at techniques to assure immutability in Chapter 4, Attribute Access, Properties, and Descriptors. While it might make sense for the attributes of a suit to be immutable, the extra effort has no tangible benefit.

The following is a class that we'll use to build four manifest constants:

from enum import Enum


class Suit(str, Enum):
Club = &quot...

Leveraging __init__() via a factory function

We can build a complete deck of cards via a factory function. This beats enumerating all 52 cards. In Python, there are two common approaches to factories, as follows:

  • We define a function that creates objects of the required classes.
  • We define a class that has methods for creating objects. This is the Factory design pattern, as described in books on object-oriented design patterns. In languages such as Java, a factory class hierarchy is required because the language doesn't support standalone functions.

In Python, a class isn't required to create an object factory, but this can be a good idea when there are related factories or factories that are complex. One of the strengths of Python is that we're not forced to use a class hierarchy when a simple function might do just as well.

While this is a book about object-oriented...

Implementing __init__() in each subclass

As we look at the factory functions for creating Card objects, there are some alternative designs for the Card class. We might want to refactor the conversion of the rank number so that it is the responsibility of the Card class itself. This pushes the initialization down into each subclass.

This often requires some common initialization of a superclass as well as subclass-specific initialization. We need to follow the Don't Repeat Yourself (DRY) principle to keep the code from getting cloned into each of the subclasses.

This version of the Card3 class has an initializer at the superclass level that is used by each subclass, as shown in the following code snippet:

class Card3:

def __init__(
self, rank: str, suit: Suit, hard: int, soft: int
) -> None:
self.rank = rank
self.suit = suit
self...

Composite objects

A composite object can also be called a container. We'll look at a simple composite object: a deck of individual cards. This is a basic collection. Indeed, it's so basic that we can, without too much struggle, use a simple list object as a deck.

Before designing a new class, we need to ask this question: is using a simple list object appropriate?

We can use random.shuffle() to shuffle the deck and deck.pop() to deal cards into a player's Hand.

Some programmers rush to define new classes as if using a built-in class violates some object-oriented design principle. Avoiding a new class leaves us with the following code snippet:

>>> d = [card(r + 1, s) for r in range(13) for s in iter(Suit)]
>>> random.shuffle(d)
>>> hand = [d.pop(), d.pop()]
>>> hand
[FaceCard(suit=<Suit.Club: '♣'>, rank...

Complex composite objects

The following is an example of a Blackjack Hand description that might be suitable for emulating play strategies:

class Hand:

def __init__(self, dealer_card: Card) -> None:
self.dealer_card: Card = dealer_card
self.cards: List[Card] = []

def hard_total(self) -> int:
return sum(c.hard for c in self.cards)

def soft_total(self) -> int:
return sum(c.soft for c in self.cards)

def __repr__(self) -> str:
return f"{self.__class__.__name__} {self.dealer_card} {self.cards}"

In this example, we have a self.dealer_card instance variable based on a parameter of the __init__() method. The self.cards instance variable, however, is not based on any parameter. This kind of initialization creates an empty collection. Note that the assignment to the self.cards variable requires...

Stateless objects without __init__()

The following is an example of a degenerate class that doesn't need an __init__() method. It's a common design pattern for Strategy objects. A Strategy object is plugged into some kind of master or owner object to implement an algorithm or decision. The Strategy object often depends on data in the master object; the Strategy object may not have any data of its own. We often design strategy classes to follow the Flyweight design pattern so we can avoid internal storage in the strategy instance. All values can be provided to a Strategy object as method argument values. In some cases, a strategy object can be stateless; in this instance, it is more a collection of method functions than anything else.

In the following examples, we'll show both stateless and stateful strategy class definitions. We'll start...

Some additional class definitions

As noted previously, a player has two strategies: one for betting and one for playing their hand. Each Player instance has a sequence of interactions with a larger simulation engine. We'll call the larger engine the Table class.

The Table class requires the following sequence of events by the Player instances:

  1. The player must place an initial, or ante, bet based on the betting strategy.
  2. The player will then receive a hand of cards.
  3. If the hand is splittable, the player must decide whether to split it or not based on their game strategy. This can create additional Hand instances. In some casinos, the additional hands are also splittable.
  4. For each Hand instance, the player must decide to hit, double, or stand based on their game strategy.
  5. The player will then receive payouts, and they must update their betting strategy based on their wins and...

Multi-strategy __init__()

We may have objects that are created from a variety of sources. For example, we might need to clone an object as part of creating a memento, or freeze an object so that it can be used as the key of a dictionary or placed into a set; this is the idea behind the set and frozenset built-in classes.

We'll look at two design patterns that offer multiple ways to build an object. One design pattern uses a complex __init__() method with multiple strategies for initialization. This leads to designing the __init__() method with a number of optional parameters. The other common design pattern involves creating multiple static or class-level methods, each with a distinct definition.

Defining an overloaded __init__() method can be confusing to mypy, because the parameters may have distinct value types. This is solved by using the @overload decorator...

Yet more __init__() techniques

We'll take a look at a few other, more advanced __init__() techniques. These aren't quite so universally useful as the techniques in the previous sections.

The following is a definition for the Player class that uses two Strategy objects and a table object. This shows an unpleasant-looking __init__() method:

class Player:

def __init__(
self,
table: Table,
bet_strategy: BettingStrategy,
game_strategy: GameStrategy
) -> None:
self.bet_strategy = bet_strategy
self.game_strategy = game_strategy
self.table = table

def game(self):
self.table.place_bet(self.bet_strategy.bet())
self.hand = self.table.get_hand()
if self.table.can_insure(self.hand):
if self.game_strategy.insurance(self.hand):
self.table.insure(self.bet_strategy.bet())
...

Summary

In this chapter, we have reviewed the various design alternatives of the __init__() method. The __init__() method is how objects are created, and it sets the initial state of an object.

We've looked at how all Python objects are subclasses of a common parent, the object class, and how the default __init__() method for the object class works. This consideration leads to two design strategies for placement of the __init__() method:

  • We can define a common __init__() method for all subclasses of a hierarchy. This can lead to using a factory function, separate from the __init__() method, to help initialize objects correctly.
  • We can push the __init__() method into each individual subclass of a complex hierarchy, and how this changes the design of classes.

After looking at building individual objects, we looked at how we can ...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Extend core OOP techniques to increase integration of classes created with Python
  • Explore a variety of Python libraries for handling persistence and object serialization
  • Learn alternative approaches for solving programming problems with different attributes to address your problem domain

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.

Who is this book 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.

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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 14, 2019
Length: 770 pages
Edition : 2nd
Language : English
ISBN-13 : 9781789531404
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jun 14, 2019
Length: 770 pages
Edition : 2nd
Language : English
ISBN-13 : 9781789531404
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
AU$16.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 6,500+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$149.99 billed annually
Feature tick icon Unlimited access to Packt's library of 6,500+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just AU$5 each
Feature tick icon Exclusive print discounts
$209.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 6,500+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just AU$5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total AU$ 185.97
Mastering Object-Oriented Python
AU$60.99
Python 3 Object-Oriented Programming
AU$63.99
Expert Python Programming
AU$60.99
Total AU$ 185.97 Stars icon
Visually different images

Table of Contents

20 Chapters
Preliminaries, Tools, and Techniques Chevron down icon Chevron up icon
The __init__() Method Chevron down icon Chevron up icon
Integrating Seamlessly - Basic Special Methods Chevron down icon Chevron up icon
Attribute Access, Properties, and Descriptors Chevron down icon Chevron up icon
The ABCs of Consistent Design Chevron down icon Chevron up icon
Using Callables and Contexts Chevron down icon Chevron up icon
Creating Containers and Collections Chevron down icon Chevron up icon
Creating Numbers Chevron down icon Chevron up icon
Decorators and Mixins - Cross-Cutting Aspects Chevron down icon Chevron up icon
Serializing and Saving - JSON, YAML, Pickle, CSV, and XML Chevron down icon Chevron up icon
Storing and Retrieving Objects via Shelve Chevron down icon Chevron up icon
Storing and Retrieving Objects via SQLite Chevron down icon Chevron up icon
Transmitting and Sharing Objects Chevron down icon Chevron up icon
Configuration Files and Persistence Chevron down icon Chevron up icon
Design Principles and Patterns Chevron down icon Chevron up icon
The Logging and Warning Modules Chevron down icon Chevron up icon
Designing for Testability Chevron down icon Chevron up icon
Coping with the Command Line Chevron down icon Chevron up icon
Module and Package Design Chevron down icon Chevron up icon
Quality and Documentation Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8
(4 Ratings)
5 star 50%
4 star 0%
3 star 25%
2 star 25%
1 star 0%
Daniel J. Snipes Aug 17, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I found this book to be insightful and easy to read. Steven Lott certainly has the rare talent of combing technical rigor with a certain level of playful eloquence. This is a great introduction to object oriented programming (OOP) paradigm in Python 3. This is both a useful introductory text for software engineers as well as a reference text for a more seasoned professional.
Amazon Verified review Amazon
Cesar Trejo Mar 05, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Muy buen producto 👍
Amazon Verified review Amazon
Amazon Customer Feb 27, 2023
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Pretty good text but code needs to formatted better. Most code has wraparound that makes it hard to read. Page 525 has serious formatting problem on code. However, because of the age of this edition, few are going to buy it now anyway.
Amazon Verified review Amazon
Prooffreader Feb 09, 2020
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
I get that it's useful to have an example class used throughout the book. But I think the blackjack-playing class was a little to obscure, it's not really a use case that a real programmer will come across (and gambling and games of gambling are of particularly little interest to me), and having the unicode symbols all over the place for the different suits (♠ ♥ ♣ ♦) instead of text is visually very distracting. I would have rathered follow along with a "real" program, even if its particular use case wasn't one I commonly use. A lot of references are made to the previous Python OOP book written by the author, so I bought it also even though only about 15% of it was new information to me. Despite these caveats, the author knows her stuff very well. Final note: this is a huge tome, extremely thick and difficuly to maneuver and weighs a ton.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.