更新时间:2021-06-11 18:49:37
封面
版权信息
Why subscribe?
About the author
About the reviewers
Preface
1 Numbers Strings and Tuples
Working with large and small integers
Choosing between float decimal and fraction
Choosing between true pision and floor pision
Rewriting an immutable string
String parsing with regular expressions
Building complex strings with f-strings
Building complicated strings from lists of characters
Using the Unicode characters that aren't on our keyboards
Encoding strings – creating ASCII and UTF-8 bytes
Decoding bytes – how to get proper characters from some bytes
Using tuples of items
Using NamedTuples to simplify item access in tuples
2 Statements and Syntax
Writing Python script and module files – syntax basics
Writing long lines of code
Including descriptions and documentation
Writing better RST markup in docstrings
Designing complex if...elif chains
Saving intermediate results with the := "walrus"
Avoiding a potential problem with break statements
Leveraging exception matching rules
Avoiding a potential problem with an except: clause
Concealing an exception root cause
Managing a context using the with statement
3 Function Definitions
Function parameters and type hints
Designing functions with optional parameters
Designing type hints for optional parameters
Using super flexible keyword parameters
Forcing keyword-only arguments with the * separator
Defining position-only parameters with the / separator
Writing hints for more complex types
Picking an order for parameters based on partial functions
Writing clear documentation strings with RST markup
Designing recursive functions around Python's stack limits
Writing testable scripts with the script-library switch
4 Built-In Data Structures Part 1: Lists and Sets
Choosing a data structure
Building lists – literals appending and comprehensions
Slicing and dicing a list
Deleting from a list – deleting removing popping and filtering
Writing list-related type hints
Reversing a copy of a list
Building sets – literals adding comprehensions and operators
Removing items from a set – remove() pop() and difference
Writing set-related type hints
5 Built-In Data Structures Part 2: Dictionaries
Creating dictionaries – inserting and updating
Removing from dictionaries – the pop() method and the del statement
Controlling the order of dictionary keys
Writing dictionary-related type hints
Understanding variables references and assignment
Making shallow and deep copies of objects
Avoiding mutable default values for function parameters
6 User Inputs and Outputs
Using the features of the print() function
Using input() and getpass() for user input
Debugging with f"{value=}" strings
Using argparse to get command-line input
Using cmd to create command-line applications
Using the OS environment settings
7 Basics of Classes and Objects
Using a class to encapsulate data and processing
Essential type hints for class definitions
Designing classes with lots of processing
Using typing.NamedTuple for immutable objects
Using dataclasses for mutable objects
Using frozen dataclasses for immutable objects
Optimizing small objects with __slots__
Using more sophisticated collections
Extending a built-in collection – a list that does statistics
Using properties for lazy attributes
Creating contexts and context managers
Managing multiple contexts with multiple resources
8 More Advanced Class Design
Choosing between inheritance and composition – the "is-a" question
Separating concerns via multiple inheritance
Leveraging Python's duck typing
Managing global and singleton objects
Using more complex structures – maps of lists
Creating a class that has orderable objects
Improving performance with an ordered collection
Deleting from a list of complicated objects
9 Functional Programming Features
Introduction
Writing generator functions with the yield statement
Applying transformations to a collection
Using stacked generator expressions
Picking a subset – three ways to filter
Summarizing a collection – how to reduce
Combining the map and reduce transformations
Implementing "there exists" processing
Creating a partial function