The Python Standard Library by Example
Doug Hellmann
Upper Saddle River Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Capetown Sydney Tokyo Singapore Mexico City
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
For sales outside the United States, please contact:
International Sales
Visit us on the Web: informit.com/aw
Library of Congress Cataloging-in-Publication Data
Hellmann, Doug.
The Python standard library by example / Doug Hellmann.
p. cm.
Includes index.
ISBN 978-0-321-76734-9 (pbk. : alk. paper)
1. Python (Computer program language) I. Title.
QA76.73.P98H446 2011
005.13'3dc22
2011006256
Copyright 2011 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to:
Pearson Education, Inc.
Rights and Contracts Department
501 Boylston Street, Suite 900
Boston, MA 02116
Fax: (617) 671-3447
ISBN-13: 978-0-321-76734-9
ISBN-10: 0-321-76734-9
Text printed in the United States on recycled paper at Edwards Brothers in Ann Arbor, Michigan. First printing, May 2011
This book is dedicated to my wife, Theresa, for everything she has done for me.
Tables
Foreword
Its Thanksgiving Day, 2010. For those outside of the United States, and for many of those within it, it might just seem like a holiday where people eat a ton of food, watch some football, and otherwise hang out.
For me, and many others, its a time to take a look back and think about the things that have enriched our lives and give thanks for them. Sure, we should be doing that every day, but having a single day thats focused on just saying thanks sometimes makes us think a bit more broadly and a bit more deeply.
Im sitting here writing the foreward to this book, something Im very thankful for having the opportunity to dobut Im not just thinking about the content of the book, or the author, who is a fantastic community member. Im thinking about the subject matter itselfPythonand specifically, its standard library.
Every version of Python shipped today contains hundreds of modules spanning many years, many developers, many subjects, and many tasks. It contains modules for everything from sending and receiving email, to GUI development, to a built-in HTTP server. By itself, the standard library is a massive work. Without the people who have maintained it throughout the years, and the hundreds of people who have submitted patches, documentation, and feedback, it would not be what it is today.
Its an astounding accomplishment, and something that has been the critical component in the rise of Pythons popularity as a language and ecosystem. Without the standard library, without the batteries included motto of the core team and others, Python would never have come as far. It has been downloaded by hundreds of thousands of people and companies, and has been installed on millions of servers, desktops, and other devices.
Without the standard library, Python would still be a fantastic language, built on solid concepts of teaching, learning, and readability. It might have gotten far enough on its own, based on those merits. But the standard library turns it from an interesting experiment into a powerful and effective tool.
Every day, developers across the world build tools and entire applications based on nothing but the core language and the standard library. You not only get the ability to conceptualize what a car is (the language), but you also get enough parts and tools to put together a basic car yourself. It might not be the perfect car, but it gets you from A to B, and thats incredibly empowering and rewarding. Time and time again, I speak to people who look at me proudly and say, Look what I built with nothing except what came with Python!
It is not, however, a fait accompli. The standard library has its warts. Given its size and breadth, and its age, its no real surprise that some of the modules have varying levels of quality, API clarity, and coverage. Some of the modules have suffered feature creep, or have failed to keep up with modern advances in the areas they cover. Python continues to evolve, grow, and improve over time through the help and hard work of many, many unpaid volunteers.
Some argue, though, that due to the shortcomings and because the standard library doesnt necessarily comprise the best of breed solutions for the areas its modules cover (best of is a continually moving and adapting target, after all), that it should be killed or sent out to pasture, despite continual improvement. These people miss the fact that not only is the standard library a critical piece of what makes Python continually successful, but also, despite its warts, it is still an excellent resource.
But Ive intentionally ignored one giant area: documentation. The standard librarys documentation is good and is constantly improving and evolving. Given the size and breadth of the standard library, the documentation is amazing for what it is. Its awesome that we have hundreds of pages of documentation contributed by hundreds of developers and users. The documentation is used every single day by hundreds of thousands of people to create thingsthings as simple as one-off scripts and as complex as the software that controls giant robotic arms.
The documentation is why we are here, though. All good documentation and code starts with an ideaa kernel of a concept about what something is, or will be. Outward from that kernel come the characters (the APIs) and the storyline (the modules). In the case of code, sometimes it starts with a simple idea: I want to parse a string and look for a date. But when you reach the endwhen youre looking at the few hundred unit tests, functions, and other bits youve madeyou sit back and realize youve built something much, much more vast than originally intended. The same goes for documentation, especially the documentation of code.
The examples are the most critical component in the documentation of code, in my estimation. You can write a narrative about a piece of an API until it spans entire books, and you can describe the loosely coupled interface with pretty words and thoughtful use cases. But it all falls flat if a user approaching it for the first time cant glue those pretty words, thoughtful use cases, and API signatures together into something that makes sense and solves their problems.