Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet6/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   2   3   4   5   6   7   8   9   ...   183
Bog'liq
python3-oop

Dusty Phillips
www.it-ebooks.info


[
 i 
]
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
www.it-ebooks.info


Table of Contents
[
 ii 
]
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
www.it-ebooks.info


Table of Contents
[
 iii 
]
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
www.it-ebooks.info


Table of Contents
[
 iv 
]
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
www.it-ebooks.info


Table of Contents
[
 v 
]
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
www.it-ebooks.info


Table of Contents
[
 vi 
]
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
www.it-ebooks.info


[
 vii 
]
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.
www.it-ebooks.info


Preface
[
 viii 
]
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.
www.it-ebooks.info


Preface

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   ...   183




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish