Python 3 Object-oriented Programming - Second Edition
4/5
()
About this ebook
Unleash the power of Python 3 objects
About This Book- Stop writing scripts and start architecting programs
- Learn the latest Python syntax and libraries
- A practical, hands-on tutorial that teaches you all about abstract design patterns and how to implement them in Python 3
If you're new to object-oriented programming techniques, or if you have basic Python skills and wish to learn in depth how and when to correctly apply object-oriented programming in Python to design software, this is the book for you.
What You Will Learn- Implement objects in Python by creating classes and defining methods
- Separate related objects into a taxonomy of classes and describe the properties and behaviors of those objects via the class interface
- Extend class functionality using inheritance
- Understand when to use object-oriented features, and more importantly when not to use them
- Discover what design patterns are and why they are different in Python
- Uncover the simplicity of unit testing and why it's so important in Python
- Grasp common concurrency techniques and pitfalls in Python 3
- Exploit object-oriented programming in key Python technologies such as Kivy and Django.
- Object-oriented programming concurrently with asyncio
Python 3 is more versatile and easier to use than ever. It runs on all major platforms in a huge array of use cases. Coding in Python minimizes development time and increases productivity in comparison to other languages. Clean, maintainable code is easy to both read and write using Python's clear, concise syntax.
Object-oriented programming is a popular design paradigm in which data and behaviors are encapsulated in such a way that they can be manipulated together. Many modern programming languages utilize the powerful concepts behind object-oriented programming and Python is no exception.
Starting with a detailed analysis of object-oriented analysis and design, you will use the Python programming language to clearly grasp key concepts from the object-oriented paradigm. This book fully explains classes, data encapsulation, inheritance, polymorphism, abstraction, and exceptions with an emphasis on when you can use each principle to develop well-designed software.
You'll get an in-depth analysis of many common object-oriented design patterns that are more suitable to Python's unique style. This book will not just teach Python syntax, but will also build your confidence in how to program.
You will also learn how to create maintainable applications by studying higher level design patterns. Following this, you'll learn the complexities of string and file manipulation, and how Python distinguishes between binary and textual data. Not one, but two very powerful automated testing systems will be introduced in the book. After you discover the joy of unit testing and just how easy it can be, you'll study higher level libraries such as database connectors and GUI toolkits and learn how they uniquely apply object-oriented principles. You'll learn how these principles will allow you to make greater use of key members of the Python eco-system such as Django and Kivy.
This new edition includes all the topics that made Python 3 Object-oriented Programming an instant Packt classic. It's also packed with updated content to reflect recent changes in the core Python library and covers modern third-party packages that were not available on the Python 3 platform when the book was first published.
Style and approachThroughout the book you will learn key object-oriented programming techniques demonstrated by comprehensive case studies in the context of a larger project.
Dusty Phillips
Dusty Phillips is a Canadian software developer and author currently living in Seattle, Washington. He has been active in the open source community for a decade and a half and programming in Python for nearly all of it. He cofounded the popular Puget Sound Programming Python meetup group; drop by and say hi if you're in the area. Python 3 Object Oriented Programming, Packt Publishing, was the first of his books. He has also written Creating Apps In Kivy, O'Reilly, the mobile Python library, and self-published Hacking Happy, a journey to mental wellness for the technically inclined. He was hospitalized for suicidal tendencies shortly after the first edition of this book was published and has been an outspoken proponent for positive mental health ever since.
Related to Python 3 Object-oriented Programming - Second Edition
Related ebooks
Mastering Objectoriented Python Rating: 5 out of 5 stars5/5Python 3 Object Oriented Programming Rating: 4 out of 5 stars4/5Python Essentials Rating: 5 out of 5 stars5/5Learning Python Rating: 5 out of 5 stars5/5Functional Python Programming Rating: 0 out of 5 stars0 ratingsPython: Programming for Intermediates: Learn the Fundamentals of Python in 7 Days Rating: 4 out of 5 stars4/5Mastering Python Design Patterns Rating: 0 out of 5 stars0 ratingsModular Programming with Python Rating: 0 out of 5 stars0 ratingsLearning Python Network Programming Rating: 5 out of 5 stars5/5Python Parallel Programming Cookbook Rating: 5 out of 5 stars5/5Python Data Analysis - Second Edition Rating: 0 out of 5 stars0 ratingsLearning Python Application Development Rating: 0 out of 5 stars0 ratingsEssential Python 3 Rating: 0 out of 5 stars0 ratingsPython In - Depth: Use Python Programming Features, Techniques, and Modules to Solve Everyday Problems Rating: 0 out of 5 stars0 ratingsPython for Beginners: Learn the Fundamentals of Computer Programming Rating: 0 out of 5 stars0 ratingsLearning Object-Oriented Programming Rating: 4 out of 5 stars4/5Hands-On Data Analysis with Pandas: Efficiently perform data collection, wrangling, analysis, and visualization using Python Rating: 0 out of 5 stars0 ratingsLearning Jupyter Rating: 3 out of 5 stars3/5Flask By Example Rating: 0 out of 5 stars0 ratingsPython For Data Science Rating: 0 out of 5 stars0 ratingsParallel Programming with Python Rating: 0 out of 5 stars0 ratingsUseful Python Rating: 0 out of 5 stars0 ratingsPython Unlocked Rating: 0 out of 5 stars0 ratingsAdvance Core Python Programming: Begin your Journey to Master the World of Python (English Edition) Rating: 4 out of 5 stars4/5Python for Beginners: Learn It as Easy as Pie Rating: 0 out of 5 stars0 ratingsPython for Beginners: A Crash Course to Learn Python Programming in 1 Week Rating: 0 out of 5 stars0 ratings
Programming For You
Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsPython Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5HTML in 30 Pages Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Beginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5Microsoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsiPhone For Dummies Rating: 0 out of 5 stars0 ratingsHacking Electronics: Learning Electronics with Arduino and Raspberry Pi, Second Edition Rating: 0 out of 5 stars0 ratingsSQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Teach Yourself C++ Rating: 4 out of 5 stars4/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsAlgorithms For Dummies Rating: 4 out of 5 stars4/5C Programming Language Essentials Rating: 4 out of 5 stars4/5
Reviews for Python 3 Object-oriented Programming - Second Edition
4 ratings1 review
- Rating: 5 out of 5 stars5/5
Mar 4, 2019
Muy buen libro, para aprender Python como primer lenguaje de programación, está lo básico, muy buenos ejemplos (a veces chistosos, lo que lo hace bastante interesante), y además permite abrir las puertas para indagar y fortalacer más la sabiduría haciendo referencias a fuentes, como la documentación que tiene el sitio web de desarrollo de Python. Muy buen libro, lo recomiendo definitivamente.
Book preview
Python 3 Object-oriented Programming - Second Edition - Dusty Phillips
Table of Contents
Python 3 Object-oriented Programming Second Edition
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
Introduction to the second edition
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Object-oriented Design
Introducing object-oriented
Objects and classes
Specifying attributes and behaviors
Data describes objects
Behaviors are actions
Hiding details and creating the public interface
Composition
Inheritance
Inheritance provides abstraction
Multiple inheritance
Case study
Exercises
Summary
2. Objects in Python
Creating Python classes
Adding attributes
Making it do something
Talking to yourself
More arguments
Initializing the object
Explaining yourself
Modules and packages
Organizing the modules
Absolute imports
Relative imports
Organizing module contents
Who can access my data?
Third-party libraries
Case study
Exercises
Summary
3. When Objects Are Alike
Basic inheritance
Extending built-ins
Overriding and super
Multiple inheritance
The diamond problem
Different sets of arguments
Polymorphism
Abstract base classes
Using an abstract base class
Creating an abstract base class
Demystifying the magic
Case study
Exercises
Summary
4. Expecting the Unexpected
Raising exceptions
Raising an exception
The effects of an exception
Handling exceptions
The exception hierarchy
Defining our own exceptions
Case study
Exercises
Summary
5. When to Use Object-oriented Programming
Treat objects as objects
Adding behavior to class data with properties
Properties in detail
Decorators – another way to create properties
Deciding when to use properties
Manager objects
Removing duplicate code
In practice
Case study
Exercises
Summary
6. Python Data Structures
Empty objects
Tuples and named tuples
Named tuples
Dictionaries
Dictionary use cases
Using defaultdict
Counter
Lists
Sorting lists
Sets
Extending built-ins
Queues
FIFO queues
LIFO queues
Priority queues
Case study
Exercises
Summary
7. Python Object-oriented Shortcuts
Python built-in functions
The len() function
Reversed
Enumerate
File I/O
Placing it in context
An alternative to method overloading
Default arguments
Variable argument lists
Unpacking arguments
Functions are objects too
Using functions as attributes
Callable objects
Case study
Exercises
Summary
8. Strings and Serialization
Strings
String manipulation
String formatting
Escaping braces
Keyword arguments
Container lookups
Object lookups
Making it look right
Strings are Unicode
Converting bytes to text
Converting text to bytes
Mutable byte strings
Regular expressions
Matching patterns
Matching a selection of characters
Escaping characters
Matching multiple characters
Grouping patterns together
Getting information from regular expressions
Making repeated regular expressions efficient
Serializing objects
Customizing pickles
Serializing web objects
Case study
Exercises
Summary
9. The Iterator Pattern
Design patterns in brief
Iterators
The iterator protocol
Comprehensions
List comprehensions
Set and dictionary comprehensions
Generator expressions
Generators
Yield items from another iterable
Coroutines
Back to log parsing
Closing coroutines and throwing exceptions
The relationship between coroutines, generators, and functions
Case study
Exercises
Summary
10. Python Design Patterns I
The decorator pattern
A decorator example
Decorators in Python
The observer pattern
An observer example
The strategy pattern
A strategy example
Strategy in Python
The state pattern
A state example
State versus strategy
State transition as coroutines
The singleton pattern
Singleton implementation
The template pattern
A template example
Exercises
Summary
11. Python Design Patterns II
The adapter pattern
The facade pattern
The flyweight pattern
The command pattern
The abstract factory pattern
The composite pattern
Exercises
Summary
12. Testing Object-oriented Programs
Why test?
Test-driven development
Unit testing
Assertion methods
Reducing boilerplate and cleaning up
Organizing and running tests
Ignoring broken tests
Testing with py.test
One way to do setup and cleanup
A completely different way to set up variables
Skipping tests with py.test
Imitating expensive objects
How much testing is enough?
Case study
Implementing it
Exercises
Summary
13. Concurrency
Threads
The many problems with threads
Shared memory
The global interpreter lock
Thread overhead
Multiprocessing
Multiprocessing pools
Queues
The problems with multiprocessing
Futures
AsyncIO
AsyncIO in action
Reading an AsyncIO future
AsyncIO for networking
Using executors to wrap blocking code
Streams
Executors
Case study
Exercises
Summary
Index
Python 3 Object-oriented Programming Second Edition
Python 3 Object-oriented Programming Second Edition
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: July 2010
Second edition: August 2015
Production reference: 1130815
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78439-878-1
www.packtpub.com
Credits
Author
Dusty Phillips
Reviewers
AMahdy AbdElAziz
Grigoriy Beziuk
Krishna Bharadwaj
Justin Cano
Anthony Petitbois
Claudio Rodriguez
Commissioning Editor
Edward Gordon
Acquisition Editors
Indrajit Das
Rebecca Pedley
Greg Wild
Content Development Editors
Divij Kotian
Arvind Koul
Anila Vincent
Technical Editor
Siddhi Rane
Copy Editor
Janbal Dharmaraj
Project Coordinator
Neha Bhatnagar
Proofreader
Safis Editing
Indexer
Hemangini Bari
Graphics
Sheetal Aute
Jason Monteiro
Production Coordinator
Komal Ramchandani
Cover Work
Komal Ramchandani
About the Author
Dusty Phillips is a Canadian software developer and author currently living in Seattle, Washington. He has been active in the open source community for a decade and a half and programming in Python for nearly all of it. He cofounded the popular Puget Sound Programming Python meetup group; drop by and say hi if you're in the area.
Python 3 Object Oriented Programming, Packt Publishing, was the first of his books. He has also written Creating Apps In Kivy, O'Reilly, the mobile Python library, and self-published Hacking Happy, a journey to mental wellness for the technically inclined. He was hospitalized for suicidal tendencies shortly after the first edition of this book was published and has been an outspoken proponent for positive mental health ever since.
About the Reviewers
AMahdy AbdElAziz has more than 8 years of experience in software engineering using several languages and frameworks. Over the last 5 years, he has focused on Android and mobile development, including cross-platform tools, and Android internals, such as building custom ROMs and customizing AOSP for embedded devices.
He is currently teaching Python at Information Technology Institution. You can visit his website, http://www.amahdy.net/, to find out more about him.
Grigoriy Beziuk is a former CIO of Crowdage Foundation, acting as an independent software developer as this book was being written. He has worked with a wide variety of programming languages and technologies, including different versions of Python in different environments, ranging from purely scientific ones to modern production-scale web development issues.
I would like to thank my mom, Larisa Beziuk, for giving me the gift of life; all of my teachers and friends for making my life more interesting; and all of my beloved ones, former and present for… well, everything.
Krishna Bharadwaj is the cofounder of SMERGERS (https://www.smergers.com/), a Fintech start-up helping small and medium businesses raise capital from investors and different financial institutions. In the past, he has worked with early stage start-ups such as BlockBeacon (Santa Monica) and PricePoint (CA) and large organizations such as National Instruments, Bangalore, and Google, New York. Krishna got introduced to Python and FOSS during his college days and has continued to use it extensively in his personal projects and also professionally at work. Because of his liking for teaching and mentoring, he visits different universities, conducting workshops whenever he gets an opportunity.
He holds a master's degree in computer science from the University of Southern California, Los Angeles, and a bachelor's degree in information science and engineering from the BMS College of Engineering, Bangalore. He can be reached through his e-mail, <[email protected]>, or his website, http://www.krishnabharadwaj.info/.
Justin Cano is a recent graduate from the University of California, Riverside, with a BS in computer engineering and is currently working as a software engineer in the Silicon Valley area with hopes of moving to a big tech company such as Google or Apple.
He first started programming in the sixth grade, creating small, primitive websites in HTML and CSS. He started to learn computer science theory and C++ in his first year at UC Riverside and then started learning Python in his third year.
Justin admits that at first, he wasn't immediately attracted to Python, since abstractions between C++ and Python are very different. It wasn't until he began to express more of an interest in coding contests and challenges that he began to show interest in Python, mainly because he feels that the readability and elegance of the Python syntax allows him to quickly and more naturally turn ideas and thought processes into Python code. He now writes Python code regularly, often to create mock-ups or prototypes of software applications before moving on to a more domain-specific language.
I would like to thank the author for taking the time to write this book as I have received a lot of valuable insights and information on the Python language and design patterns. This book has strengthened my understanding of Python, and I believe that I am now a more knowledgeable Python programmer.
Anthony Petitbois is an online architect in the video game industry with 13 years of professional experience in operations and development and more than 20 years of software development experience. He is passionate about new technologies and loves to take creative approaches to solve complex problems.
In his spare time, he learns new languages and new platforms, plays video games, and spends time with his family in the beautiful region of British Columbia, Canada, where he now lives after emigrating from France in 2009.
Claudio Rodriguez started working on PLCs for GE, but his main goal has always been research and development and turning dreams into reality. This made him move from automation engineering to software engineering and the structured way of software, OOD; the remote team working from the comfort of his computer was just too powerful not to take advantage of. During his master's, he got to learn the proper place to look for resources and found a friend in books and research papers and conferences. Eventually, he started working on a system to control an electric arc furnace, but the needs of his clients moved him into taking further control of technology. He has a deep love for complex AI and can be seen surrounded by papers, books, and a computer to test things, but he keeps things real by delivering beautiful and dynamic applications for his customers.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
Introduction to the second edition
I have a confession to make. When I wrote the first edition of this book, I didn't have a clue what I was doing. I thought I knew Python and I thought I knew how to write. I quickly learned that this was false. Luckily, I became adept at both by finishing the book!
I was so afraid that people wouldn't like Python 3 Object Oriented Programming that I skipped Pycon for two years straight. After a couple dozen positive reviews, my confidence was boosted and I finally attended Pycon 2012 in Santa Clara. I soon discovered that nobody had ever heard of me or my book. So much for arrogance!
I was also afraid to reread the book after completing it. So while it has received many accolades, the copy on my shelf has remained firmly shut, save for when I open it for reference to answer a reader's query. In preparing this second edition, I was finally forced to face my demons. To my surprise and joy, I discovered that the book I wrote five years ago was both accurate and enjoyable, just as many reviewers had suggested.
Shortly after that initial rereading, I got my first ever negative review on Amazon. It would have been devastating had I read it directly after completing the project. Fortunately, four years of good reviews and my own confidence in the writing allowed me to ignore the vitriol and take the remainder as constructive feedback. The truth is many of the flaws the reviewer had pointed out were features at the time the book was originally published. Python 3 Object Oriented Programming was showing its age, and it was clearly time for an update. You're holding the result in your hands (or flipping through it on your e-reader).
I've often wondered why authors describe in detail what has changed between the editions of a technical book. I mean, seriously, how many people reading this second edition have read the first one? As with software versions, you safely assume the latest edition is the best, and you don't really care about the project's history. And yet, this project has consumed so much of my life over the past year that I can't leave without a few words about how much better the book has become.
The original book was a little disorganized. Many chapters flowed directly into the next one, but there were a few key places where the topic change was jarring, or worse, irrelevant. The two chapters preceding the discussions about design patterns have been reorganized, reversed, and split into three chapters that flow cleanly into the next topic.
I've also removed an entire chapter on third-party libraries for Python 3. This chapter made more sense when both the book and Python 3 were new. There were only a few libraries that had been ported to Python 3 and it was reasonable to have a best of breed discussion about each of them. However, I was unable to cover any of those topics in detail, and frankly, I could write an entire book on any one of them.
Finally, I've added an entire new chapter on concurrency. I struggled with this chapter and I can freely admit that it's not directly related to object-oriented programming. However, much like the chapter on unit testing, I think that understanding concurrency is an integral part of all programming and especially of object-oriented programming in the Python ecosystem. You are, of course, free to skip those chapters if you disagree (or until you discover a reason to change your mind).
Enjoy the book and your journey into the world of object-oriented programming.
Dusty Phillips
Preface
This book introduces the terminology of the object-oriented paradigm. It focuses on object-oriented design with step-by-step examples. It guides us from simple inheritance, one of the most useful tools in the object-oriented programmer's toolbox through exception handling to design patterns, an object-oriented way of looking at object-oriented concepts.
Along the way, we'll learn to integrate the object-oriented and not-so-object-oriented aspects of the Python programming language. We will learn the complexities of string and file manipulation, emphasizing (as Python 3 does) the difference between binary and textual data.
We'll then cover the joys of unit testing, using not one, but two unit testing frameworks. Finally, we'll explore, through Python's various concurrency paradigms, how to make objects work well together at the same time.
What this book covers
This book is loosely divided into four major parts. In the first four chapters, we will dive into the formal principles of object-oriented programming and how Python leverages them. In chapters 5 through 8, we will cover some of Python's idiosyncratic applications of these principles by learning how they are applied to a variety of Python's built-in functions. Chapters 9 through 11 cover design patterns, and the final two chapters discuss two bonus topics related to Python programming that may be of interest.
Chapter 1, Object-oriented Design, covers important object-oriented concepts. It deals mainly with terminology such as abstraction, classes, encapsulation, and inheritance. We also briefly look at UML to model our classes and objects.
Chapter 2, Objects in Python, discusses classes and objects and how they are used in Python. We will learn about attributes and behaviors on Python objects, and also the organization of classes into packages and modules. Lastly, we will see how to protect our data.
Chapter 3, When Objects Are Alike, gives us a more in-depth look into inheritance. It covers multiple inheritance and shows us how to extend built-ins. This chapter also covers how polymorphism and duck typing work in Python.
Chapter 4, Expecting the Unexpected, looks into exceptions and exception handling. We will learn how to create our own exceptions and how to use exceptions for program flow control.
Chapter 5, When to Use Object-oriented Programming, deals with creating and using objects. We will see how to wrap data using properties and restrict data access. This chapter also discusses the DRY principle and how not to repeat code.
Chapter 6, Python Data Structures, covers the object-oriented features of Python's built-in classes. We'll cover tuples, dictionaries, lists, and sets, as well as a few more advanced collections. We'll also see how to extend these standard objects.
Chapter 7, Python Object-oriented Shortcuts, as the name suggests, deals with time-savers in Python. We will look at many useful built-in functions such as method overloading using default arguments. We'll also see that functions themselves are objects and how this is useful.
Chapter 8, Strings and Serialization, looks at strings, files, and formatting. We'll discuss the difference between strings, bytes, and bytearrays, as well as various ways to serialize textual, object, and binary data to several canonical representations.
Chapter 9, The Iterator Pattern, introduces us to the concept of design patterns and covers Python's iconic implementation of the iterator pattern. We'll learn about list, set, and dictionary comprehensions. We'll also demystify generators and coroutines.
Chapter 10, Python Design Patterns I, covers several design patterns, including the decorator, observer, strategy, state, singleton, and template patterns. Each pattern is discussed with suitable examples and programs implemented in Python.
Chapter 11, Python Design Patterns II, wraps up our discussion of design patterns with coverage of the adapter, facade, flyweight, command, abstract, and composite patterns. More examples of how idiomatic Python code differs from canonical implementations are provided.
Chapter 12, Testing Object-oriented Programs, opens with why testing is so important in Python applications. It emphasizes test-driven development and introduces two different testing suites: unittest and py.test. Finally, it discusses mocking test objects and code coverage.
Chapter 13, Concurrency, is a whirlwind tour of Python's support (and lack thereof) of concurrency patterns. It discusses threads, multiprocessing, futures, and the new AsyncIO library.
Each chapter includes relevant examples and a case study that collects the chapter's contents into a working (if not complete) program.
What you need for this book
All the examples in this book rely on the Python 3 interpreter. Make sure you are not using Python 2.7 or earlier. At the time of writing, Python 3.4 was the latest release of Python. Most examples will work on earlier revisions of Python 3, but you are encouraged to use the latest version to minimize frustration.
All of the examples should run on any operating system supported by Python. If this is not the case, please report it as a bug.
Some of the examples need a working Internet connection. You'll probably want to have one of these for extracurricular research and debugging anyway!
In addition, some of the examples in this book rely on third-party libraries that do not ship with Python. These are introduced within the book at the time they are used, so you do not need to install them in advance. However, for completeness, here is a list:
pip
requests
pillow
bitarray
Who this book is for
This book specifically targets people who are new to object-oriented programming. It assumes you have basic Python skills. You'll learn object-oriented principles in depth. It is particularly useful for system administrator types who have used Python as a glue
language and would like to improve their programming skills.
If you are familiar with object-oriented programming in other languages, then this book will help you understand the idiomatic ways to apply your knowledge in the Python ecosystem.
Conventions
This book uses a variety of text styles to distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: We look up the class in the dictionary and store it in a variable named PropertyClass.
A block of code is set as follows:
def add_property(self):
property_type = get_valid_input(
What type of property?
,
(house
, apartment
)).lower()
payment_type = get_valid_input(
What payment type?
,
(purchase
, rental
)).lower()
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
def add_property(self): property_type = get_valid_input(
What type of property?
,
(house
, apartment
)).lower()
payment_type = get_valid_input(
What payment type?
,
(purchase
, rental
)).lower()
Any command-line input or output is written as follows:
>>> c1 = Contact(John A
, [email protected]
) >>> c2 = Contact(John B
, [email protected]
) >>> c3 = Contact(Jenna C
, [email protected]
) >>> [c.name for c in Contact.all_contacts.search('John')] ['John A', 'John B']
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: It will fail with a not enough arguments error similar to the one we received earlier when we forgot the self argument.
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
Chapter 1. Object-oriented Design
In software development, design is often considered as the step done before programming. This isn't true; in reality, analysis, programming, and design tend to overlap, combine, and interweave. In this chapter, we will cover the following topics:
What object-oriented means
The difference between object-oriented design and object-oriented programming
The basic principles of an object-oriented design
Basic Unified Modeling Language (UML) and when it isn't evil
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 press, and levers pull.
The definition of an object in software development is not terribly different. Software objects are not typically 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.
So, knowing what an object is, what does it mean to be object-oriented? Oriented simply means directed toward. So object-oriented means functionally directed towards modeling objects. This is one of the many techniques used for modeling complex systems by describing a collection of interacting objects via their data and behavior.
If you've read any hype, you've probably come across the terms object-oriented analysis, object-oriented design, object-oriented analysis and design, and object-oriented programming. These are all highly related concepts under the general object-oriented umbrella.
In fact, analysis, design, and programming are all stages of software development. Calling them object-oriented simply specifies what style of software development is being pursued.
Object-oriented analysis (OOA) is the process of looking at a problem, system, or task (that somebody wants to turn into an application) and identifying the objects and interactions between those objects. The analysis stage is all about what needs to be done.
The output of the analysis stage is a set of requirements. If we were to complete the analysis stage in one step, we would have turned a task, such as, I need a website, into a set of requirements. For example:
Website visitors need to be able to (italic represents actions, bold represents objects):
review our history
apply for jobs
browse, compare, and order products
In some ways, analysis is a misnomer. The baby we discussed earlier doesn't analyze the blocks and puzzle pieces. Rather, it will explore its environment, manipulate shapes, and see where they might fit. A better turn of phrase might be object-oriented exploration. In software development, the initial stages of analysis include interviewing customers, studying their processes, and eliminating possibilities.
Object-oriented design (OOD) is the process of converting such requirements into an implementation specification. The designer must name the objects, define the behaviors, and formally specify which objects can activate specific behaviors on other objects. The design stage is all about how things should be done.
The output of the design stage is an implementation specification. If we were to complete the design stage in a single step, we would have turned the requirements defined during object-oriented analysis into a set of classes and interfaces that could be implemented in (ideally) any object-oriented programming language.
Object-oriented programming (OOP) is the process of converting this perfectly defined design into a working program that does exactly what the CEO originally requested.
Yeah, right! It would be lovely if the world met this ideal and we could follow these stages one by one, in perfect order, like all the old textbooks told us to. As usual, the real world is much murkier. No matter how hard we try to separate these stages, we'll always find things that need further analysis while we're designing. When we're programming, we find features that need clarification in the design.
Most twenty-first century development happens in an iterative development model. In iterative development, a small part of the task is modeled, designed, and programmed, then the program is reviewed and expanded to improve each feature and include new features in a series of short development cycles.
The rest of this book is about object-oriented programming, but in this chapter, we will cover the basic object-oriented principles in the context of design. This allows us to understand these (rather simple) concepts without having to argue with software syntax or Python interpreters.
Objects and classes
So, 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 the example, we can assume that apples go in barrels and oranges go in baskets.
Now, we have four kinds of objects: apples, oranges, baskets, and barrels. In object-oriented modeling, the term used for kind of object is class. So, in technical terms, we now have four classes of objects.
What's the difference between an object and a class? Classes describe objects. They are like blueprints for creating an object. You might have three oranges sitting on the table in front of you. Each orange is a distinct object, but all three have the attributes and behaviors associated with one class: the general class of oranges.
The relationship between the four classes of objects in our inventory system can be described using a Unified Modeling Language (invariably referred to as UML, because three letter acronyms never go out of style) class diagram. Here is our first class diagram:
This diagram shows that an Orange is somehow associated with a Basket and that an Apple is also somehow associated with a Barrel. Association is the most basic way for two classes to be related.
UML is very popular among managers, and occasionally disparaged by programmers. The syntax of a UML diagram is generally pretty obvious; you don't have to read a tutorial to (mostly) understand what is going on when you see one. UML is also fairly easy to draw, and quite intuitive. After all, many people, when describing classes and their relationships, will naturally draw boxes with lines between them. Having a standard based on these intuitive diagrams makes it easy for programmers to communicate with designers, managers, and each other.
However, some programmers think UML is a waste of time. Citing iterative development, they will argue that formal specifications done up in fancy UML diagrams are going to be redundant before they're implemented, and that maintaining these formal diagrams will only waste time and not benefit anyone.
Depending on the corporate structure involved, this may or may not be true. However, every programming team consisting of more than one person will occasionally has to sit down and hash out the details of the subsystem it is currently working on. UML is extremely useful in these brainstorming sessions for quick and easy communication. Even those organizations that scoff at formal class diagrams tend to use some informal version of UML in their design meetings or team discussions.
Further, the most important person you will ever have to communicate with is yourself. We all think we can remember the design decisions we've made, but there will always be the Why did I do that? moments hiding in our future. If we keep the scraps of papers we did our initial diagramming on when we started a design, we'll eventually find them a useful reference.
This chapter, however, is not meant to be a tutorial in UML. There are many of these available on the Internet, as well as numerous books available on the topic. UML covers far more than class and object diagrams; it also has a syntax for use cases, deployment, state changes, and activities. We'll be dealing with some common class diagram syntax in this discussion of object-oriented design. You'll find that you can pick up the structure by example, and you'll subconsciously choose the UML-inspired syntax in your own team or personal design sessions.
Our initial diagram, while correct, does not remind us that apples go in barrels or how many barrels a single apple can go in. It only tells us that apples are somehow associated with barrels. The association between classes is often obvious and needs no further explanation, but we have the option to add further clarification as needed.
The beauty of UML is that most things are optional. We only need to specify as much information in a diagram as makes sense for the current situation. In a quick whiteboard session, we might just quickly draw lines between boxes. In a formal document, we might go into more detail. In the case of apples and barrels, we can be fairly confident that the association is, many apples go in one barrel, but just to make sure nobody confuses it with, one apple spoils one barrel, we can enhance the diagram as shown:
This diagram tells us that oranges go in baskets with a little arrow showing what goes in what. It also tells us the number of that object that can be used in the association on both sides of the relationship. One Basket can hold many (represented by a *) Orange objects. Any one Orange can go in exactly one Basket. This number is referred to as the multiplicity of the object. You may also hear it described as the cardinality. These are actually slightly distinct terms. Cardinality refers to the actual number of items in the set, whereas multiplicity specifies how small or how large this number could be.
I frequently forget which side of a relationship the multiplicity goes on. The multiplicity nearest to a class is the number of objects of that class that can be associated with any one object at the other end of the association. For the apple goes in barrel association, reading from left to right, many instances of the Apple class (that is many Apple objects) can go in any one Barrel. Reading from right to left, exactly one Barrel can be associated with any one Apple.
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. An object 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. That's simple enough, but what are these data and behaviors that are associated with each object?
Data describes objects
Let's start with data. Data typically represents the individual characteristics of a certain object. A class can define specific sets of characteristics that are shared by all objects of that class. Any specific object can have different data values for the given characteristics. For example, our three oranges on the table (if we haven't eaten any) could each weigh a different amount. The orange class could then have a weight attribute. All instances of the orange class have a weight attribute, but each orange has a different value for this attribute. Attributes don't have to be unique, though; any two oranges may weigh the same amount. As a more realistic example, two objects representing different customers might have the same value for a first name attribute.
Attributes are frequently referred to as members or properties. Some authors suggest that the terms have different meanings, usually that attributes are settable, while properties are read-only. In Python, the concept of read-only
is rather pointless, so throughout this book, we'll see the two terms used interchangeably. In addition, as we'll discuss in Chapter 5, When to Use Object-oriented Programming, the property keyword has a special meaning in Python for a particular kind of attribute.
In our fruit inventory application, the fruit farmer may want to know what orchard the orange came from, when it was picked, and how much it weighs. They might also want to keep track of where each basket is stored. Apples might have a color attribute, and barrels might come in different sizes. Some of these properties may also belong to multiple classes (we may want to know when apples are picked, too), but for this first example, let's just add a few different attributes to our class diagram:
Depending on how detailed our design needs to be, we can also specify the type for each attribute. Attribute types are often primitives that are standard to most programming languages, such as integer, floating-point number, string, byte, or Boolean. However, they can also represent data structures such as lists, trees, or graphs, or most notably, other classes. This is one area where the design stage can overlap with the programming stage. The various primitives or objects available in one programming language may be somewhat different from what is available in other languages.
Usually, we don't need to be overly concerned with data types at the design stage, as implementation-specific details are chosen during the programming stage. Generic names are normally sufficient for design. If our design calls for a list container type, the Java programmers can choose to use a LinkedList or an ArrayList when implementing it, while the Python programmers (that's us!) can choose between the list built-in and a tuple.
In our fruit-farming example so far, our attributes are all basic primitives. However, there are some implicit attributes that we can make explicit—the associations. For a given orange, we might have an attribute containing the basket that holds that orange.
Behaviors are actions
Now, we know what data is, but what are behaviors? Behaviors are actions that can occur on an object. The behaviors that can be performed on a specific class of objects are called methods. At the programming level, methods are like functions in structured programming, but they magically have access to all the data associated with this object. Like functions, methods can also accept parameters and return values.
Parameters to a method are a list of objects that need to be passed into the method that is being called (the objects that are passed in from the calling object are usually referred to as arguments). These objects are used by the method to perform whatever behavior or task it is meant to do. Returned values are the results of that task.
We've stretched our comparing apples and oranges
example into a basic (if far-fetched) inventory application. Let's stretch it a little further and see if it breaks. One action that can be associated with oranges is the pick action. If you think about implementation, pick would place the orange in a basket by updating the basket attribute of the orange, and by adding the orange to the oranges list on the Basket. So, pick needs to know what basket it is dealing with. We do this by giving the pick method a basket parameter. Since our fruit farmer also sells juice, we can add a squeeze method to Orange. When squeezed, squeeze might return the amount of juice retrieved, while also removing the Orange from the basket it was in.
Basket can have a sell action. When a basket is sold, our inventory system might update some data on as-yet unspecified objects for accounting and profit calculations. Alternatively, our basket of oranges might go bad before we can sell them, so we add a discard method. Let's add these methods to our diagram:
Adding models and methods to individual objects allows us to create a system of interacting objects. Each object in the system is a member of a certain class. These classes specify what types of data the object can hold and what methods can be invoked on it. The data in each object can be in a different state from other objects of the same class, and each object may react to method calls differently because of the differences in state.
Object-oriented analysis and design is all about figuring out what those objects are and how they should interact. The next section describes principles that can be used to make those interactions as simple and intuitive as possible.
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 use to interact with that object. They do not need, and are often 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