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
Explore 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
Python Object-Oriented Programming
Python Object-Oriented Programming

Python Object-Oriented Programming: Build robust and maintainable object-oriented Python applications and libraries , Fourth Edition

Arrow left icon
Profile Icon Steven F. Lott Profile Icon Dusty Phillips
Arrow right icon
€8.99 €27.99
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.9 (33 Ratings)
eBook Jul 2021 714 pages 4th Edition
eBook
€8.99 €27.99
Paperback
€35.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Steven F. Lott Profile Icon Dusty Phillips
Arrow right icon
€8.99 €27.99
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.9 (33 Ratings)
eBook Jul 2021 714 pages 4th Edition
eBook
€8.99 €27.99
Paperback
€35.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€8.99 €27.99
Paperback
€35.99
Subscription
Free Trial
Renews at €18.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

Python Object-Oriented Programming

Object-Oriented Design

In software development, design is often considered as the step that's done before programming. This isn't true; in reality, analysis, programming, and design tend to overlap, combine, and interweave. Throughout this book, we'll be covering a mixture of design and programming issues without trying to parse them into separate buckets. One of the advantages of a language like Python is the ability to express the design clearly.

In this chapter, we will talk a little about how we can move from a good idea toward writing software. We'll create some design artifacts – like diagrams – that can help clarify our thinking before we start writing code. We'll cover the following topics:

  • What object-oriented means
  • The difference between object-oriented design and object-oriented programming
  • The basic principles of object-oriented design
  • Basic Unified Modeling Language (UML) and...

Introducing object-oriented

Everyone knows what an object is: a tangible thing that we can sense, feel, and manipulate. The earliest objects we interact with are typically baby toys. Wooden blocks, plastic shapes, and over-sized puzzle pieces are common first objects. Babies learn quickly that certain objects do certain things: bells ring, buttons are pressed, and levers are pulled.

The definition of an object in software development is not terribly different. Software objects may not be tangible things that you can pick up, sense, or feel, but they are models of something that can do certain things and have certain things done to them. Formally, an object is a collection of data and associated behaviors.

Considering what an object is, what does it mean to be object-oriented? In the dictionary, oriented means directed toward. Object-oriented programming means writing code directed toward modeling objects. This is one of many techniques used for...

Objects and classes

An object is a collection of data with associated behaviors. How do we differentiate between types of objects? Apples and oranges are both objects, but it is a common adage that they cannot be compared. Apples and oranges aren't modeled very often in computer programming, but let's pretend we're doing an inventory application for a fruit farm. To facilitate this example, we can assume that apples go in barrels and oranges go in baskets.

The problem domain we've uncovered so far has four kinds of objects: apples, oranges, baskets, and barrels. In object-oriented modeling, the term used for a kind of object is class. So, in technical terms, we now have four classes of objects.

It's important to understand the difference between an object and a class. Classes describe related objects. They are like blueprints for creating an object. You might have three oranges sitting on the table in front of you. Each orange...

Specifying attributes and behaviors

We now have a grasp of some basic object-oriented terminology. Objects are instances of classes that can be associated with each other. A class instance is a specific object with its own set of data and behaviors; a specific orange on the table in front of us is said to be an instance of the general class of oranges.

The orange has a state, for example, ripe or raw; we implement the state of an object via the values of specific attributes. An orange also has behaviors. By themselves, oranges are generally passive. State changes are imposed on them. Let's dive into the meaning of those two words, state and behaviors.

Data describes object state

Let's start with data. Data represents the individual characteristics of a certain object; its current state. A class can define specific sets of characteristics that are part of all objects that are members of that class. Any specific object can have different data values...

Hiding details and creating the public interface

The key purpose of modeling an object in object-oriented design is to determine what the public interface of that object will be. The interface is the collection of attributes and methods that other objects can access to interact with that object. Other objects do not need, and in some languages are not allowed, to access the internal workings of the object.

A common real-world example is the television. Our interface to the television is the remote control. Each button on the remote control represents a method that can be called on the television object. When we, as the calling object, access these methods, we do not know or care if the television is getting its signal from a cable connection, a satellite dish, or an internet-enabled device. We don't care what electronic signals are being sent to adjust the volume, or whether the sound is destined for speakers or headphones. If we open the television to access its...

Composition

So far, we have learned to design systems as a group of interacting objects, where each interaction involves viewing objects at an appropriate level of abstraction. But we don't yet know how to create these levels of abstraction. There are a variety of ways to do this; we'll discuss some advanced design patterns in Chapters 10, 11, and 12. But even most design patterns rely on two basic object-oriented principles known as composition and inheritance. Composition is simpler, so let's start with that.

