Sharpen your Python skills as you dive deep into the Python programming language with Serious Python. You’ll cover a range of advanced topics like multithreading and memorization, get advice from experts on things like designing APIs and dealing with databases, and learn Python internals to help you gain a deeper understanding of the language itself. Written for developers and experienced programmers, Serious Python brings together over 15 years of Python experience to teach you how to avoid common mistakes, write code more efficiently, and build better programs in less time.
As you make your way through the book’s extensive tutorials, you’ll learn how to start a project and tackle topics like versioning, layouts, coding style, and automated checks. You’ll learn how to package your software for distribution, optimize performance, use the right data structures, define functions efficiently, pick the right libraries, build future-proof programs, and optimize your programs down to the bytecode.
ABOUT THIS BOOK
This book is not necessarily designed to be read from front to back. You should feel free
to skip to sections that interest you or are relevant to your work. Throughout the book,
you’ll find a wide range of advice and practical tips. Here’s a quick breakdown of what
each chapter contains.
Chapter 1 provides guidance about what to consider before you undertake a project,
with advice on structuring your project, numbering versions, setting up automated
error checking, and more. At the end there’s an interview with Joshua Harlow.
Chapter 2 introduces Python modules, libraries, and frameworks and talks a little
about how they work under the hood. You’ll find guidance on using the sys module,
getting more from the pip package manager, choosing the best framework for you, and
using standard and external libraries. There’s also an interview with Doug Hellmann.
Chapter 3 gives advice on documenting your projects and managing your APIs as your
project evolves even after publication. You’ll get specific guidance on using Sphinx to
automate certain documentation tasks. Here you’ll find an interview with Christophe de
Chapter 4 covers the ageold issue of time zones and how best to handle them in your
programs using datetime objects and tzinfo objects.
Chapter 5 helps you get your software to users with guidance on distribution. You’ll
learn about packaging, distributions standards, the distutils and setuptools libraries, and
how to easily discover dynamic features in a package using entry points. Nick Coghlan
Chapter 6 advises you on unit testing with bestpractice tips and specific tutorials on
automating unit tests with pytest. You’ll also look at using virtual environments to
increase the isolation of your tests. The interview is with Robert Collins.
Chapter 7 digs into methods and decorators. This is a look at using Python for
functional programming, with advice on how and when to use decorators and how to
create decorators for decorators. We’ll also dig into static, class, and abstract methods
and how to mix the three for a more robust program.
Chapter 8 shows you more functional programming tricks you can implement in
Python. This chapter discusses generators, list comprehensions, functional functions
and common tools for implementing them, and the useful functools library.
Chapter 9 peeks under the hood of the language itself and discusses the abstract
syntax tree (AST) that is the inner structure of Python. We’ll also look at extending
flake8 to work with the AST to introduce more sophisticated automatic checks into your
programs. The chapter concludes with an interview with Paul Tagliamonte.
Chapter 10 is a guide to optimizing performance by using appropriate data structures,
defining functions efficiently, and applying dynamic performance analysis to identify
bottlenecks in your code. We’ll also touch on memoization and reducing waste in data
copies. You’ll find an interview with Victor Stinner.
Chapter 11 tackles the difficult subject of multithreading, including how and when to
use multithreading as opposed to multiprocessing and whether to use eventoriented or
serviceoriented architecture to create scalable programs.
Chapter 12 covers relational databases. We’ll take a look at how they work and how to
use PostgreSQL to effectively manage and stream data. Dimitri Fontaine is interviewed.
Finally, Chapter 13 offers sound advice on a range of topics: making your code
compatible with both Python 2 and 3, creating functional Lisplike code, using context
managers, and reducing repetition with the attr library.