core python programming, second edition, fifth printing...

53

Upload: nguyenhanh

Post on 11-May-2018

252 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,
DellUser
Text Box
"Core Python Programming," Second Edition, Fifth Printing, April 2009 Contains the materials: Appendix C, Appendix D, revised Index & Bio
Page 2: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1049

Python 3: The Evolution of a Programming LanguagePython 3.0 represents an evolution of the language that will not execute mostolder code that was written against the 2.x intepreters. This doesn’t mean thatyou won’t recognize the old code any more, or that “major” porting isrequired to make old code work under 3.x. In fact, the new syntax is quitesimilar to that of the past. However, when the print statement no longerexists, it makes it easy to “break” the old code. In this appendix, we discussprint and other 3.x changes, and we shed some light on the “required evolu-tion” that Python must undergo to be better than it was before. Finally, wepresent a few migration tools that may help you make this transition.

Why Is Python Changing?Python is currently undergoing its most significant transformation since itwas released in the early 1990s. Even the revision change from 1.x to 2.x in2000 was relatively mild—Python 2.0 ran 1.5.2 software just fine back then.One of the main reasons for Python’s stability over the years has been the

Chun.book Page 1049 Thursday, April 2, 2009 9:18 AM

Page 3: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1050 Appendix C

steadfast determination of the core development team to preserve backwardcompatibility. Over the years, however, certain “sticky” flaws (issues that stickaround from release to release) were identified by creator Guido van Rossum[Regrets], Andrew Kuchling [Warts], and other users. Their persistencemade it clear that a release with hard changes was needed to ensure that thelanguage evolved. Python 3.0 marks the first time that a Python interpreterhas been released that (deliberately) breaks the backward-compatibilitytrend.

What Is Changing?The changes in Python 3.0 are not mind-boggling—it’s not like you won’t rec-ognize Python any more. The remainder of this appendix gives an overview ofsome of the major changes:

• print becomes print(). • Strings are cast into Unicode by default. • There is a single class type. • The syntax for exceptions has been updated.• Integers have been updated.• Iterables are used everywhere.

print Becomes print()

The switch to print() is easily the change that breaks the most existingPython code. Why is Python changing from a statement to a BIF? Havingprint as a statement is limiting in many regards, as detailed by Guido in his“Python Regrets” talk, which outlined what he feels are shortcomings of thelanguage. In addition, having print as a statement limits improvements to it.However, when print() is available as a function, new keyword parameterscan be added, certain standard behaviors can be overridden with keywordparameters, and print() can be replaced if desired, just like any other BIF.Here are “before” and “after” examples:

Python 2.x >>> i = 1>>> print 'Python' 'is', 'number', iPythonis number 1

Chun.book Page 1050 Thursday, April 2, 2009 9:18 AM

Page 4: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Python 3: The Evolution of a Programming Language 1051

Python 3.x >>> i = 1>>> print('Python' 'is', 'number', i)Pythonis number 1

The omission of a comma between 'Python' and 'is' is deliberate,meant to show you that direct string literal concatenation has not changed.More examples can be found in the “What’s New in Python 3.0” document; inaddition, more information about this change is available in PEP 3105.

Strings: Unicode by Default

The next gotcha that current Python users face is that strings are now Uni-code by default. This change couldn’t come soon enough. There is not oneday that numerous Python developers don’t run into a problem when dealingwith Unicode and regular ASCII strings that looks something like this:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xae' in position 0: ordinal not in range(128)

These types of errors will no longer be an everyday occurrence in 3.x. (Formore information on using Unicode in Python, see the Unicode HOWTOdocument.) With the model adopted by the new version of Python, usersshouldn’t even use those terms (Unicode and ASCII/non-Unicode strings)anymore. The “What’s New in Python 3.0” document sums up this newmodel pretty explicitly.

Python 3.0 uses the concepts of text and (binary) data instead of Unicodestrings and 8-bit strings. All text is Unicode; however, encoded Unicode isrepresented as binary data. The type used to hold text is str, and the typeused to hold data is bytes.

As far as syntax goes, because Unicode is now the default, the leading u orU is deprecated. Similarly, the new bytes objects require a leading b or B forits literals (more information can be found in PEP 3112).

Table C.1 compares the various string types, showing how they will changefrom Python 2.x to 3.x. The table also includes a mention of the new mutablebytearray type.

Chun.book Page 1051 Thursday, April 2, 2009 9:18 AM

Page 5: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1052 Appendix C

Single Class Type

Prior to Python 2.2, Python’s objects didn’t behave like classes in other lan-guages: Classes were “class” objects and instances were “instance” objects.This is in stark contrast to what people perceive as “normal”: Classes aretypes and instances are objects of such types. Because of this “flaw,” youcould not subclass data types and modify them. In Python 2.2, the core devel-opment team came up with “new-style classes,” which act more like whatpeople expect. Furthermore, this change meant that regular Python typescould be subclassed—a change described in Guido’s “Unifying Types andClasses in Python 2.2” essay. Python 3.0 supports only new-style classes.

Updated Syntax for Exceptions

Exception HandlingIn the past, the syntax to catch an exception and the exception argument/instance had the following form:

except ValueError, e:

To catch multiple exceptions with the same handler, the following syntaxwas used:

except (ValueError, TypeError), e:

The required parentheses confused some users, who often attempted towrite invalid code:

except ValueError, TypeError, e:

The (new) as keyword is intended to ensure that you do not become con-fused by the comma in the original syntax; however, the parentheses are stillrequired when you’re trying to catch more than one type of exception using

Table C.1 Strings in Python 2 and 3

2.x 3.x Mutable?

str ("") bytes (b"") no

unicode (u"") str ("") no

N/A bytearray yes

Chun.book Page 1052 Thursday, April 2, 2009 9:18 AM

Page 6: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Python 3: The Evolution of a Programming Language 1053

the same handler. Here are two equivalent examples of the new syntax thatdemonstrate this change:

except ValueError as e:

except (ValueError, TypeError) as e:

Python 2.6 accepts both forms when creating exception handlers, therebyfacilitating the porting process. More information about this change can befound in PEP 3110.

Exception RaisingThe most popular syntax for raising exceptions in Python 2.x looks like this:

raise ValueError, e

To truly emphasize that you are creating an instance of an exception, the onlysyntax supported in Python 3.x is this:

raise ValueError(e)

Updates to Integers

Single Integer TypePython’s two different integer types, int and long, began their unification inPython 2.2. That change is now almost complete, with the “new” int behav-ing like a long. As a consequence, OverflowError exceptions no longeroccur when you exceed the native integer size, and the trailing L has beendropped. This change is outlined in PEP 237. long still exists in Python 2.xbut has disappeared in Python 3.0.

Changes to DivisionThe current division operator (/) doesn’t give the expected answer for thoseusers who are new to programming, so it has been changed to do so. If thischange has brought any controversy, it is simply that programmers are usedto the floor division functionality. To see how the confusion arises, try to con-vince a newbie to programming that 1 divided by 2 is 0 (1 / 2 == 0). The sim-plest way to describe this change is with examples. Following are someexcerpted from “Keeping Up with Python: The 2.2 Release,” found in theJuly 2002 issue of Linux Journal. You can also find out more about thisupdate in PEP 238.

Chun.book Page 1053 Thursday, April 2, 2009 9:18 AM

Page 7: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1054 Appendix C

Classic Division The default Python 2.x division operation works this way: Given two integeroperands, / performs integer floor division (truncates the fraction as in theearlier example). If there is at least one float involved, true division occurs:

>>> 1 / 2 # floor0>>> 1.0 / 2.0 # true0.5

True Division In Python 3.x, given any two numeric operands, / will always return a float:

>>> 1 / 2 # true0.5>>> 1.0 / 2.0 # true0.5

To try true division starting in Python 2.2, you can either import divisionfrom __future__ module or use the -Qnew switch.