Composition is the act of collecting several objects together to create a new one. Composition is usually a good choice when one object is part of another object. We've already seen a first hint of composition when talking about cars. A fossil-fueled car is composed of an engine, transmission, starter, headlights, and windshield, among numerous other parts. The engine, in turn, is composed of pistons, a crank shaft, and valves. In this example,...

Inheritance

We discussed three types of relationships between objects: association, composition, and aggregation. However, we have not fully specified our chess set, and these tools don't seem to give us all the power we need. We discussed the possibility that a player might be a human or it might be a piece of software featuring artificial intelligence. It doesn't seem right to say that a player is associated with a human, or that the artificial intelligence implementation is part of the player object. What we really need is the ability to say that Deep Blue is a player, or that Gary Kasparov is a player.

The is a relationship is formed by inheritance. Inheritance is the most famous, well-known, and overused relationship in object-oriented programming. Inheritance is sort of like a family tree. Dusty Phillips is one of this book's authors.

His grandfather's last name was Phillips, and his father inherited...

Case study

Our case study will span many of the chapters of this book. We'll be examining a single problem closely from a variety of perspectives. It's very important to look at alternative designs and design patterns; more than once, we'll point out that there's no single right answer: there are a number of good answers. Our intent here is to provide a realistic example that involves realistic depth and complications and leads to difficult trade-off decisions. Our goal is to help the reader apply object-oriented programming and design concepts. This means choosing among the technical alternatives to create something useful.

This first part of the case study is an overview of the problem and why we're tackling it. This background will cover a number of aspects of the problem to set up the design and construction of solutions in later chapters. Part of this overview will include some UML diagrams to capture elements of the problem to be solved. These will...

Recall

Some key points in this chapter:

  • Analyzing problem requirements in an object-oriented context
  • How to draw Unified Modeling Language (UML) diagrams to communicate how the system works
  • Discussing object-oriented systems using the correct terminology and jargon
  • Understanding the distinction between class, object, attribute, and behavior
  • Some OO design techniques are used more than others. In our case study example, we focused on a few:
    • Encapsulating features into classes
    • Inheritance to extend a class with new features
    • Composition to build a class from component objects

Exercises

This is a practical book. As such, we're not assigning a bunch of fake object-oriented analysis problems to create designs for you to analyze and design. Instead, we want to give you some ideas that you can apply to your own projects. If you have previous object-oriented experience, you won't need to put much effort into this chapter. However, they are useful mental exercises if you've been using Python for a while, but have never really cared about all that class stuff.

First, think about a recent programming project you've completed. Identify the most prominent object in the design. Try to think of as many attributes for this object as possible. Did it have the following: Color? Weight? Size? Profit? Cost? Name? ID number? Price? Style?

Think about the attribute types. Were they primitives or classes? Were some of those attributes actually behaviors in disguise? Sometimes, what looks like data is actually calculated from other data on the object...

Summary

In this chapter, we took a whirlwind tour through the terminology of the object-oriented paradigm, focusing on object-oriented design. We can separate different objects into a taxonomy of different classes and describe the attributes and behaviors of those objects via the class interface. Abstraction, encapsulation, and information hiding are highly-related concepts. There are many different kinds of relationships between objects, including association, composition, and inheritance. UML syntax can be useful for fun and communication.

In the next chapter, we'll explore how to implement classes and methods in Python.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Build an intuitive understanding of object-oriented design, from introductory to mature programs
  • Learn the ins and outs of Python syntax, libraries, and best practices
  • Examine a machine-learning case study at the end of each chapter

Description

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.

Who is this book for?

If you are new to object-oriented programming techniques, or if you have basic Python skills and wish to learn how and when to correctly apply OOP principles in Python, this is the book for you. Moreover, if you are an object-oriented programmer coming from other languages or seeking a leg up in the new world of Python, you will find this book a useful introduction to Python. Minimal previous experience with Python is necessary.

What you will learn

  • Implement objects in Python by creating classes and defining methods
  • Extend class functionality using inheritance
  • Use exceptions to handle unusual situations cleanly
  • Understand when to use object-oriented features, and more importantly, when not to use them
  • Discover several widely used design patterns and how they are implemented in Python
  • Uncover the simplicity of unit and integration testing and understand why they are so important
  • Learn to statically type check your dynamic code
  • Understand concurrency with asyncio and how it speeds up programs

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 02, 2021
Length: 714 pages
Edition : 4th
Language : English
ISBN-13 : 9781801075237
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 : Jul 02, 2021
Length: 714 pages
Edition : 4th
Language : English
ISBN-13 : 9781801075237
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ 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
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ 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 €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ 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 €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 109.97
Clean Code in Python
€37.99
Learn Python Programming, 3rd edition
€35.99
Python Object-Oriented Programming
€35.99
Total 109.97 Stars icon
Banner background image

