Unleash the power of Python 3 objects
Dusty Phillips
Book Details
Price
|
3.00 |
---|---|
Pages
| 460 p |
File Size
|
2,619 KB |
File Type
|
PDF format |
ISBN
| 978-1-78439-878-1 |
Copyright©
| 2015 Packt Publishing |
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 selfpublished
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.
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, krishna@krishnabharadwaj.info, or his website,
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.
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.
Table of Contents
Preface vii
Chapter 1: Object-oriented Design 1
Introducing object-oriented 1
Objects and classes 3
Specifying attributes and behaviors 5
Data describes objects 6
Behaviors are actions 7
Hiding details and creating the public interface 9
Composition 11
Inheritance 14
Inheritance provides abstraction 16
Multiple inheritance 17
Case study 18
Exercises 25
Summary 26
Chapter 2: Objects in Python 27
Creating Python classes 27
Adding attributes 29
Making it do something 30
Talking to yourself 30
More arguments 31
Initializing the object 33
Explaining yourself 35
Modules and packages 37
Organizing the modules 40
Absolute imports 40
Relative imports 41
Organizing module contents 43
Who can access my data? 46
Third-party libraries 48
Case study 49
Exercises 58
Summary 58
Chapter 3: When Objects Are Alike 59
Basic inheritance 59
Extending built-ins 62
Overriding and super 63
Multiple inheritance 65
The diamond problem 67
Different sets of arguments 72
Polymorphism 75
Abstract base classes 78
Using an abstract base class 78
Creating an abstract base class 79
Demystifying the magic 81
Case study 82
Exercises 95
Summary 96
Chapter 4: Expecting the Unexpected 97
Raising exceptions 98
Raising an exception 99
The effects of an exception 101
Handling exceptions 102
The exception hierarchy 108
Defining our own exceptions 109
Case study 114
Exercises 123
Summary 124
Chapter 5: When to Use Object-oriented Programming 125
Treat objects as objects 125
Adding behavior to class data with properties 129
Properties in detail 132
Decorators – another way to create properties 134
Deciding when to use properties 136
Manager objects 138
Removing duplicate code 140
In practice 142
Case study 145
Exercises 153
Summary 154
Chapter 6: Python Data Structures 155
Empty objects 155
Tuples and named tuples 157
Named tuples 159
Dictionaries 160
Dictionary use cases 164
Using defaultdict 164
Counter 166
Lists 167
Sorting lists 169
Sets 173
Extending built-ins 177
Queues 182
FIFO queues 183
LIFO queues 185
Priority queues 186
Case study 188
Exercises 194
Summary 195
Chapter 7: Python Object-oriented Shortcuts 197
Python built-in functions 197
The len() function 198
Reversed 198
Enumerate 200
File I/O 201
Placing it in context 203
An alternative to method overloading 205
Default arguments 206
Variable argument lists 208
Unpacking arguments 212
Functions are objects too 213
Using functions as attributes 217
Callable objects 218
Case study 219
Exercises 226
Summary 227
Chapter 8: Strings and Serialization 229
Strings 229
String manipulation 230
String formatting 232
Escaping braces 233
Keyword arguments 234
Container lookups 235
Object lookups 236
Making it look right 237
Strings are Unicode 239
Converting bytes to text 240
Converting text to bytes 241
Mutable byte strings 243
Regular expressions 244
Matching patterns 245
Matching a selection of characters 246
Escaping characters 247
Matching multiple characters 248
Grouping patterns together 249
Getting information from regular expressions 250
Making repeated regular expressions efficient 252
Serializing objects 252
Customizing pickles 254
Serializing web objects 257
Case study 260
Exercises 265
Summary 267
Chapter 9: The Iterator Pattern 269
Design patterns in brief 269
Iterators 270
The iterator protocol 271
Comprehensions 273
List comprehensions 273
Set and dictionary comprehensions 276
Generator expressions 277
Generators 279
Yield items from another iterable 281
Coroutines 284
Back to log parsing 287
Closing coroutines and throwing exceptions 289
The relationship between coroutines, generators, and functions 290
Case study 291
Exercises 298
Summary 299
Chapter 10: Python Design Patterns I 301
The decorator pattern 301
A decorator example 302
Decorators in Python 305
The observer pattern 307
An observer example 308
The strategy pattern 310
A strategy example 311
Strategy in Python 313
The state pattern 313
A state example 314
State versus strategy 320
State transition as coroutines 320
The singleton pattern 320
Singleton implementation 321
The template pattern 325
A template example 325
Exercises 329
Summary 329
Chapter 11: Python Design Patterns II 331
The adapter pattern 331
The facade pattern 335
The flyweight pattern 337
The command pattern 341
The abstract factory pattern 346
The composite pattern 351
Exercises 355
Summary 356
Chapter 12: Testing Object-oriented Programs 357
Why test? 357
Test-driven development 359
Unit testing 360
Assertion methods 362
Reducing boilerplate and cleaning up 364
Organizing and running tests 365
Ignoring broken tests 366
Testing with py.test 368
One way to do setup and cleanup 370
A completely different way to set up variables 373
Skipping tests with py.test 377
Imitating expensive objects 378
How much testing is enough? 382
Case study 385
Implementing it 386
Exercises 391
Summary 392
Chapter 13: Concurrency 393
Threads 394
The many problems with threads 397
Shared memory 397
The global interpreter lock 398
Thread overhead 399
Multiprocessing 399
Multiprocessing pools 401
Queues 404
The problems with multiprocessing 406
Futures 406
AsyncIO 409
AsyncIO in action 410
Reading an AsyncIO future 411
AsyncIO for networking 412
Using executors to wrap blocking code 415
Streams 417
Executors 417
Case study 418
Exercises 425
Summary 426
Index 427
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 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.