Python ESSENTIAL REFERENCE Fourth Edition F h Lib fL B d ff 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 ISBN 978-0-672-32916-6 David Beazley ISBN-13: 978-0-672-32862-6 MySQL Programming in Objective-C Paul DuBois ISBN-13: 978-0-672-32938-8 Stephen G Kochan ISBN-13: 978-0-321-56615-7 Linux Kernel Development PostgreSQL Robert Love ISBN-13: 978-0-672-32946-3 Korry Douglas 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 F h Lib fL B d ff Python ESSENTIAL REFERENCE Fourth Edition 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 F h Lib fL B d ff Acquisitions Editor Mark Taber Python Essential Reference Fourth Edition Copyright © 2009 by Pearson Education, Inc All rights reserved No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher No patent liability is assumed with respect to the use of the information contained herein Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions Nor is any liability assumed for damages resulting from the use of the information contained herein Development Editor Michael Thurston Managing Editor Patrick Kanouse Project Editor Seth Kerney Copy Editor Lisa Thibault ISBN-13: 978-0-672-32978-4 ISBN-10: 0-672-32978-6 Indexer David Beazley Printed in the United States of America Proofreader Megan Wade First Printing June 2009 Library of Congress Cataloging-in-Publication data is on file Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized Addison-Wesley cannot attest to the accuracy of this information Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied The information 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 contained in this book Technical Editors Noah Gift Kurt Grandis Publishing Coordinator Vanessa Evans Book Designer Gary Adair Compositor Bronkella Publishing 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 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact International Sales international@pearson.com 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 F h Lib fL B d ff ❖ For Paula,Thomas., and his brother on the way ❖ F h Lib fL B d ff Contents at a Glance Introduction Part I: The Python Language A Tutorial Introduction Lexical Conventions and Syntax Types and Objects 25 33 Operators and Expressions 65 Program Structure and Control Flow 81 Functions and Functional Programming 93 Classes and Object-Oriented Programming Modules, Packages, and Distribution Input and Output 117 143 157 10 Execution Environment 173 11 Testing, Debugging, Profiling, and Tuning 181 Part II: The Python Library 12 Built-In Functions 201 13 Python Runtime Services 14 Mathematics 219 243 15 Data Structures, Algorithms, and Code Simplification 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 22 Internet Application Programming 23 Web Programming 257 449 497 531 24 Internet Data Handling and Encoding 25 Miscellaneous Library Modules 545 585 Part III: Extending and Embedding 26 Extending and Embedding Python Appendix: Python Index 591 621 639 F h Lib fL B d ff Table of Contents Introduction I: The Python Language A Tutorial Introduction Running Python 5 Variables and Arithmetic Expressions Conditionals File Input and Output Strings Lists 10 11 12 Tuples Sets 14 15 Dictionaries 16 Iteration and Looping Functions 17 18 Generators 19 Coroutines 20 Objects and Classes Exceptions Modules 21 22 23 Getting Help 24 Lexical Conventions and Syntax 25 Line Structure and Indentation 25 Identifiers and Reserved Words Numeric Literals String Literals Containers 26 26 27 29 Operators, Delimiters, and Special Symbols Documentation Strings Decorators 30 30 30 Source Code Encoding Types and Objects Terminology 33 31 33 Object Identity and Type 33 Reference Counting and Garbage Collection References and Copies 34 35 F h Lib fL B d ff viii Contents First-Class Objects 36 Built-in Types for Representing Data The None Type 38 Numeric Types 38 Sequence Types 39 Mapping Types Set Types 37 44 46 Built-in Types for Representing Program Structure Callable Types 47 Classes, Types, and Instances Modules 50 50 Built-in Types for Interpreter Internals Code Objects 52 Traceback Objects 52 Generator Objects 53 Slice Objects 51 51 Frame Objects 53 Ellipsis Object 54 Object Behavior and Special Methods Object Creation and Destruction Object String Representation 54 54 55 Object Comparison and Ordering Type Checking 56 57 Attribute Access 57 Attribute Wrapping and Descriptors Sequence and Mapping Methods Iteration 47 58 58 59 Mathematical Operations Callable Interface 60 62 Context Management Protocol Object Inspection and dir() 62 63 Operators and Expressions 65 Operations on Numbers 65 Operations on Sequences String Formatting 67 70 Advanced String Formatting Operations on Dictionaries Operations on Sets 72 74 75 Augmented Assignment 75 F h Lib fL B d ff Contents The Attribute (.) Operator 76 The Function Call () Operator Conversion Functions 76 76 Boolean Expressions and Truth Values Object Equality and Identity Order of Evaluation 77 78 78 Conditional Expressions 79 Program Structure and Control Flow Program Structure and Execution 81 Conditional Execution 81 81 Loops and Iteration Exceptions 82 84 Built-in Exceptions 86 Defining New Exceptions 88 Context Managers and the with Statement Assertions and _ _debug_ _ Parameter Passing and Return Values Scoping Rules 93 95 96 Functions as Objects and Closures 98 101 Generators and yield 102 Coroutines and yield Expressions 104 Using Generators and Coroutines 106 List Comprehensions 108 Generator Expressions 109 Declarative Programming The lambda Operator Recursion 89 91 Functions and Functional Programming Functions 93 Decorators 110 112 112 Documentation Strings Function Attributes 113 114 eval(), exec(), and compile() 115 Classes and Object-Oriented Programming The class Statement 117 Class Instances Scoping Rules Inheritance ix 117 118 118 119 F h Lib fL B d ff sorting SimpleXMLRPCServer module, 527 sin() function, math module, 252 703 of class definitions, 132 optimization, 196 single precision floating point, 39 Smalltalk, differences in object system, 124 singleton tuple, 14 sinh() function, math module, 252 SMTP protocol, example of sending a message, 514 site configuration files, 177 SMTP() function, smtplib module, 514 site module, 166, 174, 177 smtpd module, 587 site-packages directory, 175 SMTPHandler class, logging module, 363 sitecustomize module, 177 smtplib module, 513 size attribute sndhdr module, 588 of Struct objects, 291 of TarInfo objects, 322 size() method of FTP objects, 499 of mmap objects, 371 sniff() method, of Sniffer objects, 550 Sniffer() function, csv module, 550 SO_* socket options, socket module, 479 SOCK_* constants, socket module, 470 Sized abstract base class, 265 socket attribute, of SocketServer objects, 492 sizeof() function, ctypes module, 618 socket module, 469 skippedEntity() method, of ContentHandler objects, 581 socket() function, socket module, 476 sleep() function, time module, 406 socket_type attribute, of SocketServer class, 493 sleeping, 406 until signal received, 399 socket, definition of, 449 SocketHandler class, logging module, 363 slice assignment, lists, 12 socketpair() function, socket module, 477 slice objects, 51, 53 sockets and indexing methods, 59 attributes of, 54 slice type, 51 slice() function, 53, 210 slices, 39 and special methods, 59 and xrange objects, 44 assignment to, 40, 69 deletion of, 40, 69 multidimensional, 59 slicing operator [:], 67-68 on lists, 12 on strings, 11 _ _slots_ _ attribute and _ _dict_ _ attribute of instances, 50 compatibility with other code, 133 inheritance, 133 address families, 470 methods on, 478 polling with select(), 459 specifying network addresses, 471 types of, 470 SocketServer module, 489 and Python 3, 489 changing server parameters, 492 softspace attribute, of files, 161 sort() method, of lists, 40-41 sorted() function, 210 sorting changing behavior of, 40 in-place on lists, 40 requirements for objects, 57 reverse order, 40 use of operator module, 275 How can we make this index more useful? Email us at indexes@samspublishing.com F h Lib fL B d ff 704 source code encoding source code encoding, 31 Python 3, 622 stack frames, 52 in tracebacks, 53 span() method, of MatchObject objects, 286 stack size, for threads, 444 spawnl() function, os module, 393 spawnle() function, os module, 393 stack_size() function, threading module, 444 spawnlp() function, os module, 393 Stackless Python, 467 spawnlpe() function, os module, 393 standard I/O streams, 161 spawnv() function, os module, 392 spawnve() function, os module, 393 spawnvp() function, os module, 393 spawnvpe() function, os module, 393 special methods, 21, 54 special symbols, 30 speedup, definition of, 192 split() function os.path module, 398 re module, 284 split() method of Regex objects, 285 of strings, 14, 41, 43 splitdrive() function, os.path module, 398 splitext() function, os.path module, 398 splitlines() method, of strings, 44 splitting, strings, 14, 43 splitunc() function, os.path module, 398 SpooledTemporaryFile() function, tempfile module, 324 sprintf() function equivalent, 70 spwd module, 587 SQL queries SQL injection attack, 300 examples of, 309 executing on database, 297 how to form, 300 similarity to list comprehensions, 111 SQLite database, 303 sqlite3 module, 303 sqrt() function, math module, 252 sqrt() method, of Decimal objects, 243 ssl module, 486 SSL, example of creating certificates, 489 stack() function, inspect module, 225 and integrated development environments, 162 setting the default encoding, 175 standard error, 157 standard input and output, 10 standard library reorganization, Python 3, 634 standard_b64decode() function, base64 module, 546 standard_b64encode() function, base64 module, 546 StandardError exception, 87 starmap() function, itertools module, 272 start attribute, of slices, 54 start() method of BaseManager objects, 431 of MatchObject objects, 286 of Process objects, 416 of Thread objects, 436 of Timer objects, 438 of TreeBuilder objects, 577 startDocument() method, of ContentHandler objects, 582 startElement() method, of ContentHandler objects, 582 startElementNS() method, of ContentHandler objects, 582 startfile() function, os module, 393 startPrefixMapping() method, of ContentHandler objects, 582 startswith() method, of strings, 44 startup script, in interactive mode, 174 stat module, 387, 587 stat() function os module, 388 os.path module, 398 st_* attributes, of stat objects, 389 F h Lib fL B d ff strings stat_float_times() function, os module, 389 statement termination, and semicolons, statements breaking across multiple lines, putting on the same line, 25-26 running in the debugger, 186 static method, 22, 48, 123, 125 practical use of, 123 @staticmethod decorator, 22, 48, 123, 125, 210 705 StreamRequestHandler class, SocketServer module, 491 streams, 470 StreamWriter class, codecs module, 278 streamwriter() method, of CodecInfo objects, 278 strerror() function, os module, 381 strftime() function, time module, 406 strftime() method of date objects, 337 of time objects, 338 statistics, random number distributions, 255 ‘strict’ error handling, Unicode encoding, 166 status attribute, of HTTPResponse objects, 504 string attribute, of MatchObject objects, 286 statvfs() function, os module, 389 string formatting, 70 stderr attribute, of Popen objects, 404 stderr variable, sys module, 161, 232 _ _stderr_ _ variable, sys module, 162, 232 stdin attribute, of Popen objects, 404 stdin variable, sys module, 10, 161, 232 _ _stdin_ _ variable, sys module, 162, 232 stdout attribute, of Popen objects, 404 stdout variable, sys module, 10, 161, 232 _ _stdout_ _ variable, sys module, 162, 232 step attribute, of slices, 54 StopIteration exception, 59, 87, 215 and generators, 103 storbinary() method, of FTP objects, 499 storlines() method, of FTP objects, 499 str type, 38 str() function, 11, 56, 76, 210 and print, 162 difference from repr(), 12 _ _str_ _() method, 55-56 !r specifier, 74 !s specifier, 74 alignment, 73 attribute lookup, 72 codes for % operator, 70-71 customizing format() method, 74 dictionaries, 72 dictionary lookup, 72 fill characters, 73 format specifiers, 72 formatting operator %, 70 string interpolation, 72, 163 string literals, 27 Unicode characters, 28 and Unicode encodings, 29 and documentation strings, 30 byte strings, 29 unicode characters in source code, 31 string module, 287 Template strings, 164 StreamError exception, tarfile module, 322 string_at() function, ctypes module, 618 StreamHandler class, logging module, 363 StringIO class, io module, 353 StreamReader class, codecs module, 278 stringprep module, 586 streamreader() method, of CodecInfo objects, 278 strings, 11 Unicode, 41, 165 and numeric calculations, 11 How can we make this index more useful? Email us at indexes@samspublishing.com F h Lib fL B d ff 706 strings as a dictionary key, 16 as in-memory text files, 353 as sequences, 39 basestring object for type checking, 202 byte literals, 29 byte strings, 41, 202 case conversion, 43-44 character substitution, 42 comparison of, 70 concatenation, 11 concatenation of adjacent literals, 27 creating custom formatters, 288 disabling escape codes in literals, 29 encoding for URLs, 522 escape codes in literals, 27 escaping characters for use in HTML, 536 escaping characters for use in XML, 583 executing Python code contained within, 115 format() method, 8, 72 formatting, 8, 42 formatting in log messages, 358 immutability of, 41, 69 indexing, 11 internal representation, 28 iterating over characters, 17 joining, 43 line structure, 11 mixing byte strings and Unicode, 70 mutable byte arrays, 202 partitioning, 41 regular expressions, 281 replacing substrings, 41 searching for substrings, 41 slices, 11 sorting and internationalization, 70 specifiers for format() method, 72-73 splitting, 43 splitting into fields, 14, 41 stripping, 43 unescaping XML character references, 583 strip() method, of strings, 44 stripping control characters from a string, 42 strings, 44 strptime() function, time module, 343, 407 strptime() method, of datetime class, 339 Struct class, struct module, 291 struct module, 290 Structure class, ctypes module, 614 structures, and tuples, 14 sub() function operator module, 273 re module, 284 sub() method, of Regex objects, 285 _ _sub_ _() method, 60 subclass, 119 _ _subclasscheck_ _() method, 57, 136 subdirs attribute, of dircmp objects, 315 SubElement() function, xml.etree.ElementTree module, 575 subn() function, re module, 284 subn() method, of Regex objects, 285 subprocess module, 402 subprocess, definition of, 413 substitute() method of Template objects, 289 of Template strings, 164 substrings checking for existence using in, 69 searching for, 41 subtraction operator -, 65 sucessful() method, of AsyncResult objects, 425 sum() function, 39-40, 67, 210 accuracy of, 252 and decimal module, 69 restriction to numeric data, 39 versus math.fsum() function, 252 sunau module, 588 super() function, 120, 210 Python 3, 210, 627 superclass, 119 calling methods in, 120 super() function, 120 F h Lib fL B d ff TarInfo objects, tarfile module 707 supporting both Python and 3, 638 sys.prefix variable, 177 supports_unicode_filenames variable, os.path module, 398 sys.ps1 variable, 176 surrogate pair, 28, 41 sys.stderr variable, 157, 161 suspension, of threads, 443 sys.stdin variable, 161 swapcase() method, of strings, 44 sys.stdout variable, 161 SWIG, 591 sys_version attribute, of BaseHTTPRequestHandler class, 508 example of, 619 interface file, 619 sys.ps2 variable, 176 sysconf() function, os module, 395 switch statement, lack of, syslog module, 587 symbol module, 586 SysLogHandler class, logging module, 363 symbolic links, testing a filename for, 397 system calls, os module, 378 symlink() function, os module, 389 system error codes, 344 symmetric difference operator ^, of sets, 15 system() function, os module, 393 symmetric_difference() method, of sets, 46 system.listMethods() method, of ServerProxy objects, 525 symmetric_difference_update() method, of sets, 47 SystemError exception, 87, 215 sync() method SystemExit exception, 7, 87-88, 157, 179, 215 of dbm-style database objects, 310 of shelve objects, 311 T synchronization primitives multiprocessing module, 427 threading module, 438 synchronization of close() method of generators, 104 of concurrent programs, 414 of throw() method of generators, 105 SyntaxError exception, 87, 215 Python print statement, and default arguments, 93 and except statements, 85 -t command line option, 26, 173-174 tab escape code in strings, 28 tabcheck attribute, of sys.flags, 230 TabError exception, 26, 87, 215 tabnanny module, 586 tabs, and indentation, 26 tag attribute, of Element objects, 576 tagName attribute, of DOM Element objects, 572 tail attribute, of Element objects, 576 SyntaxWarning warning, 216, 238 tail command, example with generators, 19 sys module, 13, 229 tail-recursion optimization, lack of, 112 sys.argv variable, 13, 157, 174 takewhile() function, itertools module, 272 sys.displayhook variable, 176 tan() function, math module, 252 sys.exec_prefix variable, 177 tanh() function, math module, 252 sys.exit() function, 179 TarError exception, tarfile module, 322 sys.modules variable, 144, 149 TarFile objects, tarfile module, 320 sys.path variable, 147 tarfile module, 319 and site module, 177 third-party modules, 154 TarInfo objects, tarfile module, 321 How can we make this index more useful? Email us at indexes@samspublishing.com F h Lib fL B d ff 708 task scheduler, example with coroutines and select() task scheduler, example with coroutines and select(), 460 task_done() method of JoinableQueue objects, 419 of Queue objects, 445 tasklets, 446 asynchronous I/O, 460 tasks, and coroutines, 20 tb_* attributes, of traceback objects, 53 tb_lineno() function, traceback module, 236 tbreak debugger command, pdb module, 189 tcgetpgrp() function, os module, 385 terminate() method of Pool objects, 425 of Popen objects, 403-404 of Process objects, 416 terminating statements with semicolons, termination immediate without garbage collection, 391 of programs, 179 of threads, 443 registering cleanup functions, 219 sys.exit() function, 233 without garbage collection, 179 TCP connection, diagram of, 450 termios module, 587 TCP protocol, 449 test module, 586 example code, 451 test() function, cgi module, 537 TCP_* socket options, socket module, 482 TestCase class, unittest module, 184 TCPServer class, SocketServer module, 491 testing tcsetpgrp() function, os module, 385 tearDown() method TestCase objects, 184 of TestCase objects, 184 tee() function, itertools module, 273 tell() method of IOBase objects, 350 of files, 160-161 of mmap objects, 371 doctest module, 181 documentation strings, 181 limitations of doctest, 183 unit testing, 183 testmod() function doctest() module, 182 doctest module, 182 testzip() method, of ZipFile objects, 326 Text class, xml.dom.minidom module, 572 telnetlib module, 587 text I/O, 353 tempdir variable, tempfile module, 324 text attribute, of Element objects, 576 tempfile module, 323 text file mode, 159 Template class, string module, 289 text replacement, replace() method of strings, 41 Template strings string module, 164 use in CGI script, 537 template attribute, of Template objects, 290 template variable, tempfile module, 324 temporary files, 323 TemporaryFile() function, tempfile module, 323 text versus bytes in Python 3, 629 text_factory attribute, of Connection objects, 308 TextIOBase abstract base class, 354 TextIOWrapper class, io module, 353 textwrap module, 586 third-party libraries, and Python 3, 621 third-party packages and C/C++ code, 154 and sys.path variable, 154 F h Lib fL B d ff timezone variable, time module installation of, 154 installing in per-user site directory, 154, 178 this pointer, self parameter of methods, 119 Thread class, threading module, 436 thread attribute, of Record objects, 359 threading module, 436 synchronization primitives, 438 709 signal handling, 402 signaling with semaphores, 439 suspending, 443 synchronization of, 414 termination of, 443 throw() method of generators, 105 use of queues, 444 worker thread example, 446 threadsafety variable, database API, 302 ThreadingMixIn class, SocketServer module, 494 throw() method, of generators, 53, 105-106 ThreadingTCPServer class, SocketServer module, 494 time class, datetime module, 338 ThreadingUDPServer class, SocketServer module, 494 threadName attribute, of Record objects, 359 threads CPU-bound tasks, 444 adding to network servers, 494 atomic operations and disassembly, 193 check interval, 234 close() method of generators, 104 compared to coroutines, 467 condition variables, 441 daemonic, 436 database modules, 302 decimal module, 249 definition of, 413 events, 440 extension modules, 607 global interpreter lock, 414, 444 local storage, 443 main thread, 413 mutex lock, 438 obtaining number of active, 443 random number generation, 256 reentrant mutex lock, 438 scaling properties, 415 scheduling of, 414 semaphores, 439 setting stack size, 444 setting the name, 436 time and date parsing, 407 time manipulation, 336 time module, 191, 405 accuracy of time functions, 408 current time, 405 time parsing, 343 Time() function, database API, 301 time() function, time module, 191, 407 time() method, of datetime objects, 340 timedelta class, datetime module, 340 TimedRotatingFileHandler class, logging module, 363 TimeFromTicks() function, database API, 301 timeit module, 191 timeit() function cProfile module, 190 timeit module, 191 timeout attribute, of SocketServer class, 493 timeout exception, socket module, 485 timeout, example with alarm signals, 401 Timer() function, threading module, 437 times() function, os module, 394 Timestamp() function, database API, 301 TimestampFromTicks() function, database API, 301 timetuple() method, of date objects, 337 timetz() method, of datetime objects, 340 timezone variable, time module, 405 How can we make this index more useful? Email us at indexes@samspublishing.com F h Lib fL B d ff 710 timing measurements timing measurements, 191 TIPC protocol, 470 address format, 472 TIPC_* constants, socket module, 473 title() method, of strings, 44 TreeBuilder() function, xml.etree.ElementTree module, 577 triangular() function, random module, 256 triple-quoted strings, 11 and variable interpolation, 163 Tkinter module, 588 True value, 9, 27, 38 today() method, of date class, 336 truediv() function, operator module, 273 tofile() method, of array objects, 260 _ _truediv_ _() method, 60 token module, 586 trunc() function, math module, 252 tokenize module, 586 truncate() method tolist() method, of array objects, 260 toordinal() method, of date objects, 337 toprettyxml() method, of DOM Node objects, 572 of IOBase objects, 350 of files, 160 truncating division operator //, 65 truncation, of integer division, 61, 65 tostring() function, xml.etree.ElementTree module, 578 truth value testing, 78 tostring() method, of array objects, 260 truth() function, operator module, 274 total_changes attribute, of Connection objects, 308 try statement, 22, 84-85 tounicode() method, of array objects, 260 tty module, 587 toxml() method, of DOM Node objects, 573 ttyname() function, os module, 385 trace() function, inspect module, 225 tuning strategies, 194 traceback messages, 22 tuple type, 38 traceback module, 235 tuple unpacking, and for-loops, 15 traceback objects, 51-52 tuple() function, 77, 211 attributes of, 53 stack frames, 53 _ _traceback_ _ attribute, of Exception objects, 213 tracebacklimit variable, sys module, 232 tracebacks creating with traceback module, 236 limiting the amount of output, 232 TracebackType type, 51, 237 trailing J on complex number literals, 27 trailing L on long integers, 27 trailing comma and tuples, 14 print statement, 162 transfercmd() method, of FTP objects, 500 translate() method, of strings, 42, 44 traps attribute, of Context objects, 247 truth values, -tt command line option, 26, 173-174 tuples, 14 and string formatting, 70 as dictionary key, 16 as dictionary keys, 74 as sequence, 39 comparison on, 70 concatenation, 14 creating a list of from dictionary, 45 creating with named attributes, 264 immutability, 14 immutability of, 69 indexing, 14 memory savings of, 14 omission of parantheses, 14 problems with using as data structure, 264 representing records, 14 singleton, 14 F h Lib fL B d ff unary plus operator + slicing, 14 unpacking in Python 3, 623 unpacking in iteration, 82 use by standard library, 265 use in list comprehensions, 109 versus lists, 14 Twisted library, 415, 467 two-dimensional lists, 13, 15 type, 47 type attribute of FieldStorage objects, 535 of TarInfo objects, 322 of socket objects, 484 type checking example with metaclass, 140 of objects, 34 performance impact of, 34 problem with proxy objects, 135 types boolean, 38 built-in, 37, 201 callable, 47 dictionary, 44 floating point, 38 frozenset, 46 integers, 38 of sockets, 470 set, 46 type of, 47 tzinfo attribute, of time objects, 338 tzname variable, time module, 405 tzname() method of time objects, 339 of tzinfo objects, 342 tzset() function, time module, 407 U type coercion, and operator overloading, 134 type comparison of objects, 34 type conversion, 76 lack of implicit conversion, 62 of columns in a datafile, 37 special methods for, 62 711 \U escape code, in strings, 28 \u escape code, in strings, 28 -U command line option, 28, 173 ‘U’ mode, to open() function, 159 u character, before a string literal, 28 type hierarchies, 138 -u command line option, 173 type objects, 50 u(p) debugger command, pdb module, 189 type of objects, 33 UDP client example, 486 type() function, 34, 211 UDP communication, diagram of, 451 and exceptions, 89 UDP protocol, 449 type() metaclass, 138 UDP server example, 485 type_options attribute, of FieldStorage objects, 535 UDPServer class, SocketServer module, 491 typecode attribute, of array objects, 259 uid attribute, of TarInfo objects, 322 TypeError exception, 87, 215 umask() function, os module, 381 and function calls, 94 and type coercion, 62 method resolution order, 122 types module, 47, 237 and Python 3, 237 unalias debugger command, pdb module, 189 uname attribute, of TarInfo objects, 322 uname() function, os module, 381 unary minus operator -, 65 unary plus operator +, 65 How can we make this index more useful? Email us at indexes@samspublishing.com F h Lib fL B d ff unbound method 712 unbound method, 49 and Python 3, 49 UnboundLocalError exception, 87, 98, 215 unbuffered file I/O, 159 unconsumed_tail attribute, of decompressobj objects, 329 underscores, usage in identifiers, 26 unescape() function, xml.sax.saxutils module, 583 ungetch() function, msvcrt module, 373 ungetwch() function, msvcrt module, 373 normalization of strings, 171 specifying characters in string literals, 28 surrogate pairs, 28, 41 using 32-bit character code points, 41 unicode-escape encoding, description of, 170 unicodedata module, 170, 293 UnicodeDecodeError exception, 87, 215 UnicodeEncodeError exception, 87, 215 Python interactive mode, 175 unhexlify() function, binascii module, 548 UnicodeError exception, 87, 166, 215 unichr() function, 77, 211 UnicodeTranslateError exception, 87, 215 Unicode character database, 293 unidata_version variable, unicodedata module, 296 Unicode characters, representation of, 41 Unicode string literals, 28 Unicode strings, 41 and WSGI, 541 common encodings, 166 decomposing, 295 encoding and decoing, 165 encoding in network programs, 452 error handling options, 166 handling of, 165 mixing with byte strings, 167 normalizing, 296 regular expressions, 281 unicode attribute, of sys.flags, 230 unicode type, 38 unicode() function, 211 and Python 3, 211 Unicode Python vs Python 3, 28 and BOM characters, 168 and XML, 168 byte order marker, 280 character encoding and decoding, 42 character properties database, 170 code points, 28 common encodings, 168-169 encoding in string literals, 29 encoding of source code, 31 file I/O, 167 mixing with byte strings, 70 unification of integer and long types, 38 uniform access principle, 125 uniform type arrays, 259 uniform() function, random module, 255 UnimplementedFileMode exception, http.client module, 504 Union class, ctypes module, 614 union operator |, of sets, 15 union() method, of sets, 46 unit testing Python migration, 635 example, 184 unittest module, 183 unittest module, 183 example, 184 universal newline mode, 159 UNIX domain protocol, 470 address format, 472 Unix systems log, issuing message to, 363 Unix #! execution of programs, per-user site directory, 177 time epoch definition, 405 UnixDatagramServer class, SocketServer module, 491 UnixStreamServer class, SocketServer module, 491 UnknownHandler class, urllib.request module, 518 F h Lib fL B d ff UserWarning warning UnknownProtocol exception, http.client module, 504 upper() method, of strings, 44 UnknownTransferEncoding exception, http.client module, 504 urandom() function, os module, 396 unlink() function, os module, 390 unloading modules, 149 713 uppercase variable, string module, 287 urldefrag() function, urllib.parse module, 521 unpack() function, struct module, 290 urlencode() function, urllib.parse module, 522 unpack() method, of Struct objects, 291 URLError exception, 516 unpack_from() function, struct module, 291 unpack_from() method, of Struct objects, 291 unpacking binary data structures, 290 of sequences, 67-68 of tuples, 14 urllib.error module, 523 urljoin() function, urllib.parse module, 521 urllib module, 522 see urllib.request, 515 urllib package, 514 urllib.error module, 523 urllib.parse module, 520 Unpickler class, pickle module, 228 urllib.request module, 515 unquote() function, urllib.parse module, 522 urllib.response module, 520 unquote_plus() function, urllib.parse module, 522 urllib2 module, see urllib.request, 515 urllib.robotparser module, 523 unquote_to_bytes() function, urllib.parse module, 522 urlopen() function, urllib.request module, 515 unregister() method, of Poll objects, 460 urlparse module, 520 unregister_dislect() function, csv module, 551 urlparse() function, urllib.parse module, 520 unsetenv() function, os module, 381 urlsafe_b64decode() function, base64 module, 546 until debugger command, pdb module, 189 unused_data attribute, of decompressobj objects, 329 unwrap() method, of ssl objects, 488 update() method of of of of dicts, 45 digest objects, 559 hmac objects, 560 sets, 15, 47 urlsafe_b64encode() function, base64 module, 546 urlsplit() function, urllib.parse module, 521 urlunparse() function, urllib.parse module, 521 urlunsplit() function, urllib.parse module, 521 user directory, installation of packages, 154 user module, 586 update_wrapper() function, functools module, 269 —user option to setup.py, 154 updating a dictionary, 45 user-agent header in HTTP requests, changing, 517 uploading files in CGI scripts, 536 files to a HTTP server with POST, 505 files to an FTP server, 500 packages to pypi, 155 username attribute of urlparse objects, 520 of urlsplit objects, 521 UserWarning warning, 216, 238 How can we make this index more useful? Email us at indexes@samspublishing.com F h Lib fL B d ff using Python as a calculator 714 using Python as a calculator, utcfromtimestamp() method, of datetime class, 339 utcnow() method, of datetime class, 339 utcoffset() method of time objects, 339 of tzinfo objects, 342 utctimetuple() method, of datetime objects, 340 UTF-16 encoding, description of, 170 UTF-8 compatibility with ASCII, 170 description of, 169-170 encoding and decoding, 42 including in string literals, 29 lexicographic ordering, 170 variable number of arguments in function definition, 94 variables, as names of objects, 35 binding and module imports, 145 binding of globals in functions, 98 class, 117-118 in nested functions, 97 iteration, 82 naming rules, 26 scope of, 96, 98 vars() function, 72, 211 verbose attribute, of sys.flags, 230 verify_request() method, of SocketServer class, 493 utime() function, os module, 390 version attribute, of HTTPResponse objects, 504 uu module, 587 version information, of interpreter, 231 version variable, sys module, 232 V version_info variable, sys module, 232 -V command line option, 173 vformat() method, of Formatter objects, 288 -v command line option, 173-174 view objects, Python 3, 632 validator() function, wsgiref.handlers module, 543 volume attribute, of ZipInfo objects, 327 value attribute vonmisesvariate() function, random module, 256 of FieldStorage objects, 535 of Morsel objects, 512 W Value() function, multiprocessing module, 426 -W command line option, 216, 239-240 Value() method, of Manager objects, 429 ‘w’ mode, to open() function, 159 ValueError exception, 87, 215 w(here) debugger command, pdb module, 189 and lists, 40 and strings, 41 valuerefs() method, of WeakValueDictionary objects, 241 values() method of Message objects, 552 of dicts, 45 of dicts in Python 3, 632 wait() function, os module, 394 wait() method of AsyncResult objects, 425 of Condition objects, 441 of Event objects, 440 of Popen objects, 404 wait3() function, os module, 394 ValuesView abstract base class, 266 wait4() function, os module, 394 variable interpolation in strings, 163 waitpid() function, os module, 394 variable keyword arguments in function definition, 95 walk() function, os module, 390 walk() method, of Message objects, 554 F h Lib fL B d ff wrappers, and attribute binding methods 715 wall-clock time, obtaining, 191 while statement, 8, 82 warn() function, warnings module, 216, 239 whitespace variable, string module, 287 warn_explicit() function, warnings module, 239 WIFEXITED() function, os module, 394 Warning warning, 216, 238 warning() method, of Logger objects, 357 warnings module, 238 warnings converting into exceptions, 239 difference from exceptions, 216 suppression of, 238 warnoptions variable, sys module, 232 WatchedFileHandler class, logging module, 363 wave module, 588 WCOREDUMP() function, os module, 394 weak reference, 130 weak references, 240 WeakKeyDictionary class, weakref module, 241 weakref module, 130, 240 WeakValueDictionary class, weakref module, 241 web frameworks, 538 and template strings, 164 web programming, 531 web server custom handling of requests, 510 running standalone in Python, 508 webbrowser module, 544 weekday() method, of date objects, 338 WIFCONTINUED() function, os module, 395 WIFSIGNALED() function, os module, 395 WIFSTOPPED() function, os module, 395 WinDLL() function, ctypes module, 612 Windows, 331 accesing registry, 408 accessing registry, 175 creating a binary distribution with distutils, 153 double-clicking on Python programs, 176 drive letters in filenames, 399 file locking, 373 issuing message to event log, 362 list of error codes, 346 main program with multiprocessing module, 417 per-user site directory, 177 process fork with multiprocessing, 435 running programs, WindowsError exception, 215 Wing IDE, winreg module, 408 winsound module, 588 winver variable, sys module, 232 with statement, 62, 89 and exceptions, 23 and locking, 89 decimal module, 248 locking primitives, 442 weibullvariate() function, random module, 256 work pools, of processes, 424 well known port numbers, 450 wrap_socket() function, ssl module, 486 WEXITSTATUS() function, os module, 394 wrapper functions, 95 wfile attribute of BaseHTTPRequestHandler objects, 509 of StreamRequestHandler objects, 491 whichdb module, 310 and closures, 100 example of, 101 in extension modules, 594 wrappers, and attribute binding methods, 132 whichdb() function, dbm module, 310 How can we make this index more useful? Email us at indexes@samspublishing.com F h Lib fL B d ff wraps decorator, functools module 716 @wraps decorator, functools module, 114, 269 wsgi.* environment variables, 540 writable() method wsgiref.handlers module, 542 of IOBase objects, 350 of dispatcher objects, 456 write() function, os module, 385 write() method of BufferWriter objects, 352 of ConfigParser objects, 334 of ElementTree objects, 575 of FileIO objects, 350 of StreamWriter objects, 278 of TextIOWrapper objects, 353 of ZipFile objects, 326 of files, 10, 159-160 of mmap objects, 372 of ssl objects, 488 write_byte() method, of mmap objects, 372 writelines() method of IOBase objects, 350 of StreamWriter objects, 278 of files, 159-160 writepy() method, of ZipFile objects, 326 writer() function, csv module, 549 writerow() method of csv DictWriter objects, 550 of csv writer objects, 549 writerows() method of csv DictWriter objects, 550 of csv writer objects, 550 writestr() method, of ZipFile objects, 327 writexml() method, of DOM Node objects, 573 wsgiref package, 542 wsgiref.simple_server module, 542 WSTOPSIG() function, os module, 395 wstring_at() function, ctypes module, 618 WTERMSIG() function, os module, 395 X \x escape code, in strings, 28 -x command line option, 173-174 xdrlib module, 587 xml package, 568 XML() function, xml.etree.ElementTree module, 575 XML escaping and unescaping characters, 583 example document, 569 incremental parsing of large files, 579 namespaces in ElementTree module, 578 parsing, 568 XML-RPC, 524 XML-RPC server, multithreaded example, 494 XML-RPC example of, 529 server customization, 530 xml.dom.minidom module, 570 xml.etree.ElementTree module, 573 xml.sax module, 580 writing to a file, 159 xml.sax.saxutils module, 583 WSGI (Web Server Gateway Interface), 540 ‘xmlcharrefreplace’ error handling, 166-167 WSGI XMLGenerator() function, xml.sax.saxutils module, 584 application specification, 540 example of, 541 integration with web frameworks, 543 processing form fields, 541 running a stand-alone server, 542 running in CGI scripts, 542 use of generators for I/O, 165 validation of applications, 543 XMLID() function, xml.etree.ElementTree module, 575 xmlrpc package, 524 xmlrpc.client module, 524 xmlrpc.server module, 527 xmlrpclib module, 524 F h Lib fL B d ff ZipInfo() function, zipfile module 717 XMLRPCServer class, xmlrpc.server module, 527 xor() function, operator module, 274 _ _xor_ _() method, 60 XPATH, 569 xrange type, 38 xrange() compared to a list, 44 xrange() function, 17, 44, 211 and Python 3, 17, 44, 211 XSLT, 569 Y Y2K handling, 407 year attribute, of date objects, 337 yield expressions, 20, 104 yield statement, 19, 53, 102 and context managers, 90 use with I/O, 164-165 Z ZeroDivisionError exception, 87, 215 zfill() method, of strings, 44 zip files and modules, 147 decoding and encoding, 324 use as a code archive, 147 zip() function, 83, 211 and Python 3, 211 future_builtins module, 217 type-conversion example, 37 zipfile module, 324 ZipFile() function, zipfile module, 325 zipimport module, 586 ZipInfo() function, zipfile module, 325 How can we make this index more useful? Email us at indexes@samspublishing.com F h Lib fL B d ff [...]... and Macintosh In addition, the Python website includes links to documentation, how-to guides, and a wide assortment of third-party software This edition of Python Essential Reference comes at a pivotal time in Python s evolution Python 2.6 and Python 3.0 are being released almost simultaneously.Yet, Python 3 is a release that breaks backwards compatibility with prior Python versions As an author and... this edition of the Essential Reference Namely, I have chosen to omit features of Python 2 that have been removed from Python 3 Likewise, I don’t focus on features of Python 3 that have not been back-ported (although such features are still covered in an appendix) As a result, I hope this book can be a useful companion for Python programmers, regardless of what Python version is being used The fourth edition. .. are new to Python and using Python 3, you might want to follow this chapter using Python 2.6 instead.Virtually all the major concepts apply to both versions, but there are a small number of critical syntax changes in Python 3—mostly related to printing and I/O—that might break many of the examples shown in this section Please refer to Appendix A, Python 3,” for further details Running Python Python programs... Introduction T his book is intended to be a concise reference to the Python programming language Although an experienced programmer will probably be able to learn Python from this book, it’s not intended to be an extended tutorial or a treatise on how to program Rather, the goal is to present the core Python language, and the most essential parts of the Python library in a manner that’s accurate and concise.This... 633 634 634 634 Code Migration and 2to3 Porting Code to Python 2.6 Providing Test Coverage Using the 2to3 Tool 634 635 635 A Practical Porting Strategy 637 Simultaneous Python 2 and Python 3 Support Participate 638 638 Index 639 F h Lib fL B d ff About the Author David M Beazley is a long-time Python enthusiast, having been involved with the Python community since 1996 He is probably best known for... Conversion from Python to C 597 Type Conversion from C to Python 602 F h Lib fL B d ff xx Contents Adding Values to a Module Error Handling 605 Reference Counting Threads 604 607 607 Embedding the Python Interpreter An Embedding Template 608 Compilation and Linking 608 608 Basic Interpreter Operation and Setup Accessing Python from C 610 Converting Python Objects to C ctypes 612 Loading Shared Libraries... Running Python Python programs are executed by an interpreter Usually, the interpreter is started by simply typing python into a command shell However, there are many different implementations of the interpreter and Python development environments (for example, Jython, IronPython, IDLE, ActivePython,Wing IDE, pydev, etc.), so you should consult the documentation for startup details.When the interpreter... both Python 2 and 3 Python s interactive mode is one of its most useful features In the interactive shell, you can type any valid statement or sequence of statements and immediately view the results Many people, including the author, even use interactive Python as their desktop calculator For example: >>> 6000 + 4523.50 + 134.12 10657.620000000001 >>> _ + 8192.32 18849.940000000002 >>> When you use Python. .. the reader has prior programming experience with Python or another language such as C or Java In addition, a general familiarity with systems programming topics (for example, basic operating system concepts and network programming) may be useful in understanding certain parts of the library reference Python is freely available for download at http://www .python. org.Versions are available for almost every... Modules Python Services 585 String Processing 586 Operating System Modules Network 585 586 587 Internet Data Handling Internationalization Multimedia Services Miscellaneous 587 587 588 588 III: Extending and Embedding 589 26 Extending and Embedding Python Extension Modules 591 591 An Extension Module Prototype 593 Naming Extension Modules 595 Compiling and Packaging Extensions 596 Type Conversion from Python