Table of Contents

16 Chapters
Object-Oriented Design Chevron down icon Chevron up icon
Objects in Python Chevron down icon Chevron up icon
When Objects Are Alike Chevron down icon Chevron up icon
Expecting the Unexpected Chevron down icon Chevron up icon
When to Use Object-Oriented Programming Chevron down icon Chevron up icon
Abstract Base Classes and Operator Overloading Chevron down icon Chevron up icon
Python Data Structures Chevron down icon Chevron up icon
The Intersection of Object-Oriented and Functional Programming Chevron down icon Chevron up icon
Strings, Serialization, and File Paths Chevron down icon Chevron up icon
The Iterator Pattern Chevron down icon Chevron up icon
Common Design Patterns Chevron down icon Chevron up icon
Advanced Design Patterns Chevron down icon Chevron up icon
Testing Object-Oriented Programs Chevron down icon Chevron up icon
Concurrency Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.9
(33 Ratings)
5 star 48.5%
4 star 9.1%
3 star 27.3%
2 star 15.2%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Matthew Oct 05, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Dear Packt,I have found an awesome content inside the book. But the quality of the binding I received from the publisher is unacceptable. Its not the first time, i encountered the issue.Pl don’t spoil the journey of reading awesome content just because of your poor quality of presentation.BR,Matthew
Amazon Verified review Amazon
Tharnid Jul 28, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
About this book:This is a detailed explanation of the Object-Oriented Programming (OOP) using the Python programming language but the concepts translate to other programming languages as well (C#, Java, etc)Who the book is for:This is not a beginner's book on OOP and the reader will need some solid understanding of the OOP concepts (abstraction, classes, encapsulation, inheritance, objects, polymorphism, etc). The concepts in this book transcend Python so it would a great reference for any OOP programmer. There is even a chapter on when to use OOP. It also has a chapter dedicated to where Functional and OOP cross paths in Python which was very interesting as well.ConclusionI enjoyed the depth provided in this book and refreshed my knowledge of OOP. Solid book on OOP. Do yourself a favor and purchase this book for reference at the minimum. Your career will thank you
Amazon Verified review Amazon
Carlos Cardona May 04, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Despite its unfortunate initialism, POOP is money well spent. If you already have some experience with python, this book will take you to a next level in Python programming, and teach you along the way some good practices, how to improve readability and some common useful design patterns. Thumbs up!
Amazon Verified review Amazon
Amazon Customer Jul 26, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Before I reviewed this book, I was only vaguely aware of the fact that Python had objects - my only experience with object-oriented programming had been in Java and C++. So once I dug into the book, I was surprised to see the depth that Python offers in this domain!The book explains object-oriented design starting from its core concepts, so you don't need any prior background in a language like Java or C++ (though it can help). Furthermore, it shows how these ideas apply to Python, and will help you build your existing Python skillset further. It takes some time to go over the basic data structures of Python while also getting into the complexities of objects in the language. All of this content is supplemented with plenty of Python code examples, which will only help to reinforce what you read in each chapter.This book helped reinforce my understanding of Python, helping me become more familiar with the various features and syntax in the language that I may not have seen before. Additionally, it helped show me how to implement object-oriented design in a different language, which aided my overall comprehension of that programming paradigm. If you have basic experience in Python and wish to explore a different programming paradigm in the language, then this book will be the perfect fit to help you push in the direction of object-oriented programming!
Amazon Verified review Amazon
Vlad Bezden Nov 27, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Like all books by Steven F. Lott, this book is no exception. I read all books by Steven, and I always learn something from them. Besides teaching how to write OOP and use it, this book explains how to write OOP code using modern data types like NamedTuple, TypedDict, tuples, etc.As always, I learned excellent tips and techniques on how to write nice tests. There are also very interesting chapters on Concurrency and Design Patterns.My only complaint is the type of examples provided in this book. Examples are complicated, and they are based on machine learning data, sailing data, etc. So to understand an example, sometimes you need to understand the domain knowledge of the example. I wish examples were simpler to get a concept and then maybe provide more complex examples.Steven is not biased to OOP in this book, but he also explains the difference between OOP and functional programming and teaches where OOP would be the best fit.In any way, it's an excellent book, and I learned a lot from it, and I use it as a reference if I need to find an answer to my questions.
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.