Python E S S E N T I A L R E F E R E N C E Fourth Edition ptg From the Library of Lee Bogdanoff Developer’s Library ESSENTIAL REFERENCES FOR PROGRAMMING PROFESSIONALS Developer’s Library books are designed to provide practicing programmers with unique,high-quality references and tutorials on the programming languages and technologies they use in their daily work. All books in the Developer’s Library are written by expert technology practitioners who are especially skilled at organizing and presenting information in a way that’s useful for other programmers. Key titles include some of the best,most widely acclaimed books within their topic areas: PHP and MySQL Web Development Python Essential Reference Luke Welling & Laura Thomson David Beazley ISBN 978-0-672-32916-6 ISBN-13:978-0-672-32862-6 MySQL Programming in Objective-C Paul DuBois Stephen G.Kochan ISBN-13:978-0-672-32938-8 ISBN-13:978-0-321-56615-7 ptg Linux Kernel Development PostgreSQL Robert Love Korry Douglas ISBN-13:978-0-672-32946-3 ISBN-13:978-0-672-33015-5 Developer’s Library books are available at most retail and online bookstores,as well as by subscription from Safari Books Online at safari.informit.com Developer’s Library informit.com/devlibrary From the Library of Lee Bogdanoff Python E S S E N T I A L R E F E R E N C E Fourth Edition ptg David M. Beazley Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Cape Town • Sydney • Tokyo • Singapore • Mexico City From the Library of Lee Bogdanoff Python Essential Reference Acquisitions Editor Fourth Edition Mark Taber Copyright © 2009 by Pearson Education,Inc. Development Editor All rights reserved. No part of this book shall be reproduced,stored in Michael Thurston a retrieval system,or transmitted by any means,electronic,mechani- Managing Editor cal,photocopying,recording,or otherwise,without written permission Patrick Kanouse from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution Project Editor has been taken in the preparation of this book,the publisher and Seth Kerney author assume no responsibility for errors or omissions. Nor is any Copy Editor liability assumed for damages resulting from the use of the informa- Lisa Thibault tion contained herein. ISBN-13: 978-0-672-32978-4 Indexer ISBN-10: 0-672-32978-6 David Beazley Printed in the United States of America Proofreader First Printing June 2009 Megan Wade Library of Congress Cataloging-in-Publication data is on file. Technical Editors Noah Gift Trademarks Kurt Grandis All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Addison-Wesley Publishing cannot attest to the accuracy of this information. Use of a term in Coordinator this book should not be regarded as affecting the validity of any trade- Vanessa Evans mark or service mark. Book Designer Warning and Disclaimer Gary Adair Every effort has been made to make this book as complete and as Compositor accurate as possible,but no warranty or fitness is implied. The infor- Bronkella Publishing ptg mation provided is on an “as is” basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information con- tained in this book. Bulk Sales Addison-Wesley offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 [email protected] For sales outside of the U.S.,please contact International Sales [email protected] To register this product and gain access to bonus content,go to www.informit.com/register to sign in and enter the ISBN. After you register the product,a link to the additional content will be listed on your Account page,under Registered Products. From the Library of Lee Bogdanoff ❖ For Paula,Thomas.,and his brother on the way. ❖ ptg From the Library of Lee Bogdanoff Contents at a Glance Introduction 1 Part I: The Python Language 1 A Tutorial Introduction 5 2 Lexical Conventions and Syntax 25 3 Types and Objects 33 4 Operators and Expressions 65 5 Program Structure and Control Flow 81 6 Functions and Functional Programming 93 7 Classes and Object-Oriented Programming 117 8 Modules,Packages,and Distribution 143 9 Input and Output 157 10 Execution Environment 173 11 Testing,Debugging,Profiling,and Tuning 181 Part II: The Python Library ptg 12 Built-In Functions 201 13 Python Runtime Services 219 14 Mathematics 243 15 Data Structures,Algorithms,and Code Simplification 257 16 String and Text Handling 277 17 Python Database Access 297 18 File and Directory Handling 313 19 Operating System Services 331 20 Threads and Concurrency 413 21 Network Programming and Sockets 449 22 Internet Application Programming 497 23 Web Programming 531 24 Internet Data Handling and Encoding 545 25 Miscellaneous Library Modules 585 Part III: Extending and Embedding 26 Extending and Embedding Python 591 Appendix: Python 3 621 Index 639 From the Library of Lee Bogdanoff Table of Contents Introduction 1 I: The Python Language 1 A Tutorial Introduction 5 Running Python 5 Variables and Arithmetic Expressions 7 Conditionals 9 File Input and Output 10 Strings 11 Lists 12 Tuples 14 Sets 15 Dictionaries 16 Iteration and Looping 17 Functions 18 ptg Generators 19 Coroutines 20 Objects and Classes 21 Exceptions 22 Modules 23 Getting Help 24 2 Lexical Conventions and Syntax 25 Line Structure and Indentation 25 Identifiers and Reserved Words 26 Numeric Literals 26 String Literals 27 Containers 29 Operators,Delimiters,and Special Symbols 30 Documentation Strings 30 Decorators 30 Source Code Encoding 31 3 Types and Objects 33 Terminology 33 Object Identity and Type 33 Reference Counting and Garbage Collection 34 References and Copies 35 From the Library of Lee Bogdanoff viii Contents First-Class Objects 36 Built-in Types for Representing Data 37 The None Type 38 Numeric Types 38 Sequence Types 39 Mapping Types 44 Set Types 46 Built-in Types for Representing Program Structure 47 Callable Types 47 Classes,Types,and Instances 50 Modules 50 Built-in Types for Interpreter Internals 51 Code Objects 51 Frame Objects 52 Traceback Objects 52 Generator Objects 53 Slice Objects 53 Ellipsis Object 54 ptg Object Behavior and Special Methods 54 Object Creation and Destruction 54 Object String Representation 55 Object Comparison and Ordering 56 Type Checking 57 Attribute Access 57 Attribute Wrapping and Descriptors 58 Sequence and Mapping Methods 58 Iteration 59 Mathematical Operations 60 Callable Interface 62 Context Management Protocol 62 Object Inspection and dir() 63 4 Operators and Expressions 65 Operations on Numbers 65 Operations on Sequences 67 String Formatting 70 Advanced String Formatting 72 Operations on Dictionaries 74 Operations on Sets 75 Augmented Assignment 75 From the Library of Lee Bogdanoff Contents ix The Attribute (.) Operator 76 The Function Call () Operator 76 Conversion Functions 76 Boolean Expressions and Truth Values 77 Object Equality and Identity 78 Order of Evaluation 78 Conditional Expressions 79 5 Program Structure and Control Flow 81 Program Structure and Execution 81 Conditional Execution 81 Loops and Iteration 82 Exceptions 84 Built-in Exceptions 86 Defining New Exceptions 88 Context Managers and the with Statement 89 Assertions and __debug__ 91 6 Functions and Functional Programming 93 ptg Functions 93 Parameter Passing and Return Values 95 Scoping Rules 96 Functions as Objects and Closures 98 Decorators 101 Generators and yield 102 Coroutines and yield Expressions 104 Using Generators and Coroutines 106 List Comprehensions 108 Generator Expressions 109 Declarative Programming 110 The lambda Operator 112 Recursion 112 Documentation Strings 113 Function Attributes 114 eval(),exec(),and compile() 115 7 Classes and Object-Oriented Programming 117 TheclassStatement 117 Class Instances 118 Scoping Rules 118 Inheritance 119 From the Library of Lee Bogdanoff