Floor Division The double-slash division operator (//) was added in Python 2.2 to alwaysperform floor division regardless of operand type and to begin the transitionprocess:

>>> 1 // 2 # floor0>>> 1.0 // 2.0 # floor0.0

Binary and Octal LiteralsThe minor integer literal changes were added in Python 2.6 to make literalnondecimal (hexadecimal, octal, and new binary) formats consistent. Hexrepresentation stayed the same, with its leading 0x or 0X (where the octalhad formerly led with a single 0). This format proved confusing to someusers, so it has been changed to 0o for consistency. Instead of 0177, you mustuse 0o177 now. Finally, the new binary literal lets you provide the bits of aninteger value, prefixed with a leading 0b, as in 0b0110. Python 3.0 does notaccept 0177. More information on integer literals updates can be found inPEP 3127.

Chun.book Page 1054 Thursday, April 2, 2009 9:18 AM

Page 8: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Python 3: The Evolution of a Programming Language 1055

Iterables Everywhere

Another theme inherent to Python 3.x is memory conservation. Using itera-tors is much more efficient than maintaining entire lists in memory, especiallywhen the target action on the objects in question is iteration. There’s no needto waste memory when it’s not necessary. Thus, in Python 3.x, code thatreturned lists in earlier versions of the language no longer does so. For exam-ple, the functions map(), filter(), range(), and zip(), plus the dictio-nary methods keys(), items(), and values(), all return some sort ofiterator. Yes, this syntax may be more inconvenient if you want to glance atyour data, but it’s better in terms of resource consumption. The changes aremostly under the covers—if you only use the functions’ return values to iter-ate over, you won’t notice a thing!

Migration ToolsAs you have seen, most of the Python 3.x changes do not represent some wildmutation of the familiar Python syntax. Instead, the changes are just enoughto break the old code base. Of course, the changes affect users, so a goodtransition plan is clearly needed—and most good ones come with good toolsor aids to help you out. For example, both the 2to3 code converter and thelatest Python 2.x release (2.6 at the time of this writing) may facilitate thetransition.

2to3 Tool

The 2to3 tool will take Python 2.x code and attempt to generate a workingequivalent in Python 3.x. Here are some of the actions it performs:

• Converts a print statement to a print() function • Removes the L long suffix • Replaces <> with != • Changes backtick-quoted strings (`...`) to repr(...)

This tool does a lot of the manual labor—but not everything; the rest is upto you. You can read more about porting suggestions and the 2to3 tool in the“What’s New in Python 3.0” document as well as at the tool’s Web page (http://docs.python.org/3.0/library/2to3.html).

Chun.book Page 1055 Thursday, April 2, 2009 9:18 AM

Page 9: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1056 Appendix C

Python 2.6

Because of the compatibility issue, the releases of Python that lead up to 3.0play a much more significant role in the transition. Of particular note isPython 2.6, the first and most pivotal of such releases. For users, it representsthe first time that they can start coding against the 3.x family of releases, asmany 3.x features have been backported to 2.x. Whenever possible, Python2.6 incorporates new features and syntax from version 3.0 while remainingcompatible with existing code by not removing older features or syntax. Suchfeatures are described in the “What’s New in Python 2.6” document. Wedetail some of these 2.6 migration features in Appendix D.

ConclusionOverall, the changes outlined in this appendix do have a high impact in termsof updates required to the interpreter but should not radically change theway programmers write their Python code. It’s simply a matter of changingold habits, such as using parentheses with print—or rather, print(). Onceyou’ve gotten these changes under your belt, you’re well on your way to beingable to effectively jump to the new platform. It may be a bit startling at first,but these changes have been coming for some time now. Don’t panic: Python2.x will live on for a long time to come. The transition will be slow, deliberate,pain resistant, and even keeled. Welcome to the dawn of the next generation!

ReferencesAndrew Kuchling, “Python Warts,” July 2003, http://web.archive.org/web/20070607112039, http://www.amk.ca/python/writing/warts.html

A. M. Kuchling, “What’s New in Python 2.6,” December 2008, http://docs.python.org/whatsnew/2.6.html

Wesley J. Chun, “Keeping Up with Python: The 2.2 Release,” July 2002,http://www.linuxjournal.com/article/5597

PEP Index, http://www.python.org/dev/peps

“Unicode HOWTO,” December 2008, http://docs.python.org/3.0/howto/unicode.html

Chun.book Page 1056 Thursday, April 2, 2009 9:18 AM

Page 10: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Python 3: The Evolution of a Programming Language 1057

Guido van Rossum, “Python Regrets,” July 2002, http://www.python.org/doc/essays/ppt/regrets/PythonRegrets.pdf

Guido van Rossum, “Unifying Types and Classes in Python 2.2,” April 2002,http://www.python.org/2.2.3/descrintro.html

Guido van Rossum, “What’s New in Python 3.0,” December 2008, http://docs.python.org/3.0/whatsnew/3.0.html

Chun.book Page 1057 Thursday, April 2, 2009 9:18 AM

Page 11: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Chun.book Page 1058 Thursday, April 2, 2009 9:18 AM

Page 12: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1059

Migrating to Python 3 Starts with 2.6Python 3: The Next GenerationPython is currently undergoing its most significant transformation since itwas first released back in the winter of 1991. Python 3 is backward incompat-ible with all older versions, so porting will be a more significant issue than inthe past.

Unlike other end-of-life efforts, however, Python 2.x will not disappearanytime soon. In fact, the remainder of the 2.x series will be developed inparallel with 3.x, thereby ensuring a smooth transition from the current tonext generation. Python 2.6 is the first of these final 2.x releases.

Hybrid 2.6 as Transition Tool

Python 2.6 is a hybrid interpreter. That means it can run both 1.x and 2.x soft-ware as well as some 3.x code. Some will argue that Python releases datingback to 2.2 have already been mixed interpreters because they support creationof both classic classes as well as new-style classes, but that is as far as they go.

Chun.book Page 1059 Thursday, April 2, 2009 9:18 AM

Page 13: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1060 Appendix D

Version 2.6 is many steps ahead, and if they are eventually released, versions2.7 and beyond will be even more so. The 2.6 release is the first version withspecific 3.0 features backported to it. The most significant of these featuresare summarized here:

• Integers• Single integer type • New binary and modified octal literals • Classic or true division• The -Q division switch

• Built-in functions• print or print() • reduce() • Other updates

• Object-oriented programming• Two different class objects

• Strings• bytes literals • bytes type

• Exceptions• Handling exceptions• Raising exceptions

• Other transition tools and tips• Warnings: the -3 switch • 2to3 tool

This appendix does not discuss other new features of 2.6 that are stand-alone features, meaning they do not have any consequences for porting appli-cations to 3.x. So without further ado . . .

IntegersPython integers face several changes in version 3.x and beyond, relating totheir types, literals, and the integer division operation. We describe each ofthese changes next, highlighting the role that 2.6 plays in terms of migration.

Single Integer Type

Previous versions of Python featured two integer types, int and long. Theoriginal ints were limited in size to the architecture of the platform on which

Chun.book Page 1060 Thursday, April 2, 2009 9:18 AM

Page 14: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Migrating to Python 3 Starts with 2.6 1061

the code ran (i.e., 32-bit, 64-bit), while longs were unlimited in size exceptin terms of how much virtual memory the operating system provided. Theprocess of unifying these two types into a single int type began in Python 2.2and will be complete in version 3.0.a The new single int type will be unlim-ited in size, and the previous L or l designation for longs is removed. Youcan read more about this change in PEP 237.

As of 2.6, there is little trace of long integers, save for the support of thetrailing L. It is included for backward-compatibility purposes, to support allcode that uses longs. Nevertheless, users should be actively purging longintegers from their existing code and should no longer use longs in any newcode written against Python 2.6+.

New Binary and Modified Octal Literals

Python 3 features a minor revision to the alternative base format for integers.It has basically streamlined the syntax to make it consistent with the existinghexadecimal format, prefixed with a leading 0x (or 0X for capital letters)—forexample, 0x80, 0xffff, 0xDEADBEEF.

A new binary literal lets you provide the bits to an integer number, pre-fixed with a leading 0b (e.g., 0b0110). The original octal representationbegan with a single 0, but this format proved confusing to some users, so ithas been changed to 0o to bring it in line with hexadecimal and binary literalsas just described. In other words, 0177 is no longer allowed; you must use0o177 instead. Here are some examples:

Python 2 >>> 0177127

Python 3 (including versions 2.6+) >>> 0o177127>>> 0b01106

Both the new binary and modified octal literal formats have been back-ported to 2.6 to help with migration. In fact, 2.6, in its role as a transition tool,

a. The bool type also might be considered part of this equation, because bools behave like 0and 1 in numerical situations rather than having their natural values of False and True,respectively.

Chun.book Page 1061 Thursday, April 2, 2009 9:18 AM

Page 15: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1062 Appendix D

accepts both octal formats, whereas 3.0 no longer accepts the old 0177 format.More information on the updates to integer literals can be found in PEP 3127.

Classic or True Division

A change that has been a long time coming, yet remains controversial tomany, is the change to the division operator (/). The traditional division oper-ation works in the following way: Given two integer operands, / performs inte-ger floor division. If there is at least one float involved, true division occurs:

Python 2.x: Classic Division >>> 1 / 2 # floor0>>> 1.0 / 2.0 # true0.5>>> 1.0 / 2 # true (2 is internally coerced to

float)0.5

In Python 3, the / operator will always return a float regardless of operandtype:

Python 3.x: True Division >>> 1 / 2 # true0.5>>> 1.0 / 2 # true0.5

The double-slash division operator (//) was added as a proxy in Python 2.2 toalways perform floor division regardless of the operand type and to begin thetransition process:

Python 2.2+ and 3.x: Floor Division >>> 1 // 2 # floor0>>> 1.0 // 2 # floor0.0

Using // will be the only way to obtain floor division functionality in 3.x. Totry true division in Python 2.2+, you can add the line from __future__import division to your code, or use the -Q command-line option (dis-cussed next). (There is no additional porting assistance available in Python 2.6.)

Chun.book Page 1062 Thursday, April 2, 2009 9:18 AM

Page 16: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Migrating to Python 3 Starts with 2.6 1063

Python 2.2+: Division Command-Line Option If you do not wish to import division from __future__ module in your codebut want true division to always prevail, you can use the -Qnew switch. Thereare also other options for using -Q, as summarized in Table D.1.

For example, the -Qwarnall option is used in the Tools/scripts/fixdiv.py script found in the Python source distribution.

As you may have guessed by now, all of the transition efforts have already beenimplemented in Python 2.2, and no new 2.6-specific functionality has beenadded with respect to Python 3 migration. Table D.2 summarizes the divisionoperators and their functionality in the various Python releases.

You can read more about the change to the division operator in PEP 238 aswell as in an article called “Keeping Up with Python: The 2.2 Release” Iwrote for Linux Journal back in July 2002.

Table D.1 Division Operation -Q Command-Line Options

Option Description

old Always perform classic division

new Always perform true division

warn Warn against int/int and long/long operations

warnall Warn against all use of /

Table D.2 Python Release Default Division Operator Functionality

Operator 2.1- 2.2+ 3.xa

a. The “3.x” column also applies to Python 2.2+ with -Qnew or the __future__.division import.

/ Classic Classic True

// Not applicable Floor Floor

Chun.book Page 1063 Thursday, April 2, 2009 9:18 AM

Page 17: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1064 Appendix D

Built-in Functions

print Statement or print() Function

It’s no secret that one of the most common causes of breakage betweenPython 2.x and 3.x applications is the change in the print statement, whichbecomes a built-in function in version 3.x. This change allows print() to bemore flexible, upgradeable, and swappable if desired.

Python 2.6+ supports either the print statement or the print() built-infunction. The default is the former usage, as it should be in a 2.x language. Todiscard the print statement and go with only the function in a “Python 3mode” application, you would simply import print_function from__future__:

>>> print 'foo', 'bar'foo bar>>>>>> from __future__ import print_function>>> print<built-in function print>>>> print('foo', 'bar')foo bar>>> print('foo', 'bar', sep='-')foo-bar

The preceding example demonstrates the power of print() being a func-tion. Using the print statement, we display the strings "foo" and "bar" tothe user, but we cannot change the default delimiter or separator betweenstrings, which is a space. In contrast, print()makes this functionality avail-able in its call as the argument sep, which replaces the default—and allowsprint to evolve and progress.

Note that this is a “one-way” import, meaning that there is no way to revertprint() back to a function. Even issuing a "del print_function" willnot have any effect. This major change is detailed in PEP 3105.

reduce() Moved to functools Module

In Python 3.x, the reduce()function, which is neither readily understoodnor commonly used by many programmers today, has been “demoted” (muchto the chagrin of many Python functional programmers) from being a built-infunction to become a functools module attribute. It is available in func-tools beginning in 2.6.

Chun.book Page 1064 Thursday, April 2, 2009 9:18 AM

Page 18: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Migrating to Python 3 Starts with 2.6 1065

>>> from operator import add>>> reduce(add, range(5))10>>>>>> import functools>>> functools.reduce(add, range(5))10

Other Updates

One key theme in Python 3.x is the migration to greater use of iterators, espe-cially for built-in functions and methods that have historically returned lists.Still other iterators are changing because of the updates to integers. The fol-lowing are the most high-profile built-in functions changed in Python 3.x:

• range() • zip() • map() • filter() • hex() • oct()

Starting in Python 2.6, programmers can access the new and updatedfunctions by importing the future_builtins module. Here is an exampledemonstrating both the old and new oct() and zip() functions:

>>> oct(87)'0127'>>>>>> zip(range(4), 'abcd')[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]>>> dict(zip(range(4), 'abcd')){0: 'a', 1: 'b', 2: 'c', 3: 'd'}>>>>>> import future_builtins>>> future_builtins.oct(87)'0o127'>>>>>> future_builtins.zip(range(4), 'abcd')<itertools.izip object at 0x374080>>>> dict(future_builtins.zip(range(4), 'abcd')){0: 'a', 1: 'b', 2: 'c', 3: 'd'}

If you want to use only the Python 3.x versions of these functions in yourcurrent Python 2.x environment, you can override the old ones by importing

Chun.book Page 1065 Thursday, April 2, 2009 9:18 AM

Page 19: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1066 Appendix D

all the new functions into your namespace. The following example demon-strates this process with oct():

>>> from future_builtins import *>>> oct(87)'0o127'

Object-Oriented Programming: Two Different Class ObjectsPython’s original classes are now called “classic classes.” They had many flawsand were eventually replaced by “new-style” classes. The transition began inPython 2.2 and continues today.

Classic classes have the following syntax:

class ClassicClass: pass

New-style classes have this syntax:

class NewStyleClass(object): pass

New-style classes feature so many more advantages than classic classesthat the latter have been preserved only for backward-compatibility purposesand are eliminated entirely in Python 3. With new-style classes, types andclasses are finally unified (see Guido’s “Unifying Types and Classes in Python2.2” essay as well as PEP 252 and PEP 253).

There are no new changes added in Python 2.6 for migration purposes.Just be aware that all 2.2+ versions serve as hybrid interpreters, allowing forboth class objects and instances of those classes. In Python 3, both syntaxesshown in the preceding examples result only in new-style classes being cre-ated. This behavior does not pose a serious porting issue, but you do need tobe aware that classic classes don’t exist in Python 3.

StringsOne especially notable change in Python 3.x is that the default string type ischanging. Python 2.x supports both ASCII and Unicode strings, with ASCIIbeing the default. This support is swapped in Python 3: Unicode becomes the

Chun.book Page 1066 Thursday, April 2, 2009 9:18 AM

Page 20: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Migrating to Python 3 Starts with 2.6 1067

default, and ASCII strings are now called bytes. The bytes data structurecontains byte values and really shouldn’t be considered a string (anymore) asmuch as it is an immutable byte array that contains data.

Current string literals will now require a leading b or B in Python 3.x, andcurrent Unicode string literals will drop their leading u or U. The type andbuilt-in function names will change from str to bytes and from unicode tostr. In addition, there is a new mutable “string” type called bytearray that,like bytes, is also a byte array, only mutable.

You can find out more about using Unicode strings in the HOWTO andlearn about the changes coming to string types in PEP 3137. Refer to TableC.1 for a chart on the various string types in both Python 2 and Python 3.

bytes Literals

To smooth the way for using bytes objects in Python 3.x, you can optionallyprepend a regular ASCII/binary string in Python 2.6 with a leading b or B,thereby creating bytes literals (b'' or B'') as synonyms for str literals(''). The leading indicator has no bearing on any str object itself or any ofthe object’s operations (it is purely decorative), but it does prepare you for sit-uations in Python 3 where you need to create such a literal. You can find outmore about bytes literals in PEP 3112

bytes is str It should not require much of a stretch of the imagination to recognize that ifbytes literals are supported, then bytes objects themselves need to exist inPython 2.6. Indeed, the bytes type is synonymous with str, so much so that

>>> bytes is strTrue

Thus you can use bytes or bytes()in Python 2.6 wherever you use str orstr(). Further information on bytes objects can be found in PEP 358.

ExceptionsPython 2.6 has several features that allow for porting of exception handlingand raising exceptions in Python 3.x.

Chun.book Page 1067 Thursday, April 2, 2009 9:18 AM

Page 21: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1068 Appendix D

Handling Exceptions (Using as)

Python 3’s syntax for catching and handling a single exception looks like this:

except ValueError as e:

The e variable contains the instance of the exception that provides the reasonwhy the error was thrown. It is optional, as is the entire as e phrase. Thusthis change really applies only to those users who save this value.

The equivalent Python 2 syntax uses a comma instead of the as keyword:

except ValueError, e:

This change was made in Python 3.x because of the confusion that occurswhen programmers attempt to handle more than one exception with thesame handler.

To catch multiple exceptions with the same handler, beginners often writethis (invalid) code:

except ValueError, TypeError, e:

In fact, if you are trying to catch more than one exception, you need to usea tuple containing the exceptions:

except (ValueError, TypeError), e:

The as keyword in Python 3.0 (and 2.6+) is intended to ensure that thecomma in the original syntax is no longer a source of confusion. However, theparentheses are still required when you are trying to catch more than onetype of exception using the same handler:

except (ValueError, TypeError) as e:

For porting efforts, Python 2.6+ accepts either the comma or as whendefining exception handlers that save the instance. In contrast; only the idiomwith as is permitted in Python 3. More information about this change can befound in PEP 3110.

Raising Exceptions

The change in raising exceptions found in Python 3.x really isn’t a change atall; in fact, it doesn’t even have anything to do with the transition efforts asso-ciated with Python 2.6. Python 3’s syntax for raising exceptions (providing theoptional reason for the exception) looks like this:

raise ValueError('Invalid value')

Chun.book Page 1068 Thursday, April 2, 2009 9:18 AM

Page 22: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Migrating to Python 3 Starts with 2.6 1069

Long-time Python users have probably been using the following idiom(although both approaches are supported in all 2.x releases):

raise ValueError, 'Invalid value'

To emphasize that raising exceptions is equivalent to instantiating anexception class and to provide some additional flexibility, Python 3 supportsonly the first idiom. The good news is that you don’t have to wait until youadopt 2.6 to start using this technique—the syntax with parentheses has actu-ally been valid since the Python 1 days.

Other Transition Tools and TipsIn addition to Python 2.6, developers have access to an array of tools that canmake the transition to Python 3.x go more smoothly—in particular, the -3switch (which provides obsolescence warnings) and the 2to3 tool (read moreabout it at http://docs.python.org/3.0/library/2to3.html). However, the mostimportant tool that you can “write” is a good transition plan. In fact, there’s nosubstitute for planning.

Clearly, the Python 3.x changes do not represent some wild mutation ofthe familiar Python syntax. Instead, the variations are just enough to breakthe old code base. Of course, the changes will affect users, so a good transi-tion plan is essential. Most good plans come with tools or aids to help you outin this regard. The porting recommendations in the “What’s New in Python3.0” document specifically state that good testing of code is critical, in addi-tion to the use of key tools (i.e., the 2to3 code conversion tool and Python2.6). Without mincing words, here is exactly what is suggested at http://docs.python.org/3.0/whatsnew/3.0.html#porting-to-python-3-0:

1. (Prerequisite) Start with excellent test coverage. 2. Port to Python 2.6. This should involve no more work than the

average port from Python 2.x to Python 2.(x+1). Make sure that all your tests pass.

3. (Still using 2.6) Turn on the -3 command-line switch. It enables warnings about features that will be removed (or will change) in Python 3.0. Run your test suite again, and fix any code that generates warnings. Make sure that all your tests still pass.

4. Run the 2to3 source-to-source translator over your source code tree. Run the result of the translation under Python 3.0. Manually fix any remaining issues, and continue fixing problems until all tests pass again.

Chun.book Page 1069 Thursday, April 2, 2009 9:18 AM

Page 23: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1070 Appendix D

ConclusionWe know big changes are coming in the next generation of Python, simplybecause 3.x code is backward incompatible with any older releases. Thechanges, although significant, won’t require entirely new ways of thinking forprogrammers—though there is obvious code breakage. To ease the transitionperiod, current and future releases of the remainder of the 2.x interpreterswill contain 3.x-backported features.

Python 2.6 is the first of the “dual-mode” interpreters that will allow usersto start programming against the 3.0 code base. You can read more about allof the new 2.6 features (not just the ones applicable to the 3.x transition) inthe “What’s New in Python 2.6” document. Python 2.6 runs all 2.x softwareas well as understands some 3.x code. In this way, the 2.6 release helps sim-plify the porting and migration process and eases users gently into the nextgeneration of Python programming.

Online ReferencesWesley J. Chun, “Keeping Up with Python: The 2.2 Release,” July 2002,http://www.linuxjournal.com/article/5597

A. M. Kuchling (amk at amk.ca), “What’s New in Python 2.6,” December2008, http://docs.python.org/whatsnew/2.6.html

PEP Index, http://www.python.org/dev/peps

Unicode HOWTO, http://docs.python.org/3.0/howto/unicode.html

Guido van Rossum (guido at python.org), “Unifying Types and Classes inPython 2.2” (second draft, published for 2.2.3), April 2002, http://www.python.org/2.2.3/descrintro.html

Guido van Rossum (guido at python.org). “What’s New in Python 3.0,”December 2008, http://docs.python.org/3.0/whatsnew/3.0.html

Chun.book Page 1070 Thursday, April 2, 2009 9:18 AM

Page 24: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1071

@ (“at-sign”), 422& (ampersand), 277&= (ampersand equal sign), 279* (asterisk), 35, 680–681\ (backslash), 62^ (caret), 278, 678, 680$ (dollar sign), 678, 679. (dot), 678, 689–690** (double asterisk), 35, 133, 140`` (double single quotes), 104- (hyphen), 680- (minus sign), 278. (period), 678, 689–690|= (pipe equals), 277| (pipe symbol), 277, 678, 689+ (plus sign), 35, 159, 176–177, 213–214, 233, 681# (pound sign), 34, 62/ (slash), 35, 130–131, 569{ } (braces), 681[ ] (brackets), 259, 679–680, 690

> , >= (greater than), 277< , <= (less than), 277<>, != (“not equals” comparison operators), 362to3 tool, 1055, 1069–1070

A\A (special character), 678abs( ) built-in function, 143absolute import statement, 494–495abstract methods, 519abstraction, 382, 516access models, 115–116access modes, 326, 327ActiveX, 989. See also COM (Component Object

Model)adapters (database)

about, 932–933definition of, 923examples of, 934–945

Chun.book Page 1071 Thursday, April 2, 2009 9:18 AM

Page 25: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1072 Index

adapters (continued)Gadfly, 939–945MySQL, 934–936, 939–945ORMs, 946PostgreSQL, 936–937related modules, 958–959SQLite, 937–945

add( ) method, 281, 282, 284addition ( + ) operator, 35AF_INET (socket family name), 716, 718, 730AF_UNIX (socket family name), 715, 718, 730aggregation, 517all( ) built-in function, 310alternation ( | ), 678American Standard Code for Information

Interchange. See ASCIIancestors, 545, 546and keyword, 36, 100, 101anonymous functions, 439–441any( ) built-in function, 310AOP (aspect-oriented programming), 424API (Application Programming Interface),

489, 922–923, 985–989apilevel, 924–925Application Programming Interface. See APIapply( ) built-in function, 414, 441, 442arguments

class methods, 543command-line, 338–339for exceptions, 372–375

arguments (default)functions, 49, 413, 429–442GUI development, 824Tkinter widgets, 824using with instantiation, 532–533

arguments (function)decorators with/without, 423–424default, 49, 413, 429–442dictionary, keyword variable, 434–436formal, 428–432grouped, 413–414keyword, 412–413, 434–436optional, 48positional, 428–429variable argument objects, 436–439variable-length, 433–439

arguments (method), 532–533arithmetic game (example), 415–417

arithmetic operators. See mathematical operatorsarrays. See lists; tuplesASCII, 144–145, 197–198, 497, 698aspect-oriented programming (AOP), 424assert statement, 389–390AssertionError exceptions, 390, 391assigning/assignment

augmented, 37, 65–66dictionaries, 255lists, 209multiple, 66“multuple,” 66–67numbers, 121operators, 64–67set types, 273, 274strings, 168–169tuples, 231variables, 37, 64–67

association (classes), 517asterisk operator ( * ), 680–681async∗ module, 741atomic storage, 112“at-sign” ( @ ), 422attr( ) built-in functions, 560–561attribute(s)

built-in functions, 629built-in methods, 632built-in types, 536–537class, 520–525complex number, built-in, 126–127definition of, 46double underscore ( __ ), 586file, 336–337functions, 420–421, 629, 630importing, 496interfaces to data, 516__metaclass__, 610–611methods, 632, 633module, 333multi-platform development, 333naming, 478, 513object, 90privacy, 585–586Queue module, 811simple lookup example, 554–555socket module, 730–731special class, 523–525special methods for customizing classes, 566

Chun.book Page 1072 Thursday, April 2, 2009 9:18 AM

Page 26: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Index 1073

user-defined function, 630user-defined methods, 633using files to store, 604using local to substitute for module, 81

AttributeError exception, 363–364, 391, 590, 600

augmented assignment, 37, 65–66, 569, 574–575authentication handler for HTTP, 867–869auto-loaded modules, 496

B\b, \B (special characters), 679backbone (network), 858backslash ( \ ), 62bank tellers server example, 713–714base class, 50. See also parent/parent classbase representation, 143–144BaseException, 371, 372, 391, 394BaseHTTPServer module, 907–909, 911bases argument, 504__bases__ attribute, 524, 525, 548BASIC language, 10Beazley, David, 981BIFs. See functions (built-in)BIMs. See methods (built-in)binary function, 447–448binary literals, 1054, 1061–1062binding

methods, 540–541namespaces, 480

bit operators (integer-only), 135–136Boa Constructor, 849boilerplate, 968–974bool( ) factory function, 137, 138Boolean operators (and, or, not), 36, 100–101, 292Boolean types, 93, 123, 145–147bound methods, 522, 541brace operators ( { } ), 681bracket symbols ( [ ] ), 679–680, 690break statement, 304–305, 308BSD Unix, 12, 14, 16–17, 715buffering (file), 327, 328building Python, 13__builtin__, 480–481, 496built-in attributes, 126–127, 336–337built-in exceptions, 391–393, 1040–1042built-in functions. See functions (built-in)built-in methods (BIMs). See methods (built-in)

“built-in” names, 69built-in types, 91, 536–537__builtins__, 69, 480–481, 496Button widget (Tk), 825–831byte type, 116bytes type, 1051–1052, 1067–1068bytearray type, 1051–1052, 1067–1068

CC language

conversion to/from, 969, 970“dangling else” statements, 293extensions written in, 8fopen( ), 326–327Python and, 6, 8–9, 23, 26varargs, 433

C# language, 27, 969. See also IronPythonC++ language, 8, 23, 25, 26, 969, 970__call__( ) special method, 634–635“call by reference,” 48callable( ) built-in function, 636, 637, 831callable class, 803–805callbacks, 823, 824calling functions, 49, 412–417

arguments, 412–414, 436–439built-in, 427default arguments, 413example, 415–417function operator, 412grouped arguments, 413–414keyword arguments, 412–413logging with closures, 461–463with variable argument objects, 436–439

calling modules, 52–53Canvas widget (Tk), 825caret symbol ( ^ ), 678case statement, proxy for, 294–295case-insensitive import, 496–497case-sensitive identifiers, 68casting of sequences, 165–166CGI (Common Gateway Interface)

about, 875–877advanced example of, 896–906applications, 877–892building applications, 878–892cgi module, 878cookies, 895–897, 900–901, 906creating static form Web page, 879–881

Chun.book Page 1073 Thursday, April 2, 2009 9:18 AM

Page 27: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1074 Index

CGI (continued)file uploading, 894generating form page, 882–886generating results page, 881–886HTTP headers, 882multipart form submission, 894multivalued fields, 895setting up a Web server, 878–879Unicode, 892–893user inputs/error processing, 886–892Web servers, 878–879, 906–909

cgi module, 878CGIHTTPServer module, 907, 908, 911char type (unsupported), 116, 207character(s)

accessing, in strings, 169matching any single ( . ), 678, 689–690removing, 169–170

character classes ( [ ] ), 679–680, 690characters, special, 208

and ASCII characters, 698escaping using triple quotes, 38, 192–193regular expressions, 676–677, 682, 690–693repetition/grouping and, 690–693representing character sets, 682strings, 192–193

Checkbutton widget (Tk), 825child/child class, 545–548, 554, 555, 586, 823chr( ) built-in function, 144, 145, 187class attributes, 520–525

accessing, 537–539__bases__, 524, 525, 548__class__, 524, 525data, 520–521determining, 522–523__dict__, 522–525__doc__, 524, 525instance attributes vs., 537–540methods, 521–522modifying, 540__module__ attribute, 524, 525persistence, 539–540privacy of, 586__slots__, 597special, 523–525

class definition, 510–511class keyword, 50, 504class variables, 521

classes, 50–52about, 504–507, 518–520built-in functions, 558–564callable objects, 634composition, 544–545creating, 51–52, 510–511, 519customizing with special methods, 564–585declaring, 50–51, 72, 518–520definition vs. declaration of, 519–520methods, 507–512, 521–522mix-in, 556as namespace containers, 506naming, 513related modules, 615–617Web server, 909wrapping, 587

classic classes, 503, 504, 542–543, 554–558. See also new-style classes

classic division operator ( / ), 35, 130–131, 569classmethod( ) built-in function, 543clause, 63clear( ) method, 281, 282, 284client data, 875–878clients

FTP, 752–754GUI applications as, 821Internet, 747–748NNTP, 760–765POP3, 775–777SMTP, 775–777TCP, 723–726, 736, 738–740twisted reactor TCP, 738–740UDP, 728–729Web, 855–857, 859–875Windows, 713

client/server architecture, 711–715, 821, 855–856

client-side programming, 989–991close( ) built-in method, 332, 335closures, 422, 456–463, 680–681cmp( ) built-in function, 102, 103, 136, 137,

184–185, 215–216, 260–262code

commenting, 34–35indenting, 41integration of, 963interpreted/byte-compiled, 11profiling of, 8, 84–85, 965

Chun.book Page 1074 Thursday, April 2, 2009 9:18 AM

Page 28: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Index 1075

running example, 22runtime generation/execution of, 642–649skipping, 365wrapping, in boilerplate, 968–974

code objects, 94, 635–636code reuse, 7, 964codecs, 199, 205coerce( ) built-in function, 139, 143collisions (key), 268–269colocated servers, 857, 858columns (database), 920COM (Component Object Model), 989–991command line

arguments, 338–339, 490FTP client program, 755options, 15running Python from, 14–17switches, 653

commands (database), 920comments, 34–35, 62, 69–70Common Gateway Interface. See CGIcompile( ) function, 94, 636, 637–638,

684–686compiling, 11, 685–686, 964, 965, 974–975complex( ) factory function, 137, 138complex numbers, 37, 38, 126–127complex statements, 62–63Component Object Model. See COMcomposite objects, 112composition, 516–517, 544–545compound objects, 112compound statements, 62–63concatenation ( + ) sequence operator, 159,

176–177, 213–214, 233conditional code execution, 647–649conditional statements, 291–296, See also while

statement (loops). See also loopsauxiliary statements for, 308conditional expressions, 295–296elif (aka else-if) statement, 294–295, 308else statement, 292–294, 307–308, 378if statement, 41–42if statement, 291–292multiple, 292and pass statement, 306–307single statements suites, 292switch/case statement proxy, 294–295

connect( ) function, 925–927

connection objects, 927–928connectionless sockets, 717–718connection-oriented sockets, 716–717constructors, 510, 511, 527–529, 532–533, 930–931container storage, 112context expression (context_expr), 384context management (for exceptions), 382–385continuation (exception handling), 361continuation ( \ ) statements, 62continue statement, 305–306, 308conversion

ASCII functions for, 144–145codes, 969, 970factory functions, 137–138sequences, 165–166symbols, 178–181, 1029

cookies, 856, 895–897, 900–901, 906copy( ) function/method, 240–241, 267, 283counting loops, 297couroutines, 467–468cPickle, 349, 350cProfile module, 85CPython, 26, 27credit card transaction data example, 375–378,

380–381cross-product generator expressions example, 317currying, 450cursor (database), 920cursor objects, 929–930customizing and extensions, 964CWI (Centrum voor Wiskunde en Informatica), 6cycles (import), 497–500cyclic reference, 79

Ddaemon threads, 801“dangling else”, avoiding, 63, 293–294data attributes. See attribute(s)data descriptors, 599data hiding, 516data structures, 223–230database programming, 919–959. See also DB-API

(Python Database Application Programmer’s Interface)about, 920–922adapter examples, 934–945components of databases, 920Gadfly, 923, 939–945

Chun.book Page 1075 Thursday, April 2, 2009 9:18 AM

Page 29: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1076 Index

database programming (continued)MySQL, 920, 934–936, 939–945operations/SQL, 920–922ORMs, 947–957, 959persistent storage, 919PostgreSQL adapter example, 936–937Python interfaces for relational databases,

931–932related modules, 958–959SQL, 920–922, 946SQLAlchemy, 946, 947–950, 953–955SQLite, 937–945SQLObject, 946, 951–953, 955–957storage mechanisms, 919supported, 932Unicode, 202

database servers, 713datagrams, 717, 727DB-API (Python Database Application

Programmer’s Interface), 924–945about, 922–923apilevel, 924–925changes in versions of, 931connect( ), 925–927connection objects, 927–928cursor objects, 929–930exceptions, 927function attributes, 925–927layers in, 923module attributes, 924–926paramstyle, 925, 926thread safety, 925type objects and constructors, 930–931

DBM-style modules, 349debugging, 10, 84, 181, 437–439decimal floating point numbers, 147–148decode( ) built-in method, 200–201, 204decorators, 422–426, 460–463, 544deep copy, 240–241def keyword (statement), 48, 418, 421default arguments. See arguments (default)__del__( ) method, 528–530, 565del statement, 78–79, 122, 210, 258, 276delattr( ) built-in function, 560, 561delegation, 587–595__delete__( ) special method, 598, 599derivation, 517, 545–546descriptors, 598–610

destructor (class), 528–530developer tools, 84–85diamond shape inheritance hierarchy,

556–558__dict__ attribute

built-in types, 537class attributes, 522–525instance attributes, 534–536modifying, 535–536__slots__ attribute vs., 597vars( ), 563

dict( ) factory function, 263–265dictionaries, 40–41

accessing values in, 255–257assigning, 255built-in functions, 264, 265, 267built-in methods, 254, 255, 265–268, 1034, 1035cmp( ), 260–262comparing, 260–262copy( ), 267creating, 255, 263dict( ), 263–265exact match of, 262fromkeys( ), 266, 268functions for, 263–265hash( ), 264, 265and hash tables, 254items( ), 265, 266, 1055iteration, 311–312key-lookup operator ( [ ] ), 259keys, 237–238, 265–269, 1055keyword variable arguments, 434–436len( ), 264, 265login/password programming example,

269–272as mapping type, 253–258membership operator, 259operators for, 258–259as Python feature, 7related functions, 263–268removing elements of/dictionaries, 258setdefault( ), 266–268sorted( ), 267str( ), 260type( ), 260updating, 257–258values( ), 265, 266, 1055

difference ( - ) operator, 278

Chun.book Page 1076 Thursday, April 2, 2009 9:18 AM

Page 30: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Index 1077

difference update ( -= ) operator, 279dir( ) built-in function

built-in types, 536class attributes, 522, 523classes, 54, 561instance attributes, 534instances, 561lists, 220local variables, 562modules, 561objects, 561–562

direct access model type, 115–116directory structure, 493–494discard( ) method, 281, 282, 284disk files, 315–317dispatch, static vs. dynamic, 992distutils package, 974division

classic, 1053–1054, 1062–1063floor, 1053–1054, 1063true, 1053–1054, 1062–1063

division operators, 35, 130–133, 569divmod( ) built-in function, 140, 143__doc__ attribute, 524, 525, 629, 630, 632, 633document “doc” strings, 34–35documentation

classes, 617extended import, 486extensions to Python, 982file-access related modules, 353FTP, 756generator expressions, 319, 471GUI programming, 851list comprehensions, 314method resolution order, 558module, 71, 72NNTP, 765–766OOP, 617Python, 22–23set types, 284SMTP, 771style guidelines for, 70

dollar sign symbol ($), 678, 679DOS window, 14–16, 22dot symbol ( . ), 678, 689–690double precision floating point numbers, 125double quotation mark ( " ), 207double type, 117

double underscore ( __ ) attributes, 586downloading

IMAP, 772POP3, 773–777protocols for, 772–777Python, 11, 13, 18–19SMTP, 773–777Yahoo! stock quote data, 986

dropping (dropped) databases, 920, 921dumbdbm module, 349dummy functions, 974dump( ) function, 350dynamic dispatch, 992dynamic typing, 76

EEasyGUI, 849electronic mail. See e-mailelif (aka else-if) statement, 294–295, 308ellipsis objects, 95else statement

exceptions, 378try-except statement, 378

else statement, 292–294, See also if statement“dangling else”, 293–294for loops, 307–308usage of, loops/conditionals, 308

e-mail (electronic mail), 766–777components/protocols, 766–767IMAP, 772POP3, 772–777receiving, 771–772related modules, 778sending, 767–768SMTP, 768–771, 775–777

embedding, extensions vs., 982encapsulation, 516, 585–586encode( ) built-in methods, 200–201, 204encoding, 205, 497__enter__( ) method, 385Entry widget (Tk), 825enumerate( ) built-in function

iterators, 310lists, 218for loops, 45, 300–301, 303, 304sequences, 167strings, 185–186

equal sign ( = ), 64–65

Chun.book Page 1077 Thursday, April 2, 2009 9:18 AM

Page 31: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1078 Index

equality for sets, 276eric3, 850errors/error processing, 47–48

about, 359–360“cleaner” approach, 663DB-API, 927definition of, 360hiding, 372runtime, 361“safe and sane,” 10standard error, 337try-except statement, 372using CGI for, 886–892

eval( ) built-in function, 636, 638–639events, 823Excel, 991–993, 1000–1002except statement, 368–370Exception (root class), 370, 371, 373, 391, 394, 489exception condition, 360exceptions/exception handling. See also specific

headings, e.g., TryError exceptionabout, 10, 360–361arguments for, 372–375assert statement, 389–390built-in, 391–393, 1040–1042catching all, 370–372context management for, 382–385creating, 393–401DB-API, 927detecting/handling, 47–48, 364–382, 401–403else clause, 378examples of, 375–378, 395–401except statement with multiple, 369–370finally clause, 378–379handling, 1052–1053, 1068–1069and os.path.exists( ), 81–82, 84in Python, 361–364raise statement, 48, 386–389raising, 1053, 1069related modules, 404robustness of, 10skipping code, 365standard, 391–394with statement, 382–384strings, 386sys module, 403–404try statement with multiple, 368–369try-except statement, 364–365, 372, 378

try-except-else-finally statement, 381–382try-finally statement, 379–381unhandled, 365Unicode, 204upward propagation, 365warnings, 489Web servers, 401–402and wrapping a built-in function, 365–368,

375–378exec( ) built-in function, 640exec statement, 636, 640–641execfile( ) built-in function, 651–652executable objects

built-in functions, 636–642callable( ), 637compile( ), 637–638conditional code execution, 647–649eval( ), 638–639exec( ), 640generating/executing code at runtime, 642–649input( ), 641–642

executing/executioncallable objects, 628–635code at runtime, 642–649code conditionally, 647–649code objects, 635–636current process/user related functions,

666–667file, 348, 662imported modules, 73–74, 486, 487, 650–651non-Python programs, 653–654operating system interface functions, 666–667other Python programs, 649–653related modules, 668restricted, 663TCP server/clients, 725–726, 736, 740terminating, 663–666UPD server/clients, 729–730

__exit__( ) method, 385exiting, 791exponentiatial notation output, 180exponentiation operator ( ** ), 35, 133, 140extend( ) method, 214, 222extended import statement (as), 485–486extended slicing, 162–164extensions to Python, 963–982

about, 8, 963–964

Chun.book Page 1078 Thursday, April 2, 2009 9:18 AM

Page 32: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Index 1079

compilation of, 964, 974–975creating application code, 966–968documentation, 982embedding vs., 982Global Interpreter Lock, 980importing, 976main steps of, 965multithreaded, 980Psyco, 981–982Pyrex, 981reasons for creating, 964–965reference counting, 977, 979–980SWIG, 981testing of, 976–979Win32, 989, 990wrapping code, 968–975writing, 965–980

Ffactorial functions, 807–810factory functions, 136–145

built-in functions conversion to, 595–596conversion, 137–138definition of, 111dict( ), 263–265list( ), 218–219numeric types, 1023–1025sequence type operators, 1025–1028set types, 280, 283–284, 1036–1037standard type, 1022–1023str( ), 186–187super( ), 562–563tuple( ), 218–219type, 111type( ) as, 102

fetching rows, 920Fibonacci functions, 807–810FIFO (first-in-first-out) data structure, 227file( ) built-in function, 47, 326, 328file extensions, 11file objects, 325–326

access modes for, 327built-in attributes, 336–337, 1038–1039built-in functions, 326–329built-in methods, 329–336close( ), 332file( ), 47, 326, 328fileinput( ), 351, 352

fileno( ), 332flush( ), 332fnmatch( ), 351, 352glob( ), 351, 352input, 329–336intra-file motion, 331isatty( ), 332iterating through, 312, 315, 331–332line separators, 330, 333methods, 335–336, 1038–1039modules, 351–353multi-platform development, 333open( ), 46–47, 326–328output, 330read( ), 329readline( ), 329readlines( ), 329related modules, 351–353seek( ), 331standard, 337–338truncate( ), 332Universal NEWLINE Support, 328–329using to store attributes, 604wrapping a, 594–595write( ), 330writelines( ), 330

file system, accessing, 339–347File Transfer Protocol. See FTPfileinput( ) module, 351, 352“file-like” objects, 325–326, 353files. See also persistent storage

command-line arguments, 338–339example code for reading, 332–335execution of, 348as storage mechanism, 919text file manipulation example programs, 79–85transferring, 748–755, 759–766uploading, 894using to store attributes, 604

filter (warnings), 489–490filter( ) built-in function, 313, 314, 441–445,

1055finally clause, 378–379findall( ) function, 684, 694first-in-first-out (FIFO) data structure, 227flattening, 348float( ) built-in function, 366–368, 374–375float( ) factory function, 137, 138

Chun.book Page 1079 Thursday, April 2, 2009 9:18 AM

Page 33: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1080 Index

float type, 117floating point numbers, 37, 38, 125, 147–148, 180floor( ) built-in function, 141, 142floor division ( / / ), 35, 130–132, 569flush( ) built-in method, 332, 335fnmatch( ) module, 351, 352folding, 448for statement (loops), 43–45, 298–304break statement, 304–305, 308continue statement, 305–306else statement for, 307–308file iteration, 331–332with iterator types, 301and pass statement, 306–307range( ), 301–303with sequence types, 299–301sequence-related built-in functions for, 303–304syntax for, 298–299xrange( ), 303

forking processes, 789form Web page, 879–881, 883–886formal arguments (functions), 428–432format operator, 178–181, 205, 208, 1029forms, 894forward references, 418–419FP. See functional programmingframe objects, 94Frame widget (Tk), 825free variables, 457, 458“from module import ∗”, 487, 586from-import statement, 485, 487, 489, 494–496fromkeys( ) method, 266, 268frozenset( ) factory function, 280, 283FTP (File Transfer Protocol), 748–756, 858function pointers, 426functional nesting, 456functional programming (FP), 439–453

anonymous functions/lambda, 439–441apply( ), 441, 442built-in functions, 441–449constructs, 24debugging/performance measurement example,

437–439examples, 437–439, 450–453filter( ), 441–445map( ), 441, 442, 445–447partial function application, 450–453reduce( ), 441, 442, 447–449

functions, 48–49, 409–439. See also functional programming; methods; scopeabout, 409–410accessing pathname, 342–347arguments, 48, 49, 412–414, 423–424, 428–442attributes, 420–421callable objects, 628–629calling, 49, 412–417, 436–439classes vs., 518–519closures, 456–463creating, 418–426declaring, 48, 72, 73, 418–419declaring vs. definition, 418decorators, 422–426def statement, 418default arguments, 49, 413, 429–442descriptors, 606directory access, 340–341examples, 415–417, 425–427, 431–432, 456–463file access, 340–341formal arguments, 428–432forward references, 418–419global vs. local variables, 453–455grabbing Web pages example, 431–432grouped arguments, 413–414inner/nested, 421–422integer-only, 143–145keyword arguments, 412–413lambda, 463–465logging calls to, with closures, 461–463numeric type, 137–143, 1023–1025operator, 412passing, 426–427positional arguments, 428–429procedures vs., 410for re module, 684–685return values, 410–412standard type, 136–137, 1022–1023variable argument objects, 436–439variable-length arguments, 433–439

functions (built-in) (BIFs)attributes, 629callable, 628–629, 636–642classes, 558–564conversion to factory functions, 595–596executable objects, 636–642file objects, 326–329functional programming, 441–449

Chun.book Page 1080 Thursday, April 2, 2009 9:18 AM

Page 34: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Index 1081

instances, 558–564integer-only, 143–145lists, 165, 166, 215–219, 242–245mapping types, 260–262module, 491–493new-style classes, 595–596numbers, 136–145numeric types, 137–143, 150–151, 1023–1025objects, 558–564operational, 139–143, 166–168sequence types, 165–168, 185–186, 216–219,

242–245, 1025–1028sequence-related, 303–304set types, 280, 283–284, 1036–1037standard types, 101–110, 136–137, 215–216,

1022–1023string types, 184–187, 242–245tuples, 165, 166, 232–234, 242–245wrapping, for exceptions, 365–368

functions (user-defined) (UDFs), 629–630__future__ directives, 489FXPy, 850

GGadfly database, 923, 939–945garbage collection, 79generalization, definition of, 517generator expressions, 315–319

cross-product example, 317disk file example, 316–317refactoring example, 318–319

generators, 467–471enhanced, 470–471simple, 468–470yield statement, 468

geometry managers, 823__get__( ) method, 598–600, 602GET method, 880, 882, 895__getattr__( ) method, 588, 589, 597–598, 600getattr( ) built-in function, 560, 561, 588–590__getattribute__( ) special method,

597–600getopt module, 339GIL. See Gobal Interpreter LockGlade, 850glob( ) module, 351, 352Global Interpreter Lock (GIL), 790–791, 980global statement, 455

global variables, 453–455globals( ) built-in function, 492GNOME-Python, 850Gopher, 858grandchild class, 554, 555graphical user interface programming. See GUI

(graphical user interface) programminggreater than symbols ( > , >= ), 277Greenlets, 27Grid ( geometry manager), 823group( ) method, 685, 686grouped arguments (functions), 413–414grouping, 682–683, 692–693groups( ) method, 686GTK+, 840, 846–848, 850GUI (graphical user interface) programming,

819–851about, 822–824class example, 452–453documentation, 851file system traversal example, 834–840FTP client program, 755GTK+/PyGTK, 840, 846–848other GUIs for, 840–849partial function application example, 831–834Python MegaWidgets, 840, 843related modules, 848–849Swing, 1003–1006Tcl/Tk/Tkinter, 819–820Tix, 840, 842–843Windows clients as, 713wxWidgets/wxPython, 840, 843–846

gzip module, 351, 352

Hhardware, 712hasattr( ) built-in function, 560, 561__hash__( ) method, 269hash( ) built-in function, 264, 265hash tables (dictionaries), 254hashable objects, 237, 253, 264, 269–272Haskell language, 24has_key( ) method, 256header file, including, 968heavyweight processes, 789“Hello World!” program, 32–34, 483, 826–831,

1003–1006help( ) built-in function, 34, 54

Chun.book Page 1081 Thursday, April 2, 2009 9:18 AM

Page 35: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1082 Index

hex( ) built-in function, 143–145, 1065hexadecimal output, 180hierarchy, definition of, 517host-port pairs, 716hotshot module, 85HTML (Hyper-Text Markup Language), 875–877HTTP (HyperText Transfer Protocol), 856,

867–869, 882HTTP_COOKIE environment variable, 896Hyper-Text Markup Language. See HTMLHyperText Transfer Protocol. See HTTP

Iid( ) built-in function, 113, 114, 117identifiers, 67–68

names, 70objects, 90scope, 453, 454special with underscores, 69style guidelines for names, 70

identity comparison of objects, 96–100, 108IDEs (Integrated Development Environments),

17–22, 338IDLE (Integrated DeveLopment Environment)

(Unix IDE), 18, 20–21if statement, 41–42, 291–292, See also elif

statement; else statementIMAP (Internet Mail Access Protocol), 772immutability, 65–66, 113, 194–196, 234–235, 273,

552implementation

of an abstraction, 516hiding, 585

__import__( ) built-in function, 491import statement, 484

absolute import, 494–495at end of modules, 499extended import (as), 485–486from-import statement, 485, 487, 489,

494–496loading of, 650–651

importing attributes, 496importing modules, 52, 484–486

attributes for, 496built-in functions, 491–493case-insensitive, 496–497definition of, 477execution on, 73–74, 650–651extension module, 976

features of, 486–491__future__, 489globals( ), 492__import__( ), 491import cycles, 497–500loading vs., 487locals( ), 492multi-line import, 485names imported into current namespace, 487names imported into importer’s scope,

488–489new import hooks, 490–491packages, 493–495path search/search path, 478–480related modules, 500–501relative import, 495reload( ), 492–493search path/path search, 478–480style guidelines for, 72Tkinter module, 821–822from zip files, 490

indentationfor code blocks, 41“dangling else” statements, 293style guidelines for, 70of suites, 63–64

IndexError exception, 363indexing slices, 164–165inequality sets, 276infinite loops, 297–298inheritance, 547–558

__bases__ class attribute, 548definition of, 517diamond shape hierarchy, 556–558multiple, 553–558overriding methods via, 549–551subclassing with, 512

__init__( ) methodas constructor, 51, 510–511, 527–529customizing classes with, 565, 634instantiation, 510, 527–530, 533–534overriding, 549–551return value, 533–534setting instance attributes in, 531–533and tracking instances, 530

initModule( ) module initializer function, 973–974

inner/nested functions, 421–422, 456–458“in-place” operations, 569, 574–575

Chun.book Page 1082 Thursday, April 2, 2009 9:18 AM

Page 36: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Index 1083

inputfile built-in methods, 329–336raw_input( ), 32–33, 54, 186, 641–642standard, 337user, 875–877, 886–892

input( ) built-in function, 636, 641–642inserting rows, 920–922installing Python, 11–13instance attributes, 531–540

accessing, 511class attributes vs., 537–540definition of, 506–507determining, 534instantiation of, 531–534“on-the-fly,” 531setting, 531, 532special, 534–536

instances, 526–530about, 504–507binding methods to, 522built-in functions, 558–564callable objects, 51, 634–635creating, 51–52, 511, 526–527default values for, 532–533definition of, 50__del__( ), 528–530invoking methods via, 511–512keeping track of, 530

instantiationcreating instances, 511, 526–527default arguments with, 532–533definition of, 506__init__( ), 510, 527–530, 533–534instance attributes, 531–534__new__( ), 528Thread class, 802–807

int( ) built-in function, 54, 142int( ) factory function, 137, 138, 141int type (unsupported), 117integer-only functions, 143–145integers, 122–125

bit operators, 135–136Boolean, 123format operator output, 180–181long, 37, 38, 123–125, 1053, 1061standard, 37, 123unification of long integers/integers, 38, 123–

125, 1053, 1061unsupported types, 117

Integrated DeveLopment Environment. See IDLEIntegrated Development Environments. See IDEsintegration of code (Python/non-Python), 963interfaces to data attributes, 516internal types, 93–95Internet, architecture of, 856–859Internet client programming, 747–779.

See also Web programmingabout, 747–748electronic mail, 766–778FTP, 748–756, 858newsgroups, 756NNTP, 756–766, 858related modules, 778–779SMTP, 767–772, 775–777, 858transferring files, 748–756Usenet, 756Web programming vs., 858–859

Internet Mail Access Protocol (IMAP), 772Internet protocols, 858, 912. See also specific

headings, e.g., NNTP (Network News Transfer Protocol)

Internet Service Provider. See ISPinterning, 100interpreted languages, 6, 11, 23, 965interprocess communications (IPC), 715intersection ( & ) operator, 277intersection update ( &= ) operator, 279Intranet, 857introspection, 518IOError exception, 326, 363, 365, 373, 392, 394,

399IPC (interprocess communications), 715IronPython, 12, 27, 989is keyword, 99isatty( ) built-in method, 332, 335isinstance( ) built-in function, 106–109,

559–560, 596–597ISP (Internet Service Provider), 857, 858issubclass( ) built-in function, 558–559items( ) built-in methods (dictionaries), 265, 266__iter__( ) built-in method, 576–579iter( ) function, 313iterating/iteration

file, 331–332mechanics of, 309–310by sequence item/index, 299–301through a matrix, 314–315through files, 315

Chun.book Page 1083 Thursday, April 2, 2009 9:18 AM

Page 37: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1084 Index

iterators, 309–313about, 309–310any number of terms example, 577–579creating, 313dictionaries, 311–312files, 312for loops, 301, 309–313, 331–332mutable objects, 312related modules, 319–320sequences, 309–311

itertools module, 319–320

JJava, 8, 12, 23–26, 969, 1002–1003JavaScript, 23, 24JIT (just-in-time) compiler, 982join( ) method, 159, 176, 189, 191just-in-time (JIT) compiler, 982Jython, 12, 24, 26, 1002–1006

KKanter, Brian, 756KDE desktop environment, 850KeyboardInterrupt exception, 371, 391, 394KeyError, 257, 363key-lookup operator ( [ ] ), 259keys (dictionary)

collisions, 268–269comparing, 261–262as hashable object, 253, 264, 269–272mapping type, 253, 254restrictions on, 268–269

keys( ) built-in method, 254, 255, 265, 267key-value pairs, 40, 254, 257keyword arguments (functions), 412–413keyword variable arguments (dictionary), 434–436keywords

and, or, not, 36, 99–101class, 50, 504def, 48, 418, 421identifiers, 67is, 99partial function application, 451–452tables of, 68, 1022

LLabel widget (Tk), 825–831lambda

anonymous functions, 439–441callable objects, 630–631inner functions, 422and Lisp, 24list comprehensions, 313–314map( ), 446–447reduce( ), 448, 449return value, 439scope, 463–465

LAN (Local Area Network), 857, 858languages

comparisons of, 23–26compiles, 965high-level, 6–7interpreted, 6, 11, 23, 965

Lapsley, Phil, 756last-in-first-out (LIFO) data structure, 223len( ) built-in function, 54

dictionaries, 264, 265lists, 216–217and range( ), 45sequences, 166, 167, 185set types, 280, 283strings, 185

less than symbols ( < , <= ), 277lexical variables, 458–463LIFO (last-in-first-out) data structure, 223lightweight processes, 789. See also threadsline separators (terminators), 330, 333linking wrapper extensions, 975Linux, 12, 14, 16–17Lisp, 24, 25list( ) built-in function, 165, 166list comprehensions, 24, 45, 215, 313–316.

See also generator expressionslist( ) factory function, 218–219Listbox widget (Tk), 825lists, 39–40, 208–210

accessing values in, 209assigning, 209as building blocks, 7building data structures with, 223–230built-in functions, 165, 166, 215–219,

242–245built-in methods, 219–223, 242–245, 1034concatenation of, 213–214creating, 209, 223–230membership operators, 213

Chun.book Page 1084 Thursday, April 2, 2009 9:18 AM

Page 38: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Index 1085

operators for, 211–215, 242–245other data structures, creating with, 223–230queues using, 227–230removing elements/lists, 210–211repetition in, 214sequence type functions, 216–219, 242–245sequence type operators, 211–215, 242–245slices of, 211–213special features of, 223–230stacks using, 223–227standard type functions, 215–216standard type operators, 211tables of, 242–245tuples vs., 237–238updating, 210

load( ) function, 350loading modules, 486, 487, 496Local Area Network. See LANlocal variables, 81, 453–455, 562localhost, 720locals( ) built-in function, 492lock objects, 795logging function calls (with closures), 461–463logging module, 84logical errors, 360login/password programming example, 269–272long( ) factory function, 137, 138long integers, 37, 38, 123–125long type (unsupported), 117loops, 296–308. See also iterators

auxiliary statements, 308break statement, 304–305, 308continue statement, 305–306counting, 297else statement, 307–308infinite, 297–298and iterators, 301, 309–313, 331–332and pass statement, 306–307performance enhancement for, 175range( ), 44–45for statement, 43–45, 298–308, 331–332while statement, 42, 296–298, 304–308

MMacOS X, 12, 14–19, 333macros (for reference counting), 980mail user agent (MUA), 771–772main( ), 73

maintainability, 9–10makefiles, 13mangled names. See name-manglingmap( ) built-in function, 313, 314, 441, 442,

445–447, 1055mapping types

access model category, 115–116built-in functions/factory functions, 260–265built-in methods, 265–268dictionaries, 253–258keys for, 253operators for, 258–259related functions, 263–265special methods for customizing classes, 568

marshal module, 348–350“Mash-ups,” 985match objects, 686matching. See also regular expressions

any single character ( . ), 678, 689–690beginning/end of strings, 678–679closure operators, 680–681“greedy” operators, 703–705grouping, 682–683, 690–693more than one pattern, with alternation ( | ), 678more than one string, 689multiple occurrence/repetition using closure

operators, 680–681negation ( ^ ), 680parentheses ( ( ) ), 682–683ranges ( - ), denoting, 680re module, 206–207regular expressions, 680–681repetition, 680–681, 690–693searching vs., 675, 688, 703–705special characters/symbols, 676–677, 682,

690–693strings, 678–679, 687–689, 693, 701–703word boundaries, 693

match( ) re module function, 684, 687–688mathematical operators, 35–36, 130–135matrix iteration, 314–315max( ) built-in function, 166, 167, 185, 217membership ( in, not in ) operators, 158–159

dictionaries, 259lists, 213for loops, 299sequences, 158–159, 172–175set types, 273, 276

Chun.book Page 1085 Thursday, April 2, 2009 9:18 AM

Page 39: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1086 Index

membership ( in, not in ) operators (continued)strings, 172–175tuples, 233

memory management, 75–79dynamic typing, 76garbage collection, 79interpreter performing, 11memory allocation, 76reference counting, 76–79, 99, 977, 979–980variables declarations, 75

Menu widget (Tk), 825message transport agent (MTA), 767, 768, 772message transport system (MTS), 767Message widget (Tk), 825metacharacters, 676–677__metaclass__, 610–615metaclasses, 610–615method descriptors, 599method resolution order (MRO), 554–558, 562methods, 521–522

about, 507–512arguments, 532–533binding, 522, 540–541callable objects, 631–633class, 543connection objects, 928decorators, 422–426file object, 335–336, 1038–1039group(s), 686invoking, 511–512, 540–541naming, 513for new-style classes, 597–600overriding, via inheritance, 549–551privacy, 585–586for re module, 684–685static, 542–544strings, 188–191

methods (built-in)attributes, 632dictionaries, 254, 255, 265–268, 1034, 1035files, 329–336lists, 219–223, 242–245, 1034mapping types, 265–268sequences, 242–245, 1025–1028set types, 281–284, 1036–1037strings, 188–191, 242–245, 1030–1033tuples, 242–245

methods (special) (for customizing classes), 564–585any number of terms iterator example, 577–579iterators, 576–579multi-type example, 579–585numeric customization (Time60) example,

572–576Random Sequence iterator example, 576–577RoundFloat2 simple example, 569–572special, 564–585, 1043–1046tables of, 565–568, 1043–1046

methods (special) (new-style classes), 597–600methods (user-defined) (UDMs), 632–633Microsoft Office

Excel, 991–993, 1000–1002Outlook, 996–1000PowerPoint, 994–997programming, with Win32 COM, 989–1002Word, 993–994

MIME (Multipurpose Internet Mail Extension) headers, 863, 877

min( ) built-in function, 166, 167, 185, 217mixed mode operations, 127–129, 204, 278mix-in classes, 556modeling (OOD), 515__module__ attribute, 524, 525, 629, 633modulefinder, 500ModuleMethods [ ] array, 973modules, 52–53. See also importing modules

about, 64, 477accessing module variables, 52–53auto-loaded, 496built-in functions, 491–493__builtins__ vs. __builtin__, 480–481calling, 52–53case-insensitive import, 496–497cProfile, 85debugging, 84developer tools, 84–85“executed” when loaded, 486executing as scripts, 652–653executing on import, 650–651extended import (as), 485–486and files, 478–480__future__, 489hotshot, 85import cycles, 497–500

Chun.book Page 1086 Thursday, April 2, 2009 9:18 AM

Page 40: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Index 1087

importing vs. loading, 487logging, 84multi-line import, 485names imported into importer’s scope, 488–489namespaces, 478, 480–483new import hooks, 490–491numeric types, 148–149packages, 493–495pdb, 84persistent storage, 348–350preventing attribute import, 496profile, 85search path/path search, 478–480separating, 64sequence types, 238–239source code encoding, 497standard library, 148–149, 205–207strings, 205–207structure/layout for, 71–74subprocess, 660–662warning framework, 489–490Web server, 909

modulus operator, 132–133MRO. See method resolution orderMTA. See message transport agentMTS (message transport system), 767MUA. See mail user agentmulti-line import, 485multipart form submission, 894multi-platform development, 12, 333, 964multiple assignment, 66multiple inheritance, 553–558multiplication ( * ) operator, 35Multipurpose Internet Mail Extension headers. See

MIME (Multipurpose Internet Mail Extension) headers

multithreaded programming (MT), 787–814about, 787–789accessing threads, 792examples, 792–793exiting threads, 791extensions to Python, 980global interpreter lock, 790–791, 980processes, 789related modules, 813–814thread module, 794–799, 814threading module, 793, 794, 800–814threads, 721, 789–813, 925

multi-type customization example, 579–585“multuple” assignment, 66–67mutable hash tables, 253mutable objects, 65, 90, 113, 222, 312mutable sets, 273, 279, 281–282, 284mutable types, subclassing, 552–553mutex module, 814MySQL, 920, 934–936, 939–945

N__name__ attribute, 524, 629, 630, 632, 633name lookup, 482–483__name__ system variable, 73, 74NameError exception, 362, 454, 455, 522name-mangling, 516, 586namespace(s), 480–483

__builtins__, 480–481classes as containers for, 506“Hello World!” example, 483importing names into current, 487modules, 478, 480–483name lookup/scoping/overriding, 482–483overriding, 482–483scope, 454types of, 480–481useful features of, 483variable scope, 465–466, 481–482

negation symbol ( ^ ), 680.NET/Mono implementation, 27network location components, 860Network News Transfer Protocol. See NNTPnetwork programming, 711–742

client/server architecture, 711–715functions/modules for, 718–731related modules, 741–742sockets, 715–720, 730–731SocketServer module, 732–736TCP clients/servers, 720–726, 732–740twisted framework, 737–740UDP clients/servers, 726–730

__new__( ) method, 528, 565new import hooks, 490–491NEWLINE character(s)

continuation ( \ ), 62escaping, 192POSIX systems, 333print statement, 43suppression of, 333–334

Chun.book Page 1087 Thursday, April 2, 2009 9:18 AM

Page 41: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1088 Index

NEWLINE character(s) (continued)universal support for, 326, 328–329write( ), 53

newsgroups, 756new-style classes. See also classic classes

advanced features of, 595–615classic classes vs., 504descriptors, 598–610documentation, 617general features, 595–597__getattribute__( ), 597–600metaclasses, 610–615method resolution order, 554–558and OOP, 503privacy, 586__slots__ class attributes, 597super( ), 562–563

next( ) built-in method, 335, 468–471, 576–579NNTP (Network News Transfer Protocol), 756–757

client program, 760–765documentation, 765–766examples, 759–765interactive, 759–760nntplib.NNTP class methods, 758–759object methods, 759–760as original Internet protocol, 858Python and, 758–759

non-data descriptors, 599None type, 410–412, 533–534non-keyword variable-length arguments (tuple),

433–434non-Python programs, 653–654“not equals” comparison operators ( != , <> ), 36not keyword, 36, 99–101NotImplementedError exception, 520NUL characters, 192, 208Null object, 92–93numbers

assignment, 121bit operators, 135–136Boolean, 145–147built-in/factory functions, 136–145complex, 126–127creating, 121double precision, 125floating point, 37, 38, 125, 147–148, 180integers, 122–125introduction to, 121–122

mathematical operators, 35–36, 130–135mixed-mode operations, 127–129numeric type functions, 137–143, 150–151,

1023–1025numeric type operators, 127–136, 150–151,

1023–1025operators, 35–36, 127–136, 150–151,

1023–1025removing, 122standard type functions, 136–137standard type operators, 129–130types, 37–38updating, 122

numeric coercion, 128, 129numeric customization (Time60) customization

example, 572–576numeric type(s), 37–38

Boolean “numbers,” 145–147functions, 137–143, 150–151,

1023–1025operators, 127–136, 150–151, 1023–1025related modules for, 148–149special methods for customizing classes,

566–567, 569

Oobject(s), 89–95. See also specific types

assignment of, 65attributes, 90Boolean operators, 100–101Boolean values of, 93, 96–97built-in functions, 558–564built-in types, 91calling functions with variable arguments,

436–439characteristics of, 90classes/instances, 504–507code, 94, 635–636composite/compound, 112connection, 927–928copying, 239–241cursor, 929–930as default class, 504ellipsis, 95executable, 636–642“file-like,” 325–326, 353frame, 94hashable, 237, 253, 264, 269–272

Chun.book Page 1088 Thursday, April 2, 2009 9:18 AM

Page 42: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Index 1089

identity comparison of, 97–100, 108internal types, 93–95invoking, 526–527mutable, 65, 90, 113, 222, 312Null, 92–93removing single reference to, 78–79slice, 95standard type operators, 96–101standard types, 91, 93traceback, 94value comparison of, 93, 96–97, 108wrapping, 588–595XRange, 95

object-oriented design. See OODobject-oriented language, 7object-oriented programming. See OOPobject-relational managers. See ORMsobjects (callable), 628–635

class instances, 634–635classes, 634functions, 628–629lambda, 439methods, 631–633

oct( ) built-in function, 143–145, 1065–1066octal literals, 1054, 1061–1062OOD (object-oriented design), 514–516, 545OOP (object-oriented programming), 504–518,

520–617about, 504–514buzzwords, 516–518classes, 518–520languages, 585–586new-style classes, 503and real-world problems, 514–515relationship OOD and, 514–515

open( ) built-in function, 46–47, 54, 326–328operational built-in functions, 139–143, 166–168operations (database), 920operator(s)

assignment ( = ), 64–65asterisk ( * ), 680–681augmented assignment, 65–66bit, 135–136Boolean, 36, 100–101brace ( { } ), 681closure, 680–681dictionaries, 258–259difference ( - ), 278

difference update ( -= ), 279division, 35, 130–133, 569exponentiation ( ** ), 35, 133, 140format, 178–181, 205, 208, 1029function, 412“greedy,” 703–705intersection ( & ), 277intersection update ( &= ), 279key-lookup ( [ ] ), 259lists, 211–215, 242–245mapping types, 258–259mathematical operators, 35–36, 130–135membership, 158–159, 172–175, 213, 233, 259,

273, 276, 299and mixed mode operations, 127–129modulus, 132–133multiple assignment, 66“multuple” assignment, 66–67“not equals” comparison ( !=, <> ), 36numeric type, 127–136, 150–151, 1023–1025overloading addition, 573–575parentheses and, 36plus ( + ), 681question mark ( ? ), 681raw string, 168, 182–184repetition ( * ) sequence, 159–160retention update ( &= ), 279reverse quote (``), 103–104sequence type, 158–159, 170–178, 211–215,

232–233, 242–245, 1025–1028set type, 273, 274, 276–279, 282–284, 1036–1037slices ( [ ], [ : ], [ : : ] ), 39, 160–165,

170–172, 211–213, 233standard type, 129–130, 1022–1023string format, 178–181, 205, 208, 1029, 1030strings, 168, 170–184, 208, 242–245symmetric difference ( ^ ), 278symmetric difference update ( ^= ), 279table of, 1046–1048ternary, 295–296tuples, 232–234, 242–245Unicode string (u /U), 184union ( | ), 277, 279update ( |= ), 279

operator module, 616–617optparse module, 339or keyword, 36, 100, 101ord( ) built-in function, 144, 145, 187, 204

Chun.book Page 1089 Thursday, April 2, 2009 9:18 AM

Page 43: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1090 Index

ORMs (object-relational managers)employee role database example, 947–957related modules, 959and SQL, 946SQLAlchemy, 946–950, 953–955SQLObject, 946, 951–953, 955–957as storage mechanism, 919

os moduleadditional functionality of, 347attributes, 667examples, 343–347external program execution functions, 654–662file/directory access functions, 339–341os. exec*( ), 654–655, 659os. wait*( ), 655, 659–660os._exit( ), 666os.fork( ), 654, 658–659os.kill( ), 666os.popen( ), 655, 657, 661–662os.spawn*( ), 655, 660os.system( ), 654, 656–657, 661

OSError exception, 365os.path module, 81–82, 84, 342–347, 352Outlook, 996–1000output (program), 32–33output (standard), 337output built-in methods, 330overriding (overloading)

built-in names, 69global variable, 454methods via inheritance, 549–551and mixed-mode operation, 128namespaces, 482–483

Ppackages, 493–495packer (geometry manager), 823Pango, 846paramstyle, 925, 926parentheses ( ( ) ), 36, 673–682parent/parent class, 512, 545–548, 554, 555, 586,

822, 823partial function application (PFA), 450–453,

831–834pass statement, 306–308, 504passing functions, 426–427path search, 478–480pathname, 14, 16, 342–347

pattern matching. See regular expressions (REs)pdb debugging module, 84PEPs (Python Enhancement Proposals), 53, 71, 617performance enhancement, 8, 175, 965performance measurement example, 437–439period symbol ( . ), 678, 689–690Perl, 23, 25, 26persistent storage, 348–350, 919, 920PFA. See partial function applicationPHP, 25pickle module, 202, 348–350pipe symbol ( | ), 678, 689pkgutil, 500plain integers, 123plus operator ( + ), 681PMW. See Python MegaWidgetsPmw (Python MegaWidgets), 849pointer type, 117polymorphism, 517–518POP (Post Office Protocols), 772pop( ) method, 281, 282, 284POP3, 772–777port numbers, 716portability, 8–9positional arguments (functions), 428–429POSIX systems, 333, 792Post Office Protocols. See POP; POP3Postel, Jonathan, 768PostgreSQL, 936–937pound sign ( # ) (hash symbol), 34, 62pow( ) built-in function, 140, 143PowerPoint, 994–997precedence, 35, 600, 602precision, 117print statement, 32–33print() function, 33, 1050–1051, 1064–1065printf( )-like functionality, 208privacy, 585–586, 965procedures, functions vs., 410processes, definition of, 789producer-consumer problem, 810–813profile module, 85profiling of code, 8, 84–85, 965programmers, 402–403programs

executing other non-Python, 653–654executing other Python, 649–653

prompts (primary/secondary), 31

Chun.book Page 1090 Thursday, April 2, 2009 9:18 AM

Page 44: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Index 1091

property( ) built-in function, 606–610proprietary source code, 965protocols (Internet), 858Psyco, 981–982“public” attributes, 585pure virtual functions, 519.py file extension, 11, 478PyArg_Parse*( ) function, 969PyArg_ParseTuple( ) function, 971Py_BuildValue( ) function, 969, 971, 972.pyc files, 11, 965pyFLTK, 850PyGTK, 840, 846–848, 850PyGUI, 850Py_InitModule( ), 973–974.pyo file extension, 11PyObject, 969–973PyOpenGL, 850PyQt, 850PyQtGPL, 850Pyrex, 981Python Enhancement Proposals. See PEPsPython FAQ, 617Python Library and Language Reference manual, 617Python MegaWidgets (PMW), 840, 843Python version 2.0, 313Python version 2.2, 526–527, 617Python version 2.4, 316Python Virtual Machine, 790–791PythonCard, 849PYTHONCASEOK environment variable, 497PYTHONPATH environment variable, 479, 500PythonWin IDE, 19–21

QQt GUI, 850querying (databases), 920question mark operator ( ? ), 681Queue module, 793, 810–814queue, using lists to build, 227–230quotation marks, 207quote*( ) functions, 865–866

Rrace condition, 790Radiobutton widget (Tk), 825raise statement, 48, 386–389raising an exception, 360, 361

random module, 149Random Sequence iterator example, 576–577range( ) built-in function, 44–45, 54, 219, 300,

301–303, 1055range symbol ( - ), 680ranges ( - ), 680, 682rapid prototyping, 10raw strings, 168, 182–184, 208, 698raw_input( ) built-in function, 33–34, 54, 186,

641–642RDBMS (relational database systems), 919, 920,

931–932re module, 206–207, 684–698read( ) built-in method, 329, 335readinto( ) method, 330, 335readline( ) built-in method, 312, 329, 335readlines( ) built-in method, 329, 336realm, 867reason (exceptions), 373rebinding, 480receiving e-mail, 771–772recursion, 466–467redirecting output, 33reduce( ) built-in function, 441, 442, 447–449,

1065refactoring, 318–319refcount, 76reference, 65, 79, 239reference counting, 76–79, 99, 977, 979–980reflection, definition of, 518regular expression engine, 23, 204regular expressions (REs)

about, 673–676any single character ( . ), 678, 689–690ASCII characters, 698beginning/end of strings, 678–679character classes ( [ ] ), creating, 679–680, 690compiling, 685–686example, 698–705finding every occurrence, 694and “greedy” operators, 703–705grouping, 682–683, 690–693match( ), 684, 687–688match objects/group(s) methods, 686matching more than one pattern, with alterna-

tion ( | ), 678matching more than one string, 689matching strings, 701–703

Chun.book Page 1091 Thursday, April 2, 2009 9:18 AM

Page 45: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1092 Index

regular expressions (continued)matching word boundaries, 693multiple occurrence/repetition using closure

operators, 680–681negation ( ^ ), 680parentheses ( ( ) ), 682–683ranges ( - ), denoting, 680raw strings, 183, 698re module, 206–207, 684–698repetition, 680–681, 690–693searching vs. matching, 675, 688, 703–705searching/replacing, 694–695search( ) re module, 688special characters/symbols, 676–677, 682,

690–693splitting on delimiting pattern with, 695–698strings, 701–703sub( )/ subn( ), 694–695word boundaries, 693

regular integers, 123relational database systems. See RDBMSrelative complement ( - ) operator. See difference

( - ) operatorrelative import (packages), 495reload( ) built-in function, 492–493remove( ) method, 281, 282, 284removing

dictionary elements/dictionaries, 258lists/list elements, 210–211numbers, 122set members/sets, 276single object reference, 78–79strings/characters, 169–170tuple elements/tuples, 232

repetitionlists, 214regular expressions, 680–681, 690–693special characters/grouping and, 690–693strings, 177–178tuples, 233

repetition ( * ) sequence operator, 159–160repr( ) built-in function, 102–104REs. See regular expressionsrestricted execution, 663retention update ( &= ) operator, 279return value(s), 222, 410–412, 439, 533–534reverse quote operator ( ' ' ), 103–104

reversed( ) built-in function, 166, 167, 217, 222, 303, 304, 310

Rexx, 26robustness, 10root window, 822round( ) built-in function, 140–143RoundFloat2 customization example, 569–572rows, 920–922Ruby, 24, 25running Python, 13–22

in an IDE, 17–21code examples, 22interactive interpreter from command line, 14–16as a script from command line, 16–17

runtime errors, 361runtime generation/execution of code, 642–649RuntimeError exception, 392

Sscability, 7–8scalar storage, 112Scale widget (Tk), 825, 829–831scope, 453–466

closures, 456–458global statement, 455global vs. local, 453–455lambda, 463–465name lookup, 482–483names imported into importer’s, 488–489namespaces, 465–466, 481–482number of, 456overriding, 482–483

scriptscommunicating with MS Office using, 990–1002generating/executing code with, 642–649as modules, 64running Python as, 16–17

Scrollbar widget (Tk), 825search path, 14–17, 478–480searching, 454, 675, 688, 703–705search( ) re module, 684, 688Secure Socket Layer (SSL), 866seek( ) built-in method, 331, 334, 335select( ) function, 741self argument, 51, 507, 510, 541, 550, 551__self__ attribute, 629, 632semicolon ( ; ) (multiple statements), 64

Chun.book Page 1092 Thursday, April 2, 2009 9:18 AM

Page 46: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Index 1093

sending e-mail, 767–768sequence(s), 157–167. See also lists; strings; tuples

access model type, 115–116built-in functions, 165–168, 185–186, 216–219,

242–245, 1025–1026concatenation of, 159conversion/casting of, 165–166and iterators, 309–311keys for, 253, 254membership operators, 158–159, 172–175methods, 242–245operational built-in functions for, 166–168operators, 158–159, 170–178, 211–215, 232–233,

242–245, 1025–1028related modules for, 238–239repetition of, 159–160sequence-related built-in functions, 303–304slicing, 160–165special methods for customizing classes, 567–568standard type operators, 158for statement used with, 299–301stride indices, 162–165strings, 170–178table of, 242–245

serversbank tellers as example of, 713–714infinite loop for, 823–824TCP, 720–723, 725–726, 732–738, 740twisted reactor TCP, 740UDP, 726–730Web, 401–402, 713, 855–858, 875–879,

906–909, 911window system as, 821

server-side COM programming, 990__set__( ) special method, 598, 599set( ) factory function, 280set types, 273–276

accessing values in, 275assigning, 273, 274built-in functions, 280, 283–284, 1036–1037built-in methods, 281–284, 1036–1037creating, 273, 274difference ( - ) operator, 278difference update ( -= ) operator, 279equality/inequality, 276factory functions, 280, 283–284frozenset( ) factory function, 280intersection ( & ) operator, 277

intersection update ( &= ) operator, 279len( ) built-in function, 280membership operator, 273, 276mixed operations, 278operation/relation symbols, 274operators, 276–279, 283–284, 1036–1037related modules, 284removing set members/sets, 276retention update ( &= ) operator, 279set( ) factory function, 280subsets/supersets, 277symmetric difference ( ^ ) operator, 278symmetric difference update ( ^= ) operator, 279table of, 282–284types of sets, 273union ( | ) operator, 277, 279update ( |= ) operator, 279updating, 275using operators vs. built-in methods, 282

setattr( ) built-in function, 536, 560, 561setdefault( ) built-in method, 266–268shallow copy, 240shell scripting, 23shelve module, 349–350short type (unsupported), 117showname( ) method, 52shutil module, 351, 352Simple Mail Transfer Protocol. See SMTPSimpleHTTPServer module, 907, 908, 911Simplified Wrapper and Interface Generator

(SWIG), 981single character ( . ), 678, 689–690single element tuples, 236–237single quotation mark ( ' ), 207single underscore ( _ ) attributes, 586site module, 500sizes, comparing dictionary, 261sleeping (threads), 789, 792–793, 796–799,

803–807slice objects, 95slices ( [ ] ,[ : ], [ : : ] ) sequence opera-

tors (slicing), 160–165indexing, 164–165lists, 211–213stride indices, 162–165strings, 39, 170–172tuples, 39, 233

__slots__ class attributes, 597

Chun.book Page 1093 Thursday, April 2, 2009 9:18 AM

Page 47: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1094 Index

SMTP (Simple Mail Transfer Protocol)about, 768clients, 775–777documentation, 771e-mail, 767, 772example, 770–771, 775–777interactive, 770–771object methods for, 769as original Internet protocol, 858Python and, 768–769smtplib.SMTP class modules, 769

smtplib.SMTP class modules, 769socket(s), 715–718

about, 715–716addresses, 716built-in methods, 719–720connection-oriented vs. connectionless,

716–718creating, 721, 724

socket( ) module function, 718, 730–731, 741–742

SocketServer module, 721, 732–736, 741, 814SOCK_STREAM type, 717software (client/server architecture), 712–713Solaris, 12, 14, 16–17sorted( ) built-in function, 167, 217, 222, 267,

303, 304source code (encoding), 497spacing and block delimitation, 63spawning

processes, 789threads, 721

special symbols, 676–677specialization, 517split( ) method, 190, 191split( ) re module, 685, 695–698SQL (Structured Query Language), 920–922, 946SQLAlchemy, 946, 947–950, 953–955SQLite, 937–945SQLObject, 946, 951–953, 955–957SSL (Secure Socket Layer), 866stack, building using lists, 223–227“stack trace,” 10stackless Python implementation, 12, 27standard error, 337standard exceptions, 391–394standard files, 337–338standard input, 337

standard integers, 37, 123standard output, 337standard type functions

built-in, 101–110dictionaries, 260lists, 215–216mapping type, 260numeric, 136–137set types, 280strings, 184–185table of, 1022–1023

standard type operatorsBoolean, 100–101dictionaries, 259lists, 211mapping type, 259numeric, 129–130objects, 96–101sequence types, 158set types, 276–277strings, 170tables of, 110, 1022–1023tuples, 232–233

standard typesabout, 91by access model, 115–116categorizing, 111–116deriving, 551–553by storage model, 112–113unsupported types, 116–117by update model, 113–114wrapping, 592–593

StandardError exception, 373, 391star operator ( * ), 680–681“stateless” protocol, 856, 895statements (Python)

comments, 34–35, 62, 69–70continuation, 62grouping multiple, 62–63multiple, on single line, 64rules for, 61suites, 62–63

static data, 521static dispatch, 992static members, 521, 539static methods, 542–544staticmethod( ) built-in function, 543stderr, 337

Chun.book Page 1094 Thursday, April 2, 2009 9:18 AM

Page 48: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Index 1095

stdin, 337stdout, 337stock quote server, 986–989, 1001–1002StopIteration exception, 298, 301, 310storage (attributes), 604storage model, 112–113, 116str( ) built-in function, 54, 102–104, 136, 137,

165, 166, 260str( ) factory function, 186–187stride indices, 162–165string format operator, 178–181, 208, 1029, 1030string templates, 182StringIO module, 351, 352strings, 38–39, 168–170

accessing values of, 169assigning, 168–169built-in functions, 184–187, 242–245built-in methods, 188–191, 242–245, 1030–1033chr( ), 187concatenation, 39, 176–177creating, 168–169debugging, 181ending of, 208enumerate( ), 185–186exceptions, 386identifiers, 67–68immutability of, 194–196len( ), 185matching, 701–703matching beginning/end of, 678–679, 693matching more than one, 689matching within, 688max( ), 185membership, 172–175min( ), 185no char type for, 207non-NUL/ ‘0’ ending of, 192, 208NUL characters, 192, 208operators, 170–184, 242–245ord( ), 187quotation mark delimited, 207raw strings, 168, 182–184, 208, 698raw_input( ), 186regular expressions, 206–207, 678–679, 688, 689,

693, 701–703removing, 169–170repetition of, 39, 177–178sequence operators, 170–178, 242–245

slices of, 170–172special/control characters, 192–193, 208standard library modules for, 205–207standard type operators, 168str( ), 186–187string templates, 182string-only operators, 178–184summary of, 207–208tables of, 242–245triple quotes, 38, 193–194, 208unichr( ), 187Unicode, 184, 186–187, 196–206, 892–893, 1051,

1067updating, 169zip( ), 186

Structured Query Language. See SQL“stubs,” 974style guidelines, 69–75, 513sub( ) function/method, 685, 694–695subclasses/subclassing

creating, 512–513, 546, 805–807derivation, 545–546multiple inheritance, 553–558standard types, 551–553using, 513

subn( ) function/method, 694–695subprocess module, 660–662subsets, 277substrings, accessing, 169subtraction ( - ) operator, 35suites, 41–42, 62–64, 292sum( ) built-in function, 166, 167, 218summation functions, 807–810super( ) built-in function, 562–563supersets, 277SWIG (Simplified Wrapper and Interface

Generator), 981swing, 851Swing GUI development, 1003–1006switch statement, 294–295symmetric difference ( ^ ) operator, 278symmetric difference update ( ^= ) operator, 279syntax

comments, 62continuation of lines, 62decorators, 422dictionaries, 254ease of reading, 9

Chun.book Page 1095 Thursday, April 2, 2009 9:18 AM

Page 49: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1096 Index

syntax (continued)errors, 47, 360identifiers, 68mandatory symbols, 9statement, 61

SyntaxError exception, 362, 392, 430sys module, 337–339, 403–404, 479, 491sys.argv, 338–339sys.exit function, 791sys.exit( ) function, 663–665sys.exitfunc( ) function, 665–666SystemError exception, 392SystemExit exception, 371, 391, 394, 663–664, 791

Ttables, 253, 920, 921tabs, 63Tcl, 24–26TCP (Transmission Control Protocol), 856

clients, 723–726, 735–736, 738–740servers, 720–723, 725–726, 732–738, 740

TCP/IP (Transmission Control Protocol/Internet Protocol), 717

tell( ) built-in method, 331, 334, 336, 641telnet protocol, 858tempfile module, 351, 352templates (string), 182terminating execution, 663–666

os._exit( ), 666os.kill( ), 666sys.exit( ), 663–665sys.exitfunc( ), 665–666SystemExit, 663–664

ternary operator, 295–296testing, 74–75, 437–439, 976–979text file manipulation example programs, 79–85Text widget (Tk), 825thread module, 794–799, 814threading module, 794, 800–808, 814

daemon threads, 801examples, 802–813Fibonacci/factorial/summation functions, 807–810objects, 800other functions for, 809–810producer-consumer problem,

810–813Queue module, 810–813

thread class, 801–802thread module vs., 794

threads, 801–802accessing, 792creating, 802–805definition of, 789–790examples, 792–793exiting, 791global interpreter lock, 790–791modules for, 794–813passing in callable class instance, 803–805passing in function, 802–803safety, 925spawning, 721

TIDE + IDEStudio (Tix Integrated Development Environment), 849

tilde ( ) expansion, 352“timestamp decoration” example, 425–426Tix (Tk Interface eXtensions), 840, 842–843, 849Tk, 24, 819–820, 823–826, 849Tkinter

about, 819–820, 849adding Tk to applications, 821–822examples, 826–840file system traversal GUI example, 834–840installing/working with, 820–821partial function application example, 831–834as Tk port, 24top-level window, 824

TkZinc, 849Tool Command Language (Tcl), 819Toplevel widget (Tk), 824, 825“traceback” notice, 361traceback objects, 94transferring files

about, 748client example, 760–765documentation, 765–766examples, 752–754, 759–765FTP, 748–750ftplib.FTP class methods, 750–752interactive example, 759–760Python FTP support, 750typical FTP clients, 755

Transmission Control Protocol. See TCPTransmission Control Protocol/Internet Protocol.

See TCP/IP

Chun.book Page 1096 Thursday, April 2, 2009 9:18 AM

Page 50: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Index 1097

triple quotes, 38, 193–194, 208true division ( / ), 130, 131, 569truncate( ) built-in method, 332, 334, 336try statement, 368–369, 378–379try-except statement, 47, 364–365, 372,

378–379, 382, 383try-except-else-finally statement, 381–382try-finally statement, 379–383tuple( ) built-in/factory function, 165, 166,

218–219tuples, 39–40, 231–232

assessing values of, 231–232assigning, 66–67, 231built-in functions, 165, 166, 232–234, 242–245built-in methods, 242–245concatenation of, 233creating, 231, 233default collection type, 235–236dictionary keys for, 237–238flexibility of, 234–235immutability of, 234–235as keys, 269lists vs., 237–238membership, 233non-keyword variable-length arguments, 433–434operators, 232–234, 242–245removing tuple elements/tuples, 232repetition, 233sequence operators, 232–233, 242–245single element, 236–237slicing, 233special features of, 234–238standard operators, 232–233tables of, 242–245updating, 232

twisted framework, 737–740twisted reactor TCP, 737–738type(s). See also standard types

built-in, 91built-in functions, 54, 92, 102–103, 105–110,

136, 137categorizing standard, 111–116function to check, 106–107internal, 93–95module, 615–617None, 92–93object, 90–92return values and function, 410–412

Unicode, 204unsupported, 116–117“wrapping a type,” 587

type( ) built-in functions, 54, 102–103checking types with, 105–109finding object types with, 92numbers, 136, 137table of, 110

type( ) factory functions, 102, 111, 260TypeError exception, 187, 264, 374, 375, 393,

434, 522typing, dynamic, 76

U‘U’ access mode, 326, 328–329UDFs. See functions (user-defined)UDMs. See methods (user-defined)UDP (User Datagram Protocol), 717–718

clients, 728–729servers, 726–730

unbinding, 480unbound methods, 511, 522, 541–542UnboundLocalError, 455underscores ( _, __ ), 69unhandled exceptions, 365unichr( ) function, 187, 203Unicode

CGI, 892–893codecs, 199coercion for, 204common codecs/encodings, 205decoding, 200–201definition of, 197–198encoding, 200–201exceptions, 204ordinals, 204regular expression engine, 204rules for, 201–202source code encoding, 497standard encodings, 204, 205strings, 184, 186–187, 196–205, 892–893, 1051,

1067terminology for, 197using, 198–199, 201–203

unicode( ) built-in function, 203unicode( ) factory function, 186–187UnicodeError exception, 204Uniform Resource Locators. See URLs

Chun.book Page 1097 Thursday, April 2, 2009 9:18 AM

Page 51: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

1098 Index

union ( | ) operator, 277, 279. See also update ( |= ) operator

Universal NEWLINE Support (UNS), 326, 328–329

Unix, 697–698availability of Python on, 12compiling extensions on, 964IDE for, 18–19installing Python on, 12Internet systems running, 858line separators, 333multithreaded programming, 792running Python on, 14, 16–19shell scripting languages, 7

UNS. See Universal NEWLINE Supportupdate model, 113–114, 116update ( |= ) operator, 279updating

dictionaries, 257–258lists, 210numbers, 122rows, 920, 922set types, 275strings, 169tuples, 232

uploading files, 894upward propagation (of exceptions), 365urllib module, 862–866urllib2 module, 866–869urlparse module, 861–862URLs (Uniform Resource Locators), 856, 859–861Usenet, 756User Datagram Protocol. See UDPuser input, 875–877, 886–892user interface, 920user-defined functions. See functions (user-defined)user-defined methods. See methods (user-defined)

(UDMs)UserDict module, 615, 617UserList module, 615, 617users (application), 401–402UserString module, 615, 617UTF-8 encoding, 199, 205UTF-16 encoding, 199, 205

Vvalue(s)

accessing dictionary, 255–257

Boolean, 93comparing dictionary, 262comparison of object, 93, 96–97, 108list, 209object, 90, 93, 96–97, 108set type, 275string, 169tuple, 231–232

ValueError exception, 374, 375, 393values( ) built-in method, 265, 266van Rossum, Guido, 6, 18, 23, 53, 295, 544variable-length arguments (functions), 433–439variables

accessing module, 52–53assignment of, 37, 64–67declarations for, 72, 75global vs. local, 453–455multiple assignment, 66“multuple” assignment, 66–67naming, 69scope, 453–466underscores in naming, 69using local to substitute for module attributes, 81

vars( ) built-in function, 563versions of Python, 12VisualBasic.NET, 27, 969

Wwarning(s), 393, 489–490, 927Watters, Aaron, 923Web addresses, 860Web applications, 910–911Web browsers, 755, 855, 856Web clients, 855–857, 859–875Web crawlers, 860–875Web pages, 432, 881–886Web programming, 855–912. See also CGI

(Common Gateway Interface); Internet client programmingadvanced Web clients, 869–870cookies, 856, 895–906crawlers, 860–875fully interactive sites, 886–892Internet architecture, 856–859Internet programming vs., 858–859multipart form submission/file uploading, 894multivalued fields, 895related modules, 909–912

Chun.book Page 1098 Thursday, April 2, 2009 9:18 AM

Page 52: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

Index 1099

Unicode, 202, 892–893urllib module, 862–866urllib2 module, 866–869urlparse module, 861–862URLs, 859–861user input/error processing, 886–892Web clients, 859–875

Web serversabout, 855–858CGI, 875–878, 906–909as common software server, 713exception handling, 401–402processing client data with, 875–878related modules, 911setting up, 878–879

Web services, 985–989, 1000–1002Web sites, fully interactive, 886–892Web surfing, 855–856, 859–869, 886–892while statement (loops), 42, 296–298break statement, 304–305continue statement, 305–306counting loops, 297else statement, 307–308infinite loops, 297–298and pass statement, 306–307syntax for, 296–297

while_suite, 42who command (Unix), 697–698widgets, 822–826Win32 platforms/systems

availability of Python on, 12compiling extensions on, 964multithreaded programming, 792

win32ui, 851Windows clients, 713windows servers, 713Windows/DOS platforms

IDE for, 19–21installing Python on, 12–13line separators, 333running Python on, 14–17, 19–21

with statement, 382–384–without-universal-newlines switch, 329

with_suite (context object), 385Word (Microsoft), 993–994word boundaries, matching, 678–679, 693wrapping/wrappers

adding initModule( ) module initializer function, 973–974

adding MethodDef ModuleMethods [ ] array/table, 973

an object with enhancements, 594–595any object example, 588–593built-in function (exceptions), 365–368compilation of, 974–975PyObject∗ function, 969–973try-except, 364–365using “stubs”/dummy functions, 974“wrapping a type,” 587

write( ) built-in method, 330, 336, 594–595writelines( ) built-in method, 330, 336wxGlade, 849wxPython, 840, 843–846, 849wxWidgets, 840, 843–846, 849

XX Window System, 821XML processing, 910–911xrange( ) built-in function, 95, 303xreadlines( ) method, 330, 335

YYahoo! Finance Stock Quote server, 986–989,

1001–1002yield statement, 468, 470yielding (threads), 789

Z\Z (special character), 678ZeroDivisionError exception, 362, 391zip( ) built-in function, 167, 186, 218, 303, 304,

1055, 1065-1066zip files, 490zipfile module, 351, 352zipimport, 500, 501zlib module, 351, 352

Chun.book Page 1099 Thursday, April 2, 2009 9:18 AM

Page 53: Core Python Programming, Second Edition, Fifth Printing ...cpp.wesc.webfactional.com/cpp2e/cpp5thPrintingAddendum.pdf · "Core Python Programming," Second Edition, Fifth Printing,

About the AuthorWesley Chun was introduced to the world of computing in high school,where he learned BASIC & 6502 assembly on Commodore PET/CBM sys-tems, Pascal on the Apple IIe, and FORTRAN on punch cards executed on amainframe. His first experience teaching was as a student instructor for aBASIC class for elementary schoolers and their parents.

Wesley then attended UC Berkeley where he completed an AB in appliedmathematics (computer science) and a minor in music (classical piano). AtCal, he coded in Pascal, Logo, and C. During one memorable summerinternship, he coded in a 4GL, initiating his writing career by completing afull “Getting Started” user manual.

Wesley worked briefly in Silicon Valley but missed college life and soentered graduate school at UC Santa Barbara. There, he served as a teachingassistant and taught a C programming class for UCSB Extension. His gradu-ate research lead to the completion of his master’s thesis, an abridged versionof which was published in the University of Singapore’s Journal of High Per-formance Computing.

Upon graduation, Wesley joined Sun Microsystems as an engineer forSolaris and taught UNIX, C, and Python for UC Santa Cruz Extension. Whenthe Internet went mainstream he joined Four11, where he was exposed to—and became enamored with—Python, using it full-time ever since. AtFour11, Wesley built the spellchecker and address book of what becameYahoo!Mail and, after acquisition by Yahoo!, became the lead engineer forconverting Four11’s white pages service to Yahoo! People Search. After leav-ing Yahoo!, Wesley wrote the bestselling Core Python Programming.

Wesley then traveled around the world, and upon his return entered a verydifferent line of work: analyzing spinal fractures. Soon, though, he returnedto his roots at IronPort, where his efforts continue to help keep spam andviruses out of inboxes. After updating his book for its second edition, Wesleyjoined NearbyNow, where he architected their SMS server and product res-ervation tool. At the end of his tenure there, Wesley wrote Python WebDevelopment with Django and Python Fundamentals (a LiveLessons bookletand DVD). Currently, he’s an architect for Slide.

In his spare time, Wesley is an independent consultant and technicaltrainer. His non-computing hobbies include bowling, basketball, cycling, yoga,ultimate, poker, finance, piano, and spending time with his wife and kids.He’s a member of several local usergroups and volunteers on the Tutor mailinglist. Finally, he’s responsible for creating and maintaining the “Monster Dis-cography” for the Alan Parsons Project. If you think you’re a fan but don’thave Freudiana yet, you’d better find it!

Chun.book Page 1100 Thursday, April 2, 2009 9:18 AM