Warnin$ &f youCre 0or/in$ 0ith both com#le8 and real numbers6 use the
module name such as cmat2"sNrt)* or mat2"sNrt)*% 0hen
callin$ a function9 Many of the t0o modulesC functions ha7e the
same names but 0or/ differently9 3he module name hel#s you /ee#
trac/ of 0hich function youCre usin$9
:ecimal numbers, 3he decimal module lets you 0or/ 0ith decimal
numbers rather than binary floatin$ #oint numbers9
3i# Decimal numbers $i7e more accurate results for many calculations9 See
I3urnin$ 4ython into a :alculator 0ith decimal6I later in this cha#ter9
/andom numbers, 3he random module includes tools for such tas/s as
$eneratin$ random numbers and choosin$ randomly from se7eral elements9
For a fun introduction to the tric/s u# the slee7e of the random module6 see
:ha#ter 2-9
Turnin" ython into a #alculator with decimal
3he decimal module6 ne0 in 4ython 29!6 su##orts decimal floatin$ #oint arithmetic9
&f youCre 0ritin$ a##lications that reGuire control o7er #recision and roundin$ such as
financial soft0are or statistics modules%6 this module is for you9 &ts results also match
calculations done by hand math educators ta/e note5%9
-06
3he decimal module $i7es more accurate results than binary floatin$ #oint math for
these ty#es of calculations) Calculating a percentage, modulo calculations 0hich return
the fractional result of di7ision%6 and e7uality tests.
.i/e other numbers6 decimal numbers are immutable9
3i# 3o im#ort the decimal module6 use this statement)
from decimal import Oecimal
/epresenting nubers by using the decial odule
3o create a decimal number6 ty#e the follo0in$6 substitutin$ your number or obFect inside
the #arentheses)
Oecimal)numerical_object*
3he numerical_object can be any of these ty#es of data)
An inte$er
Oecimal)3*
A calculation
Oecimal)3;&*
A number re#resented as a strin$
Oecimal)"3"2"*
A float con7erted to a strin$ 0ith the str)* function
>>> x = 3"2
>>> Oecimal)str)x**
Oecimal)"3"2"*
Warnin$ Eou canCt use floatin$ #oint numbers directly 0ith the decimal module9
3hey must be con7erted to strin$s first9
A three1item tu#le
>>> Oecimal))%, )3, /, .*, 72**
Oecimal)"3"/."*
3i# 3o create a decimal from a three1item tu#le6 use this format for the tu#le)
-9 3he first item in the tu#le is % for #ositi7e% or / for ne$ati7e%9
29 3he second item is another nested% tu#le containin$ the di$its of the
number6 one element for each di$it9
"9 3he third item is a #ositi7e or ne$ati7e inte$er s#ecifyin$ the
e8#onent9
Oecimal numbers 0or/ 0ith 4ythonCs re$ular math functions9 ;i7e them names and use
the names as ar$uments6 or insert them directly as ar$uments)
>>> po9)Oecimal)=*, 2*
-0+
Oecimal)"'/"*
Vie&ing and changing paraeters
3he decimal module has #arameters that control ho0 it 0or/s 0ith and dis#lays
numbers9 3o see the #arameters6 ty#e the follo0in$)
>>> import decimal
>>> decimal"getcontext)*
Hontext)prec=2', ro-nding=BJ41O$HL3>$6W61, 6min=7=========,
6max==========,
capitals=/, flags=[], traps=[Oi5isionIyKero, Gn5alidJperation,
J5erflo9]*
Mne of the #arameters is the ma8imum #recision of decimal numbers ho0 many di$its to
the ri$ht of the decimal #oint you see%9 3o chan$e the #recision #arameter6 ty#e
somethin$ li/e this)
>>> decimal"getcontext)*"prec = E
3i# &f you 0ant to chan$e the #recision of a s#ecific number 0ithout chan$in$ the
#recision #arameter for all the numbers6 use the N-antiQe)* method9 N-antiQe)*
rounds the number to a s#ecific number of decimal #laces9 3his e8am#le rounds to
t0o decimal #laces)
>>> data = Oecimal),E"32.,*
>>> data"N-antiQe)Oecimal),"%/,**
Oecimal)"E"32"*
-0,
#hapter G, rocessin" -ists and Tuples
.ists and tu#les are t0o of 4ythonCs compound data types6 also called container ob-ects9
:om#ound data ty#es $rou# collections of data9
4ython has se7eral /inds of com#ound data ty#es9 .ists and tu#les are se7uence ty#es
because each element of data is numbered seGuentially6 startin$ 0ith 09 SeGuence ty#es
su##ort o#erations called inde!ing and slicing for 0or/in$ 0ith indi7idual elements or
subsets of elements9 Eou can find out about inde8in$ and slicin$ in :ha#ter 69
A seGuence data ty#e is $ood 0hen you 0ant to o#erate on all the elements in a collection
or 0hen the order of elements in a collection is im#ortant9 &n contrast6 the dictionary data
ty#e>a mapping ty#e>is for random access to elements6 and a set is for com#arin$
collections of obFects9 Dictionaries and sets are co7ered in :ha#ter *9%
3his cha#ter sho0s you the synta8 for lists and tu#les6 0hen to use them6 ho0 to 0or/
0ith elements inside them6 ho0 to use them in loo#s6 and ho0 to a7oid some of the
common errors #ro$rammers ma/e 0hen 0or/in$ 0ith them9
Introducin" -ists and Tuples
3he follo0in$ sections describe the features and synta8 of lists and tu#les and e8#lain
0hen to use each9
*hat a list is
A list is a mutable data ty#e6 0hich means you can chan$e the contents of a list 0ithout
creatin$ a ne0 list9 3he elements of a list can be of different data ty#es9 A sin$le list can
contain numbers6 strin$s6 other lists6 tu#les>and e7en functions and classes9
&n 4ython6 a list literal the actual data6 not a name referrin$ to the data% is defined by
sGuare brac/ets surroundin$ zero or more elements9 @lements are se#arated by commas9
'elo0 are e8am#les of lists)
a$list = [,t2is,, ,is,, ,a,, ,list,]
empty$list = []
anot2er$list = [&]
.ists come 0ith a 7ariety of built1in methods that let you mani#ulate them9 For e8am#le6
to sort a list6 you 0rite code li/e this)
>>> mylist = [', 33, 2=, ., /, &, =']
>>> mylist"sort)*
>>> mylist
[/, ., &, ', 2=, 33, =']
-0*
*hat a tuple is
A tu#le is a seGuence data ty#e that can contain elements of different data ty#es9 3u#les
are different from lists because tu#les are immutable. &f you 0ant to chan$e the contents
of a tu#le6 you must create a ne0 tu#le that has the ne0 content you 0ant9
Warnin$ &f a tu#le contains a mutable element such as a list or dictionary%6 the mutable
element can be modified9 <o0e7er6 some of the ob7ious 0ays of chan$in$ the
mutable element 0ill cause errors because 4ython 0ill attem#t to modify the
tu#le9 We recommend that you a7oid chan$in$ mutable elements in tu#les9
3u#les donCt include a lot of s#ecial methods6 the 0ay strin$s and lists do9 'ut tu#les do
include methods that su##ort standard 4ython o#erators9 For e8am#le6 you can add
tu#les6 chec/ 0hether an item is inside a tu#le6 slice elements in a tu#le6 com#are a tu#le
to other data6 find a tu#leCs len$th6 and so on9
Many 4ython functions6 such as the time and date functions6 return tu#les9
#hoosin" between lists and tuples
.ists and tu#les loo/ similar at first $lance6 but they ha7e intrinsic differences)
A tu#le is immutable6 and a list is mutable9
3u#les are more memory1efficient9
.ists ha7e se7eral useful built1in methods9
3u#les are often better than lists for information that you donCt 0ant to chan$e6 for t0o
reasons) 3hey use less memory6 and because theyCre immutable% they 0onCt chan$e
une8#ectedly9
.ists are often better for information you 0ant to chan$e because there are more 0ays of
mani#ulatin$ them9
;uido 4ythonCs creator% and the 4ython community #romote the follo0in$ con7entions
for choosin$ bet0een lists and tu#les)
Tuples for hetero"eneous data, lists for homo"eneous data, ?se a tu#le if your
data includes se7eral different data ty#es6 such as names and addressesH use lists
for elements that are all of the same ty#e9
For e8am#le6 if youCre accessin$ a database6 the fields of a record mi$ht be
returned as a tu#le6 but the records themsel7es should be returned as a list9
3herefore the o7erall database 0ould be a list of tu#les9
Tuples for se=uence keys, &f you need to use a seGuence as a dictionary /ey6 you
must use a tu#le because dictionary /eys are immutable9
--0
Tuples for some functions, Some functions reGuire ar$uments to be #assed in
tu#les9
-ists for mutable ob>ects, A7oid usin$ mutable obFects in immutable containers
because errors and une8#ected results can occur if you attem#t to chan$e the
mutable obFects9
A tuple literal is defined in one of three 0ays)
A sin$le element follo0ed by a comma
Multi#le elements se#arated by commas
An em#ty set of #arentheses
When 4ython out#uts a tu#le6 it encloses the elements in #arentheses9 Eou can and
usually should% use #arentheses 0hen creatin$ a tu#le6 but you donCt ha7e to9 'elo0 are
e8am#les of tu#les9
>>> a$t-ple = ),t2is,, ,is,, ,a,, ,t-ple,*
>>> t-ple$2 = "t2is", "is", "anot2er", "t-ple"
>>> t-ple$2
),t2is,, ,is,, ,anot2er,, ,t-ple,*
>>> empty$t-ple = )*
A tu#le e8ce#t for an em#ty tu#le% must al0ays use a comma6 e7en if there is only one
element6 because the comma is the o#erator that defines a tu#le9
3i# 'ecause the comma is overloaded that is6 it does more than one thin$ in 4ython% and
because it has lo0 #recedence that is6 most other o#erations are e7aluated before
comma o#erations%6 itCs usually a ;ood &dea to add #arentheses to ma/e it clear that
you are usin$ a tu#le9
'elo0 are the results of usin$ the ; o#erator on a sin$le1element tu#le and on an inte$er9
>>> anot2er$t-ple = )3,*
>>> anot2er$t-ple ; 3
)3, 3, 3*
>>> not$a$t-ple = )3*
>>> not$a$t-ple ; 3
=
---
4anipulatin" !e=uence (b>ects
3he follo0in$ sections describe ho0 to com#are6 combine6 and search the contents of
lists6 tu#les6 and other seGuence obFects9
#oparing se3uence ob'ects
SeGuence obFects such as lists can be com#ared to other obFects of the same data ty#e9 A
com#arison tells you 0hether the obFects are eGual or 0hether one is smaller than the
other9 <ereCs ho0 it 0or/s)
3he first items of each seGuence are com#ared6 and then the second items6 and so
on9
&f an item inside a seGuence is itself a seGuence6 then each of its items is
com#ared a$ainst the item of the same number in the other seGuence9
&f all items of t0o seGuences are eGual6 the seGuences are eGual9
&f seGuence - is the same as the be$innin$ of seGuence 26 seGuence - is smaller
lesser%9
Strin$s are com#ared based on the AS:&& order of their characters9 :ha#ter 6 has
more information about character order9
3able ,1- sho0s the results of com#arisons bet0een seGuences9
3able ,1-) :om#arisons bet0een SeGuences
M#en table as s#readsheet
#omparison /eason
)/, 2, 3* # )/, 2, .*, [/, 2,
3] # [/, 2, .]
" is less than !
,LIH, # ,H, # ,Cascal, #
,Cyt2on,
AS:&& order of characters
)/, 2, .* > )/, 2, 3, .*, )/,
2* # )/, 2, 7/*
First seGuence is the same as the be$innin$ of
second seGuence
)/, 2, 3* == )/"%, 2"%, 3"%*
&nte$ers eGual their float eGui7alents
)/, 2, ),aa,, ,a(,** # )/, 2,
),a(c,, ,a,*, .*
Al#habetical order of characters in sub1seGuence
JaaK is smaller than JabcK%
&f you com#are obFects of different ty#es6 the ty#es are ordered al#habetically by their
names) .ists are smaller than strin$s6 0hich are smaller than tu#les9 3he rules for
com#arin$ obFects of different ty#es may chan$e in future 7ersions of 4ython9
--2
0perating on se3uence ob'ects
3he follo0in$ o#erators 0or/ on lists and tu#les9 Most of these o#erators also 0or/ on
other data ty#es)
Addition :%) :oncatenates the contents of lists to$ether to form a ne0 list6 or
concatenates the contents of tu#les to$ether to form a ne0 tu#le9 Eou canCt
concatenate a list and a tu#le9%
>>> [/, 2, 3] : [2, .]
[/, 2, 3, 2, .]
!e=uence multiplication ;%) De#eats the contents of a list or a tu#le9
Au"mented assi"nment := and ;=%) Simultaneously concatenates and assi$ns or
re#eats and assi$ns9 3he statement mylist := ne9list is the same as mylist =
mylist : ne9list9 3he former 0or/s sli$htly faster and is easier to read9
#omparison # >6 ==6 !=%) 3ests 0hether the contents of t0o or more lists or t0o
or more tu#les are the same9
#ontents test in%) 3he /ey0ord in determines 0hether a list or tu#le contains
the $i7en element and returns 0r-e or >alse6 as sho0n here)
>>> p2one$so-nds = ),(eep,, ,ring,, ,flig2t of t2e (-m(le(ee,*
>>> y = ,(eep, in p2one$so-nds
>>> y
0r-e
-istcraft, 4ethods, Indexes, and !lices
&n this section 0e describe im#ortant 0ays you can 0or/ 0ith lists9 Eou can turn other
data into lists6 count items in lists6 sort lists in 7arious orders6 and use inde8in$ and
slicin$ to chan$e6 add6 delete6 and mo7e indi7idual list items9
%unctions that &ork &ith or create lists
3he list)* function creates a co#y of a list9 &t also turns any other seGuence obFect or
iterable into a list9 An iterable is an obFect 0hose elements can be retrie7ed one at a
time9% 3his e8am#le turns a tu#le into a list)
>>> myt-ple = ),apple,, ,orange,, ,pear,*
>>> mylist = list)myt-ple*
>>> mylist
[,apple,, ,orange,, ,pear,]
&f you feed a strin$ to the list)* function6 it ma/es a se#arate list item out of each
character6 as in this e8am#le9
>>> mystring = "Ioo!"
>>> list)mystring*
--"
[,I,, ,o,, ,o,, ,!,]
3o find out the number of elements in a list6 use the len)* function it also 0or/s on
other data ty#es%9
>>> len)mylist*
3
(ethods of lists
A method is li/e a function e8ce#t that it Ibelon$sI 0ith a #articular obFect6 and 0hen
you call it6 you 7ualify it 0ith the name of the obFect9 3he method names are attributes of
the obFect9
-ist method syntax
3o call a list method6 ty#e the list name6 a dot6 the method name6 and #arentheses9 &n the
#arentheses6 #ass any ar$uments the method needs9 Some methods donCt reGuire or use
ar$uments9% <ereCs an e8am#le)
list$name"met2od$name)arg-ments*
The most popular list methods
3he follo0in$ are some of the most freGuently used list methods)
3o add an item to the end of a list6 use the append)* method9 3his method
chan$es the list in #lace9 &t returns 1one9
3his e8am#le adds the strin$ ,apple, to a list)
(asAet = [,apple,, ,(anana,, ,orange,]
(asAet"append),apple,*
3o find out the number of times a 7alue occurs in a list6 use the co-nt) value *
method9
3his e8am#le counts the number of times the strin$ ,apple, a##ears9
>>> (asAet"co-nt),apple,*
2
3o add to a list the contents of another seGuence obFect or iterable an obFect
0hose elements can be retrie7ed one at a time%6 use the extend)* method9 &f the
iterable is a strin$6 each character is added indi7idually9
3his e8am#le adds each character of the strin$ ,pear,9
--!
>>> x = [,apple,, ,apple,, ,(anana,, ,orange,]
>>> x"extend),pear,*
>>> x
[,apple,, ,apple,, ,(anana,, ,orange,, ,p,, ,e,, ,a,, ,r,]
3o delete the first occurrence of an item in a list6 use the remo5e)* method9 &t
raises a Wal-e6rror if the item isnCt found9
3his e8am#le remo7es the first ,apple, strin$9
>>> x"remo5e),apple,*
>>> x
[,apple,, ,(anana,, ,orange,, ,p,, ,e,, ,a,, ,r,]
3he sort)* and re5erse)* methods both chan$e the list itself9
o 3he sort)* methodCs default orderin$ is al#habetical for lists containin$
te8t6 numerical for lists containin$ numbers6 and so on9
o 3he re5erse)* method in7erts the #ositions of items in a list9
3i# 4ython 29! and later also su##ort these built1in functions)
o sorted)*6 0hich returns a sorted copy of the list or other iterable%9
o re5ersed)*6 0hich returns an iterator obFect that lets you use a loo#
to #rocess the items in the list in re7erse order9
3he follo0in$ e8am#les sho0 the use of the sort)* and re5erse)* methods on a
list of strin$s)
>>> x"sort)*
>>> x
[,a,, ,apple,, ,(anana,, ,e,, ,orange,, ,p,, ,r,]
>>> x"re5erse)*
>>> x
[,r,, ,p,, ,orange,, ,e,, ,(anana,, ,apple,, ,a,]
List indexing and slicing
.ist and tu#le elements ha7e inde8 numbers9 &nde8in$ and slicin$ o#erations on lists and
tu#les 0or/ mostly the same 0ay that they do 0ith strin$s9 <ereCs a Guic/ re7ie0 of ho0
inde8 numbers 0or/9 For more about inde8in$ and slicin$6 see :ha#ter 69
Wor/in$ from left to ri$ht6 the first element has the inde8 06 the ne8t has the inde8
-6 and so on9
3his e8am#le sho0s the items at inde8 0 and - of the list x9
--A
>>> x = [,apple,, ,(anana,, ,pear,]
>>> x[%]
,apple,
>>> x[/]
,(anana,
Wor/in$ from ri$ht to left6 the first element has the inde8 7/6 the ne8t one 726 and
so on>but the leftmost element is still 09
3his e8am#le sho0s the items at inde8 7/ and 729
>>> x[7/]
,pear,
>>> x[72]
,(anana,
3he be$innin$s and ends of slices sit bet$een the elements6 li/e noteboo/
di7iders9
3his e8am#le sho0s that slice %!/ contains Fust one element9
>>> x[%!/]
[,apple,]
How indexin" and slicin" are different with a list
&nde8in$ and slicin$ 0or/ sli$htly differently 0ith lists than they do 0ith strin$s and
tu#les because lists are mutable9 Eou can chan$e a list by usin$ inde8in$ and slicin$9
With a tu#le or a strin$6 you need to $i7e a ne0 name to the chan$ed obFect or reassi$n
the same name%9
/etrievin" items from a list with slicin"
Follo0in$ is a brief re7ie0 of slicin$ synta89 Eou can find more information in :ha#ter 69
3he synta8 for slicin$ is as follo0s)
seN-ence$o(Tect$name[firstindex!lastindex!step]
3hese are the rules for slicin$)
%ou can leave out the first slice index 5but not the colon followin" it6. &t
defaults to %9
%ou can leave out the last slice index. &t defaults to the last item of the seGuence9
3o access the last seGuence item in a slice6 if you donCt /no0 the len$th of the
seGuence6 lea7e the inde8 blan/9
--6
%ou can leave out the step and the colon precedin" it. 3he ste# defaults to /9
*hen you specify a ran"e, it be"ins with the first slice index specified and
ends !e%ore the last slice index specified. 3his is because slices #oint bet0een
elements9 So [%!2] retrie7es the first and second elements9
The notation [:] returns a shallow copy of the whole se=uence.
3he section6 I.ist references that une8#ectedly chan$e6I later in the cha#ter6 has
more on shallo0 co#yin$9
3he follo0in$ e8am#le code creates a list of numbers and then6 usin$ slice synta86 selects
and dis#lays all the e7en1numbered items9
>>> mylist = range)%,/%*
>>> mylist[!!2]
[%, 2, ., <, ']
#han"in" a list item by usin" its index number
Mne 0ay to chan$e the contents of a list is to assi$n a ne0 item to a #articular inde8
number also called an offset%9 3his re#laces 0hate7er is already at that inde8 number9
+otcha< Assi"nin" numbers and strin"s to slices
&f you assi$n to a slice6 you must assi$n an iterable9 3his means t0o thin$s)
-9 Eou canCt assi$n a number to a slice9
2" >>> mylist = [/, 2, 3, ., &, <]
3" >>> mylist [2!3] = '
." 0race(acA )most recent call last*!
&" >ile "#stdin>", line /, in V
<" 0ype6rror! can only assign an itera(le
+9 &f you assi$n a strin$ to a slice6 each letter becomes a se#arate list item9
'" >>> mylist[2!.] = ,t2ree,
=" >>> mylist
/%"[/, 2, ,t,, ,2,, ,r,, ,e,, ,e,, &, <]
//"9ay aro-nd t2is second pro(lem is to p-t t2e n-m(er
Mne 0ay around this second #roblem is to #ut the number or strin$ into a list9 3he
follo0in$ e8am#le assumes you 0ant to re#lace the numbers " and ! 0ith the strin$s
JthreeK and JfourK)
>>> mylist = [/, 2, 3, ., &, <]
>>> mylist[2!.] = [,t2ree,, ,fo-r,]
>>> mylist
[/, 2, ,t2ree,, ,fo-r,, &, <]
3o assi$n a ne0 item to a list by inde8 number6 follo0 these ste#s)
--+
-9 Type the list name and then, in s=uare brackets, type the index number, like
this,
2" mylist[/]
"9 (n the same line, type = and the new list element, so your line looks
somethin" like this,
." mylist[/] = new_list_element
&n the follo0in$ e8am#le6 the element at inde8 / the strin$ ,eggs,% is re#laced 0ith the
strin$ "lo5ely spam"9
>>> 3 = [,spam,, ,eggs,, ,2am,]
>>> 3[/] = ,lo5ely spam,
>>> 3
[,spam,, ,lo5ely spam,, ,2am,]
Eou canCt assi$n to an inde8 number that doesnCt e8ist9 4ython raises an Gndex6rror6 as
sho0n here)
>>> 3[3] = ,green eggs,
0race(acA )most recent call last*!
>ile "#stdin>", line /, in #mod-le>
Gndex6rror! list assignment index o-t of range
3o add items to the end of a list6 use the append)* method)
>>> 3"append),green eggs,*
>>> 3
[,spam,, ,lo5ely spam,, ,2am,, ,green eggs,]
Assi"nin" to a list with slicin"
3o assi$n se7eral items in a ro0 to a list6 use a ran$e of inde8 numbers9
Assi$nin$ usin$ a slice follo0s these rules)
4ython first deletes the items currently assi$ned to those inde8 numbers and then
inserts the ne0 items9
Eou donCt ha7e to assi$n the same number of items that you delete9
Eou must assi$n an iterable see the sidebar6 I;otcha5 Assi$nin$ numbers and
strin$s to slicesI%9
3o assi$n items to a list slice6 follo0 these ste#s)
-9 Type the name of the list and, in brackets, type the slice notation, like this,
2" 3[/!.]
"9 (n the same line, type = and the items you want to assi"n.
--,
&f you assi$n items from another list6 your 0hole line mi$ht loo/ somethin$ li/e
this)
3[/!.] = [,spam,, ,spam,, ,spam,]
3he resultin$ list no0 loo/s li/e this)
[,spam,, ,spam,, ,spam,, ,spam,]
3he follo0in$ e8am#le mani#ulates a list of 0ords by usin$ slice assi$nment)
>>> 3 = [,spam,, ,eggs,, ,2am,, ,green eggs,]
>>> 3[!2] = )"don,t", "9ant"*
>>> 3
["don,t", ,9ant,, ,2am,, ,green eggs,]
>>> 3[2!] = )"green eggs", "and", "2am"*
>>> 3
["don,t", ,9ant,, ,green eggs,, ,and,, ,2am,]
-ist indexin" and slicin" operations
3he follo0in$ o#erations use the inde8in$ and slicin$ features of lists)
3o return the inde8 number of a list item6 use the index)* method9
&f the item isnCt in the list6 it raises a Wal-e6rror9
3his e8am#le returns the inde8 number of the second item in the list)
>>> mylist = [/, ,t9o,, 3]
>>> mylist"index),t9o,*
/
3o remo7e from a list the items% corres#ondin$ to the $i7en inde8 number or
slice6 use the /ey0ord del9
3his e8am#le deletes t0o items9
>>> mylist2 = [,(-sy signal,, ,fast (-sy signal,, ,no s-c2
n-m(er,, ,at t2e
tone, t2e time 9ill (e,]
>>> del mylist2[/!3]
>>> mylist2
[,(-sy signal,, ,at t2e tone, t2e time 9ill (e,]
3o insert an item before a #articular inde8 number6 use the insert)* method9
>>> (asAet"insert)%, ,peac2,*
>>> (asAet
[,peac2,, ,apple,, ,(anana,, ,orange,]
--*
3o remo7e and return an item at a #articular inde8 number6 use the pop)* method9
3he default inde8 number for pop)* is the last item in the list9 3his e8am#le
remo7es and returns the item ,(aAed (eans, in the list (reaAfast)
>>> (reaAfast = [,spam,, ,spam,, ,spam,, ,(aAed (eans,, ,spam,,
,spam,]
>>> (reaAfast"pop)3*
,(aAed (eans,
>>> (reaAfast
[,spam,, ,spam,, ,spam,, ,spam,, ,spam,]
3he follo0in$ code uses a 92ile loo# to return and delete all the list elements one
at a time)
>>> 92ile (reaAfast!
""" (reaAfast"pop)*
"""
,spam,
,spam,
,spam,
,spam,
,spam,
>>> (reaAfast
[]
!teerin" #lear of -ist +otcha's
3his section describes ho0 to a7oid some common list errors9
Siultaneous test9add9delete
&f youCre testin$ a list condition in a 92ile loo# or iteratin$ o7er a list in a for loo#6
a7oid addin$ and deletin$ list elements at the same time9 &tCs easy to lose trac/ of ho0 the
list is chan$in$9 An e8ce#tion is the use of pop)* in a 92ile loo# to remo7e all list
elements one by one9
List references that unexpectedly change
'ecause lists are mutable6 you can chan$e them 0ithout ma/in$ a co#y9 'ut if you
reference a sin$le list from se7eral #laces in your code6 it mi$ht chan$e 0hen you arenCt
e8#ectin$ it to9
!hallow copyin"
&f a container ob-ect an obFect>such as a list6 tu#le6 or dictionary>that holds other
obFects% contains lists6 then ma/in$ a co#y of the container obFect by usin$
ne9container = oldcontainer or ne9container = oldcontainer[!] doesnCt ma/e a
co#y of the lists it contains9 3he ne0 container continues to refer to the ori$inal lists9 3his
-20
is called shallo$ copying6 and 4ython uses it because amon$ other reasons% it sa7es
resources9 3he follo0in$ e8am#le illustrates shallo0 co#yin$9 3he larder list chan$es
0hen one of the lists inside the food list is chan$ed)
>>> (reaAfast = [,spam,, ,(aAed (eans,]
>>> (asAet = [,peac2,, ,apple,, ,goose(erries,]
>>> food = [(reaAfast, (asAet]
>>> larder = food
>>> larder
[[,spam,, ,(aAed (eans,], [,peac2,, ,apple,, ,goose(erries,]]
>>> del (asAet[%]
>>> larder
[[,spam,, ,(aAed (eans,], [,apple,, ,goose(erries,]]
:eep copyin"
3i# Sometimes you 0ant to ma/e a full6 or deep, co#y of a list or any item that contains
other items%9 3o do so6 im#ort the copy module and use the copy"deepcopy)*
function9 &n this e8am#le6 0hen you co#y the food list to the larder list by usin$ the
deepcopy)* function6 chan$in$ the contents of one of the lists in food doesnCt
chan$e the contents of larder9
>>> food
[[,spam,, ,(aAed (eans,], [,apple,, ,goose(erries,]]
>>> import copy
>>> larder = copy"deepcopy)food*
>>> larder
[[,spam,, ,(aAed (eans,], [,apple,, ,goose(erries,]]
>>> del (asAet[%]
>>> food
[[,spam,, ,(aAed (eans,], [,goose(erries,]]
>>> larder
[[,spam,, ,(aAed (eans,], [,apple,, ,goose(erries,]]
1isappearing lists
3he list methods append)* and sort)* chan$e a list in #lace but donCt return the list9
3hey return 1one9 DonCt use these methods in assi$nment statements or else the list mi$ht
disa##ear6 as in this e8am#le)
>>> 3 = ["my", "list"]
>>> 3 = 3"append),x,* + 9rong!
>>> print 3
1one
3o a##end to a list6 Fust use the append)* method by itself9 DonCt $i7e the result a name9
>>> 3 = ["my", "list"]
>>> 3"append),x,*
>>> print 3
[,my,, ,list,, ,x,]
-2-
That tricky asterisk
&f you re#eat ;% a list6 you $et a list 0ith multi#le co#ies of its elements9 'ut if you
re#eat ;% a list containing a list6 you $et multi#le shallo0 co#ies of the contained list9
3he follo0in$ e8am#les demonstrate both re#eatin$ a list and re#eatin$ a list that
contains a list)
A re#eated list)
>>> mylist = [3, ,(lind,, ,mice,]
>>> x = mylist ; 2
>>> x
[3, ,(lind,, ,mice,, 3, ,(lind,, ,mice,]
De#eatin$ a list containing a list)
>>> y = [mylist] ; 2
>>> y
[[3, ,(lind,, ,mice,], [3, ,(lind,, ,mice,]]
&n the #recedin$ e8am#les6 if you chan$e mylist6 the 7alue of y chan$es because y
contains references to mylist9 'ut the 7alue of x doesnCt chan$e because x only contains
co#ies of the elements of mylist9 <ereCs 0hat ha##ens)
We chan$e the first element of mylist)
>>> mylist[%] = ,t9o,
<ereCs the result)
y reflects the ne0 7ersion of mylist)
>>> y
[[,t9o,, ,(lind,, ,mice,], [,t9o,, ,(lind,, ,mice,]]
x doesnCt chan$e)
>>> x
[3, ,(lind,, ,mice,, 3, ,(lind,, ,mice,]
Perforance probles
Mani#ulatin$ lar$e lists can cause #erformance #roblems9 <ereCs ho0 to a7oid these
#roblems)
Avoid copyin" lon" lists.
For e8am#le6 a7oid usin$ loo#s that delete elements from the be$innin$ of a list
del mylist[%] or mylist"pop)%*% because doin$ so reGuires 4ython to ma/e
multi#le co#ies of the list9
Avoid makin" multiple copies of the elements of a list.
-22
:o#yin$ the list itself doesnCt ta/e a lot of time6 but co#yin$ the elements does9
Build lists when you need them. 4o#ulatin$ a list 0ith elements before you need
the list isnCt necessary6 and it mi$ht e7en 0aste memory9
Buildin" -ists, !tacks, and Iueues
3his section describes t0o often1used list tas/s) buildin$ a list one element at a time and
buildin$ stac/s and Gueues9
Building lists increentally
'ecause lists are mutable6 you can build them one element at a time9 &t is often useful to
build lists from other iterable obFects or to combine multi#le lists9 3he easiest 0ay of
doin$ this is to 0rite a for loo# and build the ne0 list 0ith the append)* method6 as in
the follo0in$ e8am#le)
3/ = [,spam,, ,spam,, ,spam,, ,(aAed (eans,, 1one, ,spam,, ,spam,]
32 = []
for i in 3/!
if i is not 1one!
32"append)i*
After you run the abo7e code6 the 7alue of 32 is ["spam", "spam", "spam", "(aAed
(eans", "spam", "spam"]9
Warnin$ &f youCre iteratin$ o7er a list in a for loo#6 itCs best not to add elements to or
delete elements from that list>doin$ so can introduce errors9 &tCs safer to use
code li/e the abo7e e8am#le to build a ne$ list and then chan$e the elements of
the ne0 list9
4ython #ro7ides se7eral other tools for buildin$ lists)
3o con7ert or co#y seGuences and other iterable obFects% to a list6 use the list)*
function described earlier in this cha#ter%9
Another 0ay to co#y a 0hole list or other seGuence and return a shallo0 co#y is
to use this slice notation) mylist[!]
4ython includes a feature called a list comprehension, 0hich 0or/s in much the
same 0ay as a list1buildin$ for loo# but is #ac/a$ed more com#actly9 See
:ha#ter -6 to find out about this ad7anced feature9%
3o loo# o7er t0o or more seGuences at the same time6 use the Qip)* function9 &t
returns a list of tu#les that associates the items in one seGuence 0ith the
corres#ondin$ items in another seGuence9 &t also truncates the resultin$ list to the
len$th of the shortest seGuence9
-2"
>>> a = [', 3, &, //]
>>> ( = ),eggs,, ,spam,*
>>> c = Qip)a, (*
>>> print c
[)', ,eggs,*, )3, ,spam,*]
&f you use Qip)* a$ain 0ith the ;args style #arameter6 you $et a list of tu#les that
corres#ond to the ori$inal seGuences you started 0ith but any items that 0ere
truncated are missin$%9
3i# 3he ;args style ar$ument is e8#lained in :ha#ter --9
<ereCs 0hat it could loo/ li/e)
>>> Qip);c*
[)', 3*, ),eggs,, ,spam,*]
3o $et tu#les of list inde8 numbers and their elements6 use the en-merate)*
iterator9
>>> a = [', 3, &, //]
>>> for G, item in en-merate)a*!
""" print )G, item*,
"""
)%, '* )/, 3* )2, &* )3, //*
Stacking and 3ueuing &ith lists
,tacks and 7ueues are conce#ts that describe ho0 items $et added to and remo7ed from
lists9 Stac/s and Gueues are not 4ython obFects6 but you can 0rite code in 0hich a list
beha7es li/e a stac/ or a Gueue9
A stack is li/e a s#rin$1loaded #late dis#enser 0here the last item on is the first item off
this is also called Ilast in6 first outI order6 0hich is inscribed in the ;reat 'oo/ of ;ee/y
Acronyms as I.&FMI%9 A Gueue6 as fol/s familiar 0ith 'ritish @n$lish /no06 is li/e a line
at a store chec/out counter) 3he first #erson in is the first ser7ed also called Ifirst in6 first
out6I or IF&FMI%9
Addin" items to a stack
3o add an item to the to# of a stac/6 use append)*9
3his e8am#le #uts si8 #lates on the #ile)
>>> plates = []
>>> for p in range)<*!
""" plates"append)p*
"""
>>> plates
-2!
[%, /, 2, 3, ., &]
/etrievin" items from a stack
3o retrie7e an item from the to# of a stac/6 use pop)* 0ithout an ar$ument so it defaults
to the last item in the list%9
3o retrie7e all the items6 use pop)* in a 92ile loo#9 ;et the #lates off the #ile6 last1on6
first1off6 li/e this)
>>> 92ile plates!
""" plates"pop)*
"""
&
.
3
2
/
%
Iueuin" up
3o use a list as a Gueue to #rocess items Iin the order in 0hich they 0ere recei7edI
anyone 0ho has tried to tal/ to a lar$e com#any on the tele#hone 0ill be familiar 0ith
that #hrase%6 follo0 these ste#s)
-9 &se append)* to add an item to the end of the =ueue.
&n the follo0in$ e8am#le6 0e create an em#ty list and then a##end si8 callers to it)
>>> callers = []
>>> for c in range)<*!
""" callers"append)c*
"""
>>> callers
[%, /, 2, 3, ., &]
29 &se pop)%* in a 92ile loop to retrieve an item from the front of the =ueue,
like this,
3" >>> 92ile callers!
." """ callers"pop)%*
&" """
<" %
E" /
'" 2
=" 3
/%".
//"&
Warnin$ 3o a7oid #erformance #roblems6 donCt use pop)%* 0ith a lar$e list more
than about -00 elements%9
-2A
Takin" Tuples in Hand
3his section describes some 0ays to 0or/ 0ith tu#les9 We co7er con7ertin$ other obFects
to tu#les6 se7eral 0ays of creatin$ tu#les6 usin$ tu#les to s0a# 7alues6 and efficiently
$i7in$ names to indi7idual items in tu#les9
#on$erting another ob'ect to a tuple
3o con7ert another iterable obFect to a tu#le6 use the t-ple)* function)
>>> mylist = [/, 2, 3]
>>> t-ple)mylist*
)/, 2, 3*
D@M@M'@D &f you con7ert a list to a tu#le6 remember that the ne0 tu#le cannot be
chan$ed9
Tuple packing
&f you ty#e some names se#arated by commas6 you create a tu#le9 3his is called tuple
packing. &t 0or/s on either side of an assi$nment statement9
3he follo0in$ e8am#le creates a tu#le containin$ t0o strin$s)
>>> N = ,(la2, (la2, (la2,
>>> r = ,SG1S6B,
>>> 92at$t2ey$2ear = N, r
>>> print 92at$t2ey$2ear
),(la2, (la2, (la2,, ,SG1S6B,*
3o create a tu#le containin$ more than t0o items6 Fust use multi#le comma o#erators9
3his e8am#le creates a tu#le containin$ four strin$s9
>>> N, r, N, r
),(la2, (la2, (la2,, ,SG1S6B,, ,(la2, (la2, (la2,, ,SG1S6B,*
Tuple unpacking
)npacking a tuple means $i7in$ a different name to each element of the tu#le9
3u#le un#ac/in$ is useful because many functions in 4ython6 such as the Qip)* function
described in the #re7ious I'uildin$ lists incrementallyI section6 return tu#les9 With tu#le
un#ac/in$6 you can easily $et at the indi7idual items in these tu#les9
3o unpack a tu#le6 Fust assi$n multi#le names on a sin$le line9 4ut the names you 0ant to
un#ac/ into on the left side of the assi$nment statement and #ut the tu#le on the ri$ht
side9 Eou need one name for each item in the tu#le9
-26
3he follo0in$ e8am#le un#ac/s a tu#le consistin$ of three lists)
>>> x = [),rocA cr-s2es,, ,scissors,*, ),paper co5ers,, ,rocA,*,
),scissors
c-t,, ,paper,*]
>>> rocA, paper, scissors = x
>>> print rocA
),rocA cr-s2es,, ,scissors,*
&f you donCt ha7e the same number of names as tu#le items6 you $et a Wal-e6rror6 as
sho0n here)
>>> rocA, paper = x
0race(acA )most recent call last*!
>ile "#stdin>", line /, in #mod-le>
Wal-e6rror! too many 5al-es to -npacA
3i# Eou can actually un#ac/ any seGuence obFect in this 0ay9 3he follo0in$ e8am#le
un#ac/s a strin$)
>>> x = ,string,
>>> a, (, c, d, e, f = x
>>> a
,s,
>>> print a, (, c, d, e, f
s t r i n g
<o0e7er6 un#ac/in$ is most commonly used 0ith tu#les because there are better 0ays to
$et elements out of strin$s and lists9
)sing a tuple to s&ap $alues
'ecause of the s#ecial 0ay assi$nments 0or/ 0ith tu#les6 tu#les are the Pythonic 0ay of
s0a##in$ 7alues9
3his e8am#le s0a#s the 7alues associated 0ith t0o names)
>>> N = ,(la2, (la2, (la2,
>>> r = ,SG1S6B,
>>> N, r
),(la2, (la2, (la2,, ,SG1S6B,*
>>> N, r = r, N
>>> N, r
),SG1S6B,, ,(la2, (la2, (la2,*
-2+
#hapter J, :ivin" into :ictionaries
(verview
&n this cha#ter6 0e discuss dictionaries dicts for short% and sets6 t0o data ty#es that store
multi#le data elements9 <ere are some of the thin$s you can disco7er in this cha#ter)
What dictionaries and sets are and ho0 they differ from seGuences
Some of the most useful dictionary tools and set o#erations
<o0 to con7ert other ty#es of data into dictionaries and sets
Dictionaries are useful in the follo0in$ $eneral and s#ecific circumstances)
When you need random access to elements9
When you 0ant to focus on uniGueness9
'ut consider usin$ sets if you are usin$ 4ython 29" or later9 See ISettin$ 3hem
?#6I later in this cha#ter9
For ma##in$ se7eral s#ellin$s of a term to a #referred s#ellin$9
For usin$ a /ey to find a function to call /no0n as a dispatch table%9
For loo/in$ u# zi# codes or ban/ account numbers9
For 0or/in$ 0ith numeric inde8es 0ith only a fe0 nonconti$uous /eys6 0hen you
donCt need to use slicin$9
3@:<(&:A.
S3?FF
Data that fits the #recedin$ descri#tion is called sparse data9
Eou can also use a list in this case6 but a dict is more
memory1efficient6 0hich often ma/es thin$s faster9
:efinin" the :ictionary
<ereCs ho0 dictionaries dicts% com#are and contrast 0ith other 4ython data ty#es)
:ictionaries are a container type that stores data, like lists and tuples.
:ictionaries are a mapping data type 9 indexed by keys.
A dictionaryCs elements the items it contains% are /ey)7alue #airs9 &n contrast6 lists
and tu#les store data by usin$ numeric inde8es they are se7uence data types% and
su##ort inde8in$ and slicin$9
:ictionaries are mutable.
-2,
3hat means a dictionary can be modified in #lace>you donCt ha7e to create a
co#y of it to modify it as you do 0ith strin$s and tu#les%9
However, a dict's keys must be immutable.
#reating a dictionary
3he sim#lest dictionary is a #air of braces {}%6 0hich defines an empty dict9
D@M@M'@D 3o create a dictionary 0ith some data in it6 follo0 these ste#s)
-9 Type a left brace, a key, a colon, and a value 5the information
you want to associate with the key6, like this,
2" {"parrot"! "Cining for fTords"
3he /ey can be any immutable data ty#e6 such as a strin$ or
inte$er9 Most /eys are strin$s9 3he 7alue can be any data ty#e9
"9 'or each additional element in the dictionary, type a comma to
separate it from the previous value and then type another key,
a colon, and a value,
." {"parrot"! "Cining for fTords", "Colly"! "6x7parrot"
A9
3i# De#eat Ste# 2 for each element you 0ant to add9
To end
the dictionary, type a ri"ht brace, }
&f you added t0o elements6 your dictionary loo/s somethin$ li/e
this)
{"parrot"! "Cining for fTords", "Colly"! "6x7parrot"*
3@:<(&:A.
S3?FF
Hash strin"s or hash brownsC
Some other lan$ua$es ha7e dictionary structuresH they are called
thin$s li/e hashes, associative memories, or associative arrays. We
thin/ dictionaryis a friendlier term.
Dict /eys must be immutable because 4ython associates them 0ith a
uniGue number called a hash9 A hash 7alue or hash% is historically a
number $enerated from a strin$ of te8t9 'ecause the number and
strin$ #air are uniGue6 hashes #re7ent #roblems that mi$ht occur if
t0o different obFects ha7e the same 7alue9
<ashes are used for s#ace1efficient stora$e6 for security and
cry#to$ra#hy6 and so on9 Another benefit of hashin$ is that after the
hash is com#uted6 /eys can be loo/ed u# directly>thereCs no need
for the com#uter to search throu$h a 0hole list of /eys9 &n human
terms6 hashes hel# a com#uter be smart in the same 0ay a human is
-2*
smart 0hen she /no0s to Fum# to the end of a #hysical dictionary to
loo/ u# a term that starts 0ith 8.% &n 4ython6 this feature ma/es
dictionaries 7ery fast9
&n 4ython6 most immutable obFects ha7e a hash6 and the attributes of
all names#ace obFects are stored in dictionaries9
3@:<(&:A.
S3?FF
Some details about dictionary /eys)
=eys must be an immutable data ty#e>for e8am#le6 a strin$6
inte$er6 or tu#le9
&f you use a tu#le as a /ey6 the tu#le cannot ha7e any mutable
elements9
For e8am#le6 you canCt use a tu#le that contains a list9
Eou can ty#e a /ey directly into the dictionary6 or you can
ty#e the name of an obFect6 li/e this)
>>> x = "mydictAey"
>>> mydict = {x! "mydict5al-e"}
0rder in the dict
4ython doesnCt store dictionary elements in any #articular order9 &f you enter elements in
one order6 they may be stored in another essentially random% order inside 4ython9 <ereCs
an e8am#le)
>>> l-m(erTacA = {,sleep,! "Lll nig2t", ,9orA,! "Lll day"}
>>> l-m(erTacA
{,9orA,! ,Lll day,, ,sleep,! ,Lll nig2t,}
3he ste#s to ma/e a sorted list of dictionary /eys de#end on your 4ython 7ersion9
ython 7.A and later
&f you ha7e 4ython 29! or later6 follo0 these ste#s to $et a sorted usually al#hanumeric%
list of the /eys of a dictionary)
-9 Type a list name and = sorted6 followed by the name of your dict in
parentheses, like this,
2" >>> sorted$Aeys = sorted)l-m(erTacA*
"9 To see the sorted list of keys, type the name of the list.
." >>> sorted$Aeys
&" [,sleep,, ,9orA,]
-"0
2arlier versions
&f you ha7e 4ython 29" or earlier6 follo0 these ste#s to $et a sorted list of the /eys of a
dictionary)
-9 Type a name, an = si"n, the name of your dict, and "Aeys)*6 like so,
2" >>> list$of$Aeys = l-m(erTacA"Aeys)*
"9 &se the sort)* method to sort the list by typin" this on the next line,
substitutin" the name you created in !tep . for list$of$Aeys)
." >>> list$of$Aeys"sort)*
A9 Type the list name a"ain to see the sorted list of keys, like this,
<" >>> list$of$Aeys
E" [,sleep,, ,9orA,]
Warnin$ DonCt try to $i7e a name to the result of list$of$Aeys"sort)*9 3he sort)*
method chan$es the list in #lace but returns 1one6 not a list to find out more
about list methods and 0hat they return6 see :ha#ter ,%9
:oodlin" Around with :icts
3his section describes some of the common tas/s you can do 0ith dicts6 such as storin$6
e8tractin$6 addin$6 deletin$6 and searchin$ for /eys and 7alues9
Popular dict operations
3he main o#erations for a dictionary are storin$ a /ey and its associated 7alue and usin$
the /ey to e8tract the 7alue9
!torin" a value
3o store a /ey)7alue #air in an e8istin$ dictionary6 follo0 these ste#s)
-9 Type the name of the dict and a key in s=uare brackets, like this,
2" >>> mydict[,drac-la,]
D@M@M'@D 3he /ey can be ne0 or it can be a /ey thatCs already in the
dictionary9 &f you use an e8istin$ /ey6 the ne0 7alue re#laces the
old one9
"9 Type = and the value you want to store.
Eour line of code mi$ht loo/ li/e this)
>>> mydict[,drac-la,] = ,stoer,
3he follo0in$ e8am#le code creates a dictionary and then creates a ne0 /ey6 ,drac-la,6
and $i7es it the 7alue ,stoAer,)
>>> mydict = {,Ta((er9ocAy,! ,carroll,, ,s2erlocA 2olmes,! ,doyle,}
>>> mydict[,drac-la,] = ,stoAer,
-"-
>>> mydict
{,Ta((er9ocAy,! ,carroll,, ,drac-la,! ,stoAer,, ,s2erlocA 2olmes,!
,doyle,}
<ere6 the e8istin$ /ey ,drac-la, is $i7en a ne0 7alue6 ,oldman,)
>>> mydict[,drac-la,] = ,oldman,
>>> mydict
{,Ta((er9ocAy,! ,carroll,, ,drac-la,! ,oldman,, ,s2erlocA 2olmes,!
,doyle,}
2xtractin" a value
3here are t0o 0ays of e8tractin$ a 7alue based on a dictionary /ey9 3he best o#tion
de#ends on a cou#le of factors)
&f youCre certain the /ey is in the dictionary6 or if you 0ant to raise a Xey6rror
e8ce#tion 0hen the /ey is not in the dictionary6 use the sGuare brac/ets synta89
&f many of the /eys youCre searchin$ for arenCt in the dictionary6 or if you 0ant
4ython to return a 7alue you s#ecify rather than raise an e8ce#tion 0hen /eys
arenCt found6 use the get)* method9
#o raise an exeption on %ailure
&f you 0ant to raise an e8ce#tion if the /ey youCre searchin$ for doesnCt e8ist6 ty#e the
name of the dict follo0ed by a /ey in sGuare brac/ets6 li/e this)
>>> mydict[,Tane eyre,]
0race(acA )most recent call last*!
>ile "#stdin>", line /, in #mod-le>
Xey6rror! ,Tane eyre,
#o return a value you spei%y on %ailure
3o return a default 7alue if the /ey doesnCt e8ist6 follo0 these ste#s)
-9 Type the name of the dict, a dot, get)6 and a key.
Eour code mi$ht loo/ li/e this)
>>> mydict"get),Tane eyre,
29 5(ptional6 Type a comma and a strin" you want to return if the key doesn't
exist.
&f you donCt s#ecify a strin$6 the default 7alue 1one is returned9
>>> mydict"get),Tane eyre,, ,Aey not fo-nd,
-"2
"9 2nd the line by typin" a ri"ht parenthesis, *
Eour com#leted line of code 0ill loo/ somethin$ li/e this if the /ey doesnCt e8ist)
>>> mydict"get),Tane eyre,, ,Aey not fo-nd,*
,Aey not fo-nd,
%inding out about dict ethods
Dictionaries come 0ith built1in methods that allo0 you to #erform the follo0in$ tas/s)
Detrie7e items from dicts by usin$ items)*6 Aeys)*6 and 5al-es)* methods9
?se dicts in loo#s 0ith iterAeys)*6 iter5al-es)*6 and iteritems)* methods9
3est 0hether a /ey is in a dict by usin$ the in /ey0ord9
Delete items from dicts by usin$ the del /ey0ord and the popitem)* and pop)*
methods9
Ma/e shallo0 co#ies of dicts by usin$ the copy)* method9
Add items to dicts by usin$ the setdefa-lt)* method9
/etrievin" items from dicts
3he Aeys)*6 5al-es)*6 and items)* methods are $ood for list1#rocessin$ o#erations
usin$ the contents of dictionaries9 3he pop)* and popitem)* methods retrie7e and delete
thin$s at the same time6 so theyCre $ood for #rocessin$ a tem#orary dictionary you donCt
need to /ee# or for #rocessin$ items one at a time until there arenCt any more9
items&'
3he items)* method retrie7es all the /ey)7alue #airs in a dictionary and returns them as
a list of tu#les9 @ach /ey)7alue #air is a tu#le9
3o retrie7e all the items in a dictionary6 ty#e the name of the dictionary6 a dot6 and
items)*)
>>> mydict = {,(ed price,! ,'%% po-nds,, ,(ed lengt2,! ,2 feet long,}
>>> print mydict"items)*
[),(ed lengt2,, ,2 feet long,*, ),(ed price,, ,'%% po-nds,*]
keys&'
3he Aeys)* method retrie7es all the /eys in the dictionary9 &t returns a list6 as sho0n
here)
>>> print mydict"Aeys)*
[,(ed lengt2,, ,(ed price,]
values&'
-""
3he 5al-es)* method retrie7es all the 7alues in the dictionary9 &t returns a list6 as sho0n
here)
>>> print mydict"5al-es)*
[,2 foot long,, ,'%% po-nds,]
popitem&'
3o retrie7e a sin$le /ey)7alue #air and remo7e it from the dict6 use the popitem)*
method6 li/e this)
>>> mydict"popitem)*
),(ed lengt2,, ,2 foot long,*
3i# Eou canCt use popitem)* to s#ecify 0hich /ey)7alue #air to retrie7e9 &tCs most often
used in a 92ile loo# 0hen you 0ant to #rocess items one at a time until there arenCt
any more but you donCt care 0hat order theyCre #rocessed in9
3@:<(&:A.
S3?FF
3he popitem)* method 0as added in 4ython 29-9
pop&'
3o remo7e a /ey)7alue #air and retrie7e only the 7alue6 use the pop)* method and #ass
the /ey in the #arentheses6 li/e this)
>>> mydict"pop),(ed price,*
,'%% po-nds,
+ettin" loopy with dicts
Eou can use a dictCs /eys6 7alues6 or /ey)7alue #airs in a loo#9 3his is called iterating
over the items9
3@:<(&:A.
S3?FF
3he Aeys)*6 5al-es)*6 and items)* methods also 0or/ in loo#s6
but the iterator methods are more efficient9
<ere are se7eral 0ays of loo#in$ 0ith a dict9
(eys
3o iterate o7er /eys in 4ython 292 and later6 create a for statement 0ith the follo0in$
synta86 substitutin$ the name of your dictionary you can also use a different name for the
iterator6 althou$h Aey is standard for keys%)
for Aey in mydict!
3@:<(&:A.
S3?FF
3he #recedin$ code 0or/s only in 4ython 292 and later9 &t is
eGui7alent to the follo0in$ in earlier 7ersions)
-"!
for Aey in mydict"Aeys)*!
)alues
3o iterate o7er 7alues6 use this synta8 in the for statement)
for 5al-e in mydict"iter5al-es)*!
(ey:value pairs
3o iterate o7er /ey)7alue #airs6 use the follo0in$ synta8)
for Aey, 5al-e in mydict"iteritems)*!
Testin" a dict
<o0 you test 0hether a sin$le /ey a##ears in the dict de#ends on your 7ersion of 4ython9
Eou mi$ht 0ant to do this if6 for e8am#le6 you 0anted to see if a customer number 0as
in your dictionary9%
Python *.* and newer
3o chec/ 0hether a sin$le /ey is in the dictionary6 use the /ey0ord in 0ithin an if
statement6 li/e this)
if ,(ed price, in mydict!
+lder versions
&f you are usin$ 4ython 29- or earlier6 use the 2as$Aey)* method instead)
if mydict"2as$Aey),(ed price,*!
:eletin" items from a dict
Eou can delete a s#ecific /ey)7alue #air from a dict by usin$ the del /ey0ord9 &f youCre
0or/in$ 0ith the dict in a loo# and doin$ somethin$ 0ith the dict data6 usin$ pop)* or
popitem)* is more efficientH they return and delete elements at the same time9
(ey:value
3o delete a /ey)7alue #air from a dictionary6 use the del /ey0ord and $i7e the /ey in
sGuare brac/ets9 For e8am#le)
del mydict[,(ed price,]
Individual elements
-"A
3o delete elements from a dict one at a time in a loo#6 use either of these methods6
de#endin$ on 0hether you 0ant the /ey)7alue #air or Fust the 7alue returned)
popitem)*) 3he popitem)* method returns the key9value pair and deletes the
#air from the dict9
92ile mydict!
Aey, 5al-e = mydict"popitem)*
pop)*) 3he pop)* method returns the value and deletes the /ey)7alue #air from
the dict9
for Aey in mylist!
if Aey in mydict!
5al-e = mydict"pop)Aey*
,ll elements
3o delete all the elements of a dict at once6 use the clear)* method9 3his remo7es all the
elements at once lea7in$ an em#ty dict% and returns 1one)
mydict"clear)*
4akin" a shallow copy of a dict
3o ma/e a shallo0 co#y of a dict6 use the copy)* method9
mycopy = mydict"copy)*
See the sidebar6 I?nderstandin$ shallo0 co#ies6I to find out more about shallo0 co#ies
of dicts9
+et or set, Addin" items to a dict
3he setdefa-lt)* method 4ythonistas call it get or set% is li/e get)*6 0hich e8tracts a
7alue by usin$ a /ey9 'ut setdefa-lt)* $oes one ste# further) &f the /ey is not found6 it
adds the /ey to the dict9
&nderstandin" shallow copies
3o understand shallo0 co#ies6 you need to /no0 somethin$ about ho0 4ython stores
information9 When you $i7e a name to an obFect such as a list that stores some 7alues6
4ython creates a reference the name% to that obFect the stored 7alues%9
>>> mylist = [,my,, ,list,]
(o06 say you create a dictionary that contains this list)
>>> mydict = {,myn-m(er,! /, ,alist,! mylist, ,myt-ple,! )/,2*}
-"6
&f you loo/ at the dict6 it loo/s li/e the 7alues stored in the list are stored in the dict6 too)
>>> mydict
{,myt-ple,! )/, 2*, ,alist,! [,my,, ,list,], ,myn-m(er,! /}
'ut in fact the dictionary is not storin$ the actual 7alues stored in the list9 &t is only
storin$ the reference to the list9
3hat means6 if you chan$e the listS
>>> mylist"append),c2anged,*
S your dict also chan$es9
>>> mydict
{,myt-ple,! )/, 2*, ,alist,! [,my,, ,list,, ,c2anged,], ,myn-m(er,! /}
When you ma/e a shallo0 co#y of somethin$6 you co#y only the references6 not the
actual 7alues that the references #oint to9 So if you ma/e a co#y of a dict by usin$ the
copy)* method6 the co#y reflects any chan$es made to the list that the dictionary is
referrin$ to9
>>> mycopy = mydict"copy)*
>>> mycopy
{,myt-ple,! )/, 2*, ,alist,! [,my,, ,list,, ,c2anged,], ,myn-m(er,! /}
3o ma/e a co#y of the actual 7alues so that these chan$es donCt occur6 im#ort the copy
module and use the deepcopy)* function9 See :ha#ter ,9
3he standard use for setdefa-lt)* is for addin$ a list or other mutable data ty#e% to the
dict and a##endin$ items to the list9 <ereCs ho0 the synta8 loo/s)
mydict"setdefa-lt)Aey, []*"append)5al-e*
<ereCs 0hat ha##ens)
-9 &f the /ey isnCt in the dict6 4ython adds the /ey and uses an em#ty list as the 7alue9
3hen it a##ends the 7alue you s#ecified to the list9
29 &f the /ey is in the dict6 4ython tries to a##end your 7alue to that /eyCs 7alue9 For
this to 0or/6 the 7alue must be a list because the append)* method 0or/s only
0ith lists9%
D@M@M'@D &f you donCt s#ecify a 7alue6 the 7alue 1one is used9
3he follo0in$ code creates a /ey6 /36 and #airs it 0ith a 7alue of an em#ty list9 3hen it
a##ends the strin$ ,complaints, to the list)
-"+
>>> rooms = {,/2L,! [,arg-ment clinic,], /2! [,a(-se,]}
>>> rooms"setdefa-lt)/3, []*"append),complaints,*
>>> rooms
{,/2L,! [,arg-ment clinic,], /2! [,a(-se,], /3! [,complaints,]}
3he follo0in$ code a##ends the strin$ ,don,t (ot2er, to the list that has the /ey /39
>>> rooms"setdefa-lt)/3, []*"append)"don,t (ot2er"*
>>> rooms
{,/2L,! [,arg-ment clinic,], /2! [,a(-se,], /3! [,complaints,, "don,t
(ot2er"]}
3he follo0in$ synta8 does the same thin$ 0ith a set9
mydict"setdefa-lt)Aey, sets"@et)**"add)5al-e*
Buildin" :ictionaries
&n 4ython6 most com#ound data obFects are created by #rocessin$ data in a loo# structure6
so youCll often build lists6 dictionaries6 and so on by 0ritin$ loo#s9 Follo0in$ are some
loo#y and non1loo#y 0ays of buildin$ dicts9
#on$erting other data types into a dict
Eou ha7e se7eral 0ays to con7ert other data ty#es into dicts9 3his section sho0s the
dict)* constructor6 the fromAeys)* method6 and a for loo#9
#onvertin" a list of tuples
3o con7ert a list of t0o1item tu#les or any iterable containin$ t0o1item iterables% to a
dict6 #ass the list to the dict)* method li/e this)
>>> list$of$t-ples = [),spam,, /*, ),eggs,, ,green,*]
>>> dict)list$of$t-ples*
{,eggs,! ,green,, ,spam,! /}
#onvertin" a se=uence
3o create a dict from a list or tu#le and an o#tional ma##in$ 7alue6 use the fromAeys)*
method9 3his 0or/s in 4ython 29" or later9% 3he items in the seGuence become the /eys6
and the ma##in$ 7alue 1one by default% is used for the 7alues9 &n this e8am#le6 each item
in the list is turned into a /ey and $i7en the 7alue ,(reaAfast,)
>>> mylist = [,spam,, ,eggs,, ,orange T-ice,]
>>> print dict"fromAeys)mylist, "(reaAfast"*
{,eggs,! ,(reaAfast,, ,orange T-ice,! ,(reaAfast,, ,spam,! ,(reaAfast,}
-",
Eou can do the same thin$ by usin$ the dict)* constructor and a list comprehension a
line of code that creates a list from a ran$e of items%9
>>> dict)[)i, ,(reaAfast,* for i in mylist]*
{,eggs,! ,(reaAfast,, ,orange T-ice,! ,(reaAfast,, ,spam,! ,(reaAfast,}
3i# &f you 0onCt be 0or/in$ 0ith 7alues later6 consider creatin$ a set rather than a dict9 A
set has only /eys6 no 7alues9 3o create a set from a list or tu#le6 Fust ty#e set)* and
include the list or tu#le in #arentheses9 For more information6 see ISettin$ 3hem
?#6I later in this cha#ter9
3o create a dict from a t0o1element iterable6 you can also use a list com#rehension9 3his
code builds a dict of #o0ers of 2)
>>> dict)[)x, x;;2* for x in )2, ., <*]*
{2! ., .! /<, <! 3<}
Eou can $et the lo0do0n on list com#rehensions in :ha#ter -69
Buildin" a dict with keyword ar"uments
&n 4ython 29" and later6 you can #ass /ey0ord ar$uments ;;A9args% to the dict)*
method and build a dictionary6 li/e this)
>>> sacA = dict)cats=E, Aits=E*
>>> sacA
{,Aits,! E, ,cats,! E}
For more about ;;A9args6 see :ha#ter --9
Buildin" a dict out of another dict by usin" a for loop
3o $o throu$h a dict 0hile buildin$ a second one6 use a for loo# and assi$n each #air in
the old dict to the ne0 dict6 li/e this)
>>> mydict = {,(ed price,! ,'%% po-nds,, ,(ed lengt2,! ,2 foot long,}
>>> ne9dict = {}
>>> for Aey in mydict!
""" 5al-e = mydict[Aey]
""" ne9dict[Aey] = 5al-e
"""
>>> ne9dict
{,(ed lengt2,! ,2 foot long,, ,(ed price,! ,'%% po-nds,}
3@:<(&:A.
S3?FF
&n 4ython 29- and earlier6 the in /ey0ord doesnCt 0or/ 0ith a
dictionary obFectH you need to use it 0ith a dictionary method9 So to
0rite the abo7e loo#6 you could use this synta8)
for Aey, 5al-e in mydict"items)*!
-"*
)pdating a dictionary
3o re#lace the 7alues in a dictionary 0ith ne0 7alues6 use the -pdate)* method9 3he ne0
/ey)7alue #airs can come from either another dictionary or other ma##in$ obFect% or an
iterable of key9value pairs such as a list of t0o1 item tu#les%9
3@:<(&:A.
S3?FF
3he iterable /ey)7alue% o#tion reGuires 4ython 29! or later9
Eou can also re#lace 7alues by usin$ /ey0ord ar$uments6 as in this e8am#le)
>>> rooms = {,/2L,! ,arg-ment clinic,, /2! ,a(-se,}
>>> rooms"-pdate)L/.=,2it on t2e 2ead lessons,*
>>> rooms
{,/2L,! ,arg-ment clinic,, /2! ,a(-se,, ,L/.,! ,2it on t2e 2ead
lessons,}
<ereCs code that u#dates one dict by usin$ /ey)7alue #airs from a second dict9 &f a /ey
a##ears in both dicts6 the second dictCs 7alues o7er0rite the first dictCs 7alues9 3he second
dict is not modified9
>>> rooms2 = {,L/.,! ,canceled (y >lying >ox,}
>>> rooms"-pdate)rooms2*
>>> rooms
{,/2L,! ,arg-ment clinic,, /2! ,a(-se,, ,L/.,! ,canceled (y >lying >ox,}
*hen (nly a :ict *ill :o
3his section $i7es brief e8am#les of some real10orld uses for dicts6 includin$ a $ame that
retrie7es 7alues associated 0ith /eys6 a cache for storin$ freGuently used items6 and a
0ay of findin$ du#licate /eys9
Storing and retrie$ing $alues
A dict is useful for storin$ and retrie7in$ immutable obFects and corres#ondin$ 7alues6
li/e #roduct #rices6 for e8am#le9 <ereCs a small $ame that uses a dict for this #ur#ose)
c2oice = ra9$inp-t)"H2oose scissors, rocA, or paper! "*
res-lts = {"scissors"! "c-t paper", "rocA"! "cr-s2es scissors",
"paper"! "co5ers
rocA"}
print c2oice, res-lts[c2oice]
<ereCs one round of the $ame)
H2oose scissors, rocA, or paper! paper
paper co5ers rocA
-!0
Eou can also use if8elif8else statements for this #ur#ose see :ha#ter -0%6 but a
dictionary is often better because itCs built 0hen a #ro$ram first runs 0hich is more
efficient% and itCs easier to read if a lot of /ey)7alue #airs are in7ol7ed9 &tCs also easier to
maintain and modify9
)sing a dict as a cache
&f youCre usin$ a for loo# to #rocess some items6 a dict can store the results for reuse9
3his ty#e of stora$e is called a cache. 3here are t0o 0ays to 0rite cachin$ code9 3he one
you use de#ends on 0hether youCre #rocessin$ a lot of different items or mostly multi#le
co#ies of similar items9
3i# 3he follo0in$ e8am#les refer to a function called getWal-e)*9 3his isnCt a function
built in to 4ythonH youCll ha7e to 0rite it yourself9
:ifferent items
&f many uniGue items #robably 0onCt be in the dict6 0rite code li/e this)
res-lt = []
cac2e = {}
for Aey in mylist!
if Aey in cac2e!
res-lt"append)cac2e[Aey]*
else!
5al-e = getWal-e)Aey*
cac2e[Aey] = 5al-e
res-lt"append)5al-e*
3i# We use a cache because the getWal-e)* function is e8#ensi7e that is6 it ta/es a lot
of time%9 <o0e7er6 you should a7oid o#timizin$ code 0hen you donCt need to>it
ma/es code harder to read9
!imilar items
3he follo0in$ code is efficient if youCre #rocessin$ many similar items6 but itCs harder to
read)
res-lt = []
cac2e = {}
for Aey in mylist!
if Aey in cac2e!
res-lt"append)cac2e[Aey]*
else!
+ Ldd to cac2e and append to res-lt
res-lt"append)cac2e"setdefa-lt)Aey, getWal-e)Aey***
3i# &f you thin/ that the /eys 0ill almost al0ays be in the dict6 the follo0in$ is e7en
more efficient9 3hatCs because try bloc/s ta/e 7ery little time if an e8ce#tion isnCt
raised6 but e8ce#tions ta/e a lot of time to #rocess)
-!-
res-lt = []
cac2e = {}
for Aey in mylist!
try!
res-lt"append)cac2e[Aey]*
except Xey6rror!
+ Ldd to cac2e and append to res-lt
res-lt"append)cac2e"setdefa-lt)Aey, getWal-e)Aey***
1ealing &ith duplicate keys
'ecause setdefa-lt)* returns the 7alue for a /ey6 itCs easy and efficient to 0rite a loo#
that con7erts a list of /ey)7alue tu#les into a dict in 0hich each /ey contains a list of
7alues9 3his is handy 0hen you e8#ect /ey du#lications9
3his code #rocesses a list of tu#les and finds that t0o 7alues share a /ey)
>>> 3 = [)/, ,pear tree,*, )&, ,golden rings,*, )/, ,partridge,*]
>>> d = {}
>>> for Aey,5al-e in 3!
""" d"setdefa-lt)Aey, []*"append)5al-e*
"""
>>> d
{/! [,pear tree,, ,partridge,], &! [,golden rings,]}
!ettin" Them &p
,ets are a data ty#e that su##orts mathematical o#erations li/e union6 intersection6
difference6 and symmetric difference9
*hat sets are and aren"t
<ereCs ho0 sets fit in 0ith the rest of 4ythonCs data ty#es)
3he set)* ty#e is built into 4ython 29! and later9 &f youCre usin$ 4ython 29"6 you
must im#ort the sets module before usin$ sets9 3he data ty#e in the sets module
is called @et)* 0ith a ca#ital ,%6 not set)*9
.i/e a dict6 a set is an unordered collection of elements9
?nli/e a dict6 a set contains only /eys that is6 uniGue6 immutable obFects%9 A set
does not ha7e 7alues6 and it is not a ma##in$ ty#e9
?nli/e a seGuence6 the elements in sets arenCt stored in any #articular order6 so
sets donCt su##ort inde8in$ or slicin$9
&n seGuences6 an element can occur multi#le times6 but a set contains only uniGue
elements6 no du#licates9
-!2
De$ular sets are mutable6 and you canCt use one as an element of another set or as
a dictionary /ey9 'ut see the section6 I&mmutable or frozen sets6I later in this
cha#ter9%
<ereCs an e8am#le of a set based on a list that contains some du#licate elements9 3he set
contains no du#licates9
>>> (asAet = [,apple,, ,orange,, ,apple,, ,pear,, ,orange,, ,(anana,]
>>> fr-its = set)(asAet*
>>> fr-its
set)[,orange,, ,pear,, ,apple,, ,(anana,]*
(ebership testing &ith sets
A set is useful for com#arin$ indi7idual items a$ainst a $rou# of elements9 For e8am#le6
if you ha7e a set that contains all states on the Atlantic coast6 you can chec/ 0hether a
user1entered state is on the Atlantic9
#heckin" elements
3o test 0hether an element is in a set6 use the in /ey0ord6 li/e this)
>>> ,aine, in atlantic$states
0r-e
>>> ,Jregon, in atlantic$states
>alse
#heckin" subsets and supersets
3o test 0hether one set is a subset or su#erset of another6 use the a##ro#riate method
listed belo09 'oth return 0r-e or >alse9
a"iss-(set)(*) 3ests 0hether all the elements of set a are also in set (9
a"iss-perset)(*) 3ests 0hether set a contains all the elements in set (9
%inding set eleents
Set operators and methods can be used to combine sets and find their intersections and
their uniGue elements9
3@:<(&:A.
S3?FF
Set o#erations ha7e corres#ondin$ methods9 &n some cases6 usin$ the
method synta8 rather than the o#erator creates easier1to1read code9
3he follo0in$ o#erations are the most commonly used9
-!"
:ifference
3he difference o#eration com#ares t0o sets and returns a set containin$ the elements that
are in the first set but not in the second set9 3he difference o#erator 7 uses this synta8)
a 7 (
3he difference method uses this synta8)
a"difference)(*
3he follo0in$ e8am#le finds the set of round thin$s that arenCt #in/)
>>> ro-ndt2ings = [,orange,, ,g-m(all,, ,egg,, ,tire,]
>>> pinAt2ings = [,lipsticA,, ,g-m(all,, ,(a(y (lanAet,]
>>> a = set)ro-ndt2ings*
>>> N = set)pinAt2ings*
>>> a 7 N
set)[,orange,, ,egg,, ,tire,]*
&nion
3he [ o#erator returns the union of the sets all the elements from both sets%9 3he union
method uses this synta8)
a"-nion)(*
3his e8am#le finds the set of all round thin$s and all #in/ thin$s)
>>> a [ N
set)[,lipsticA,, ,tire,, ,orange,, ,g-m(all,, ,egg,, ,(a(y (lanAet,]*
Intersection
3he ^ o#erator returns the intersection of the sets the elements that are #resent in both
sets%9 3he intersection method uses this synta8)
a"intersection)(*
3his e8am#le finds all the round #in/ thin$s)
>>> a ^ N
set)[,g-m(all,]*
!ymmetric difference
3he ? o#erator returns the symmetric difference of the sets>elements in
-!!
@8actly one set
#ot both sets
3he symmetric difference method uses this synta8)
a"symmetric$difference)(*
3his e8am#le finds all the thin$s that are either round or #in/ but not both)
>>> a ? N
set)[,orange,, ,egg,, ,lipsticA,, ,tire,, ,(a(y (lanAet,]*
Iutable or fro6en sets
4ython 29! has a built1in data ty#e called froQenset9 &tCs similar to set6 but itCs
immutable that is6 after youC7e created one6 you canCt chan$e it%9 'ecause a froQenset is
immutable6 it can be used as a dict /ey or as an element in a set9
&n the follo0in$ e8am#le6 a froQenset instance is used to associate a three1item set /ey
0ith the 7alue 1one)
>>> mylist = [,foo,, ,(ar,, ,spam,]
>>> s = froQenset)mylist*
>>> s
froQenset)[,foo,, ,(ar,, ,spam,]*
>>> d = {s! 1one}
>>> d
{froQenset)[,foo,, ,(ar,, ,spam,]*! 1one}
&f you try to use a re$ular set as a dict /ey6 4ython builds an immutable set from your set
and uses that set as the dict /ey instead9
3@:<(&:A.
S3?FF
4ython 29" has a similar subclass of sets called Gmm-ta(le@et9 &t is
a7ailable if you im#ort the sets module9
-!A
art III, !tructures
#hapter -ist
:ha#ter -0) Stayin$ in :ontrol
:ha#ter --) Fun 0ith Functions
:ha#ter -2) 'uildin$ A##lications 0ith Modules and 4ac/a$es
:ha#ter -") ;ettin$ :lassy
:ha#ter -!) &ntroducin$ (e01Style :lasses
:ha#ter -A) Feelin$ @8ce#tional
:ha#ter -6) 3ac/lin$ Some Ad7anced Features
3he A
th
Wa7e 'y Dich 3ennant
IWeCre here to clean the code9I
In this part
3his #art co7ers 4ythonCs code bloc/s6 the chun/s of code that mani#ulate data and return
results9
&f youC7e e7er seen a flo0 chart6 youCll be comfortable 0ith :ha#ter -06 0hich describes
if statements and loo#sS loo#sS loo#sS9
-!6
:ha#ter -- ta/es you inside the ma$ic bo8es called functions. EouCll disco7er ho0 to #ut
stuff into them and ho0 to $et stuff out of them9
Modules and #ac/a$es are the .e$o bloc/s of 4ython #ro$rams9 :ha#ter -2 sho0s you
ho0 to build and 0or/ 0ith both module files6 0hich store chun/s of code that 0or/
to$ether6 and #ac/a$es6 0hich are directories that contain related modules9
.uc/y :ha#ter -" inducts you into the tem#le of obFect1oriented #ro$rammin$9 :lasses6
li/e factories or tem#lates6 create other obFects that do the actual 0or/9 After you read
this cha#ter6 youCll understand 0hat itCs li/e to be a ca#italist baron 0ho controls the
means of #roduction9
:lasses are such an im#ortant conce#t that 0e couldnCt $et a0ay 0ith Fust one cha#ter
about them9 4ython 29A introduced ne$-style classes. Eou donCt ha7e to use them6 but if
you 0ant to6 :ha#ter -! is 0here they are9
What do you do 0hen somethin$ 0eird ha##ens in your 4ython #ro$ram2 :ha#ter -A
offers ad7ice9 <ere6 you find out ho0 to antici#ate and handle e8ce#tions in your
#ro$rams9
&f youC7e de7oured the rest of the cha#ters in this #art and youCre still hun$ry6 turn to
:ha#ter -69 <ere6 0e introduce some ad7anced features of 4ython #ro$rammin$9 @7en if
you donCt 0ant to use these features yourself yet6 youCll a##reciate bein$ able to reco$nize
them in other #eo#leCs #ro$rams9
-!+
#hapter .K, !tayin" in #ontrol
(verview
With 4ython6 you use control structures to determine 0hether a chun/ of code runs or
ho0 many times to run that code9
3@:<(&:A.
S3?FF
3his /ind of code is also called a compound statement. A com#ound
statement is a /ind of code block lines of code that #erform an
action%9
&n this cha#ter6 0e sho0 you
3he condition and com#arison o#erators that control structures use
3ests you can do on data to decide 0hether and ho0 code should run
<o0 to use if bloc/s to test se7eral s#ecific conditions
When and ho0 to use for and 92ile loo#s to e8ecute code re#eatedly
hin"s to 8now about #ontrol !tructures
3his section describes some $eneral thin$s you need to /no0 about ho0 control
structures 0or/9 &t a##lies to all the structures in this cha#ter9
A control structure starts 0ith a /ey0ord>if6 for6 try6 92ile6 or 9it29 3he first
line also contains an e8#ression and ends 0ith a colon9
Eou can nest control structures9 So an if statement can be nested inside a for
loo#6 and 7ice 7ersa9 Eou can also nest for in for and if in if9%
4ython uses indentation to $rou# statements to$ether9
@ach line 0ithin a code bloc/ must be indented by the same amount>the
con7ention is four s#aces for the first bloc/6 ei$ht s#aces for any bloc/ nested in
the first bloc/6 and so on9
When you enter a com#ound statement a statement 0ith more than one line% in
interacti7e mode see :ha#ter 2 for instructions%6 you need to end it 0ith a blan/
line a Deturn%9 3his tells the #arser that you ha7e ty#ed the last line of the bloc/9
-!,
All about #onditions and #omparisons
:ontrol structures use conditions and com#arisons to tell 4ython 0hen to carry out
actions9 A condition tests 0hether some e8#ression is true or false9 A comparison tests
0hether one 7alue is lar$er than6 eGual to6 or smaller than another9 3his section co7ers the
truth 7alues of obFects and the o#erators that you use for conditions and com#arisons9
The $alue of truth
&n 4ython6 e7ery obFect has a truth value>a 'oolean 7alue of true or false9
D@M@M'@D &n 4ython6 true and false donCt 0or/ Guite the same 0ay they do in the rest
of life9 For e8am#le6 any strin$ e8ce#t an em#ty strin$ e7aluates as true9
So you could tell 4ython ,G am a (illionaire,6 and it 0ould be true5
True and 'alse aren't true and false
4ython 2929- and later ha7e built1in 'oolean data ty#es 0r-e and >alse6 0hich are
associated 0ith the (ool)* built1in function9 Mther functions and methods also return
'oolean results6 such as isdigit)*9% :om#arison e8#ressions6 such as x # y6 also return
0r-e and >alse9
<istory $ee/s may be interested to /no0 that #re7ious 7ersions of 4ython used / and %
rather than 0r-e and >alse because thatCs ho0 it 0or/s in the : #ro$rammin$ lan$ua$e9
3he odd result of this is that the 0r-e and >alse data ty#es are inte$ers)
>>> / : 0r-e
2
&tCs im#ortant to remember that the 'oolean data types are not the same thin$ as the truth
7alues of obFects returned by 'oolean operators. "hen you check the truth value of
ob-ects by usin$ 'oolean o#erators for e8am#le6 by usin$ x and y%6 you are chec/in$
0hether the obFects are em#ty9 3he 'oolean o#erators return a 4ython obFect for
e8am#le6 in x and y6 the obFect x or the obFect y is returned%H they donCt necessarily
return 0r-e or >alse9
'alse ob>ects
All 4ython obFects e7aluate as true e8ce#t for null, empty, and 2ero obFects9 3he follo0in$
obFects e7aluate as false)
>alse one of the t0o 'oolean obFects%
Qero %% for any numerical data ty#e%
-!*
@m#ty strin$ ,,
@m#ty list []
@m#ty tu#le )*
@m#ty dictionary {}
@m#ty set set)*
1one a s#ecial 4ython obFect that stands for Ino 7alueI%
Testin" an ob>ect for truth
3estin$ 0hether an obFect is true or false is sim#le9 Just use an if bloc/)
-9 Type if followed by a literal or the name of an ob>ect. Then type a colon and
press /eturn,
2" >>> if "a"!
"9 Type four spaces, and then type somethin" for ython to do. ress /eturn
twice when you're finished.
." >>> if "a"!
&" """ print "yes!"
<" """
E" yes!
4ython #erforms the action only 0hen the obFect e7aluates as true9 3his test does not
assume that the obFect is any #articular data ty#e9 'ecause youCre not com#arin$ the
obFect a$ainst anythin$ else6 you can thin/ of it as Ilettin$ the obFect e7aluate itself9I
3@:<(&:A.
S3?FF
if name$of$o(Tect! is s2ort for if (ool)name$of$o(Tect*
== 0r-e"
Testin" the elements of a se=uence ob>ect
&n 4ython 29A6 ne0 built1in functions allo0 you to test the truth 7alues of multi#le
elements of a seGuence obFect such as a list% or other iterables at once9
all&'
3he all)* function returns 0r-e if all the elements are true9 &t also returns 0r-e if the
seGuence obFect is em#ty9 For e8am#le)
>>> mylist = [/, 2, 3]
>>> all)mylist*
0r-e
>>> myot2erlist = [%, 2, 3]
>>> all)myot2erlist*
>alse
>>> myemptylist = []
>>> all)myemptylist*
0r-e
-A0
any&'
3he any)* function returns 0r-e if at least one element is true the obFect must not be
em#ty%9 For e8am#le)
>>> any)mylist*
0r-e
>>> any)myot2erlist*
0r-e
>>> any)myemptylist*
>alse
Boolean operators
Eou use 'oolean o#erators to test the truth 7alues of obFects to test 0hether obFects are
em#ty%9 Eou can test more than one obFect at once6 and you can set u# conditions that
de#end on the truth 7alue of one obFect or the truth 7alue of all the obFects youCre testin$9
D@M@M'@D 3he 'oolean o#erators are e7aluated in this #riority order)
-9 not
29 and
"9 or
3he o#erators and and or are short-circuit operators. 4ython e7aluates e8#ressions 0ith
these o#erators from left to ri$ht and sto#s as soon as the outcome is determined9 4ython
returns the obFect it e7aluated last and s/i#s the rest of the e8#ression9
All about or
3he or o#erator sto#s e7aluatin$ as soon as it finds somethin$ thatCs true9 &n the follo0in$
e8am#le6 the first item is true6 so #rocessin$ sto#s there and 4ython returns the first 7alue9
>>> 3 or . or &
3
&n the ne8t e8am#le6 the first item is false6 but the second item is true6 so #rocessin$ sto#s
and 4ython returns the second 7alue)
>>> % or / or 2
/
&f no item e7aluates as true6 the last false% item is returned9
>>> % or [] or ,,
,,
-A-
2mbrace the power of and
3he and o#erator reGuires all the conditions to be met for the e8#ression to be true9
@7aluation sto#s either 0hen an obFect tests false or 0hen all items ha7e been tested9
3i# 3he follo0in$ e8am#les sho0 the results of t0o e8#ressions usin$ and)
3his e8am#le tests all the items and returns the last 7alue)
>>> 3 and . and &
&
3his e8am#le sto#s after testin$ the second item %% because it tests false)
>>> 3 and % and &
%
#oparison operators
:om#arison o#erators test 0hether somethin$ is eGual to andLor $reater than or less than
somethin$ else9 3able -01- lists the com#arison o#erators9
3able -01-) 4ythonCs :om#arison M#erators
M#en table as s#readsheet
(perator &sa"e 4eanin"
# x # y
x is less than y
#= x #= y
x is less than or eGual to y
> x > y
x is $reater than y
>= x >= y
x is $reater than or eGual to y
!=6 #> x != y6 x #> y x is not eGual to y
== x == y
x is eGual to y
D@M@M'@D All com#arison o#erators ha7e the same #riority9 Statements 0ith multi#le
com#arison o#erators are e7aluated from left to ri$ht9
The difference between e=uals and e=uals e=uals
&n 4ython6 = the assi$nment o#erator% $i7es a name to an obFect9 3he = o#erator is #art of
a statement9 &f you 0ant to test 0hether somethin$ is eGual to somethin$ else6 you must
use the == o#erator9
Is you is or is you ain't my babyC
3here are t0o s#ecial com#arisons) is and is not9 3hese donCt test the 7alues of x and yH
they test the identities of x and y>that is6 0hether x and y are bound to the same obFect9
Warnin$ 3o a7oid #roblems6 ma/e a habit of usin$ == and != to com#are obFectsC 7alues
-A2
rather than usin$ is and is not to com#are their identities9 ?sually6 you use is
and is not only 0hen you 0ant to test 0hether somethin$ has the s#ecial 7alue
1one9 3here is only one 1one9%
+et in<
3he com#arison o#erators in and not in chec/ 0hether a 7alue occurs in a seGuence
data ty#e a list6 tu#le6 or strin$%6 a dict6 or a set9
3he follo0in$ e8am#le chec/s for the te8t ,fox, in a set)
>>> data = set)["t2e", "N-icA", "(ro9n", "fox"]*
>>> if "fox" in data!
""" print "Hall o-t t2e 2o-nds!"
"""
Hall o-t t2e 2o-nds!
3i# &f your #ro$ram 0or/s 0ith lots of data6 itCs a ;ood &dea to use dicts or sets to store
the data9 Searchin$ dicts and sets is faster than searchin$ seGuences9
#omparatively speakin"
:om#arisons can be lin/ed in a chain9 3he follo0 e8am#le tests three 7alues9 'oth tests
must be true for the com#arison to be true9
>>> if / # 2 # 3!
""" print "yes!"
"""
yes!
3@:<(&:A.
S3?FF
.in/ed com#arisons can be hard to read9 &n some cases6 combinin$
com#arisons 0ith 'oolean o#erators and6 or6 not% and #arentheses
0ill im#ro7e readability of your code9 :om#are these #airs of
e8#ressions and decide 0hich are easier to read)
if a # ( and ( # c!
if a # ( # c!
if a # ( > c # d!
if )a # (* and )( > c* and )c # d*!
3he 'oolean o#erators ha7e lo0er #riorities than com#arison o#erators>that is6 they are
e7aluated after com#arison o#erators9 Eou can use #arentheses to ma/e sure com#arisons
are e7aluated in the order you 0ant9
'eelin" Iffy
An if statement is an instruction that 4ython carries out only if a condition is true9 Eou
often use a comparison operator to test the condition in an if statement9 3he condition
must be either true or false no $ray areas here5%9 3his is referred to as 0oolean
com#arison9
-A"
3i# Eou can translate an if statement into @n$lish as follo0s) I&f this is true6 then carry
out the instructions belo09I
*riting an if stateent
3o 0rite an if statement6 follo0 these ste#s)
-9 Type if6 a condition, and a colon, like so,
2" if 9eat2er == "raining"!
"9 (n the next line, type an instruction about what to do if the condition is true.
." if 9eat2er == "raining"!
&" (ring$-m(rella = "yes"
'e sure to indent this line and any other lines of the bloc/% four s#aces9
Warnin$ Eou mi$ht see sin$le1line code bloc/s combined on one line li/e this)
if 9eat2er == "raining"! (ring$-m(rella = "yes"
We donCt recommend usin$ this style in your o0n code9 &tCs harder to read6 and #robably
0onCt be allo0ed in 4ython "90 and later9
Adding another condition to an if block
&f you 0ant to test se7eral conditions6 you can include additional code bloc/s to an if
statement9 3hese bloc/s be$in 0ith the /ey0ord elif 0hich is short for Ielse ifI%9 3he
elif statement says6 essentially6 IMther0ise6 if this other thin$ is true6 then carry out this
other instruction9I
Warnin$ :rucial code that al0ays has to run doesnCt belon$ in if or elif code9 4ython
reads if bloc/s if and elif statements% in order9 As soon as it finds a true
condition6 it sto#s e7aluatin$9 3he rest of the bloc/ is not e8ecuted9
3o 0rite an if bloc/ that includes an elif statement6 follo0 these ste#s)
-9 *rite an if statement.
3i# ?se the instructions in the IWritin$ an if statementI section6 earlier in this
cha#ter9 Eour code mi$ht loo/ somethin$ li/e this)
if 9eat2er == "raining"!
(ring$-m(rella = "yes"
29 -ithout indenting. on the next line, type elif, a condition, and a colon 5:6.
3" if 9eat2er == "raining"!
." (ring$-m(rella = "yes"
&" elif 9eat2er == "9indy"!
-A!
3i# &f you are ty#in$ in a #ro$ram that automatically formats 4ython code6 you
mi$ht ha7e to #ress 'ac/s#ace or Delete one or more times to ma/e sure that
this line is not indented9 3his is called dedenting.
69 (n the next line, indent four spaces and type an instruction, like so,
E" if 9eat2er == "raining"!
'" (ring$-m(rella = "yes"
=" elif 9eat2er == "9indy"!
/%"(ring$TacAet = "yes"
Eou can ty#e more instructions9 'e sure to indent any additional instruction lines
four s#aces9
Adding an else stateent to an if block
An else statement al0ays comes last in an if bloc/9 &t means I&f none of the #recedin$
statements 0as true6 then carry out this final instruction9I
Warnin$ DonCt #ut critical code in an else statement9 3he else code $ets e8ecuted only
if none of the other statements in the if bloc/ is true9
3o 0rite an if bloc/ that includes an else statement6 follo0 these ste#s)
-9 *rite an if statement.
3i# &f you ha7e only one condition to test6 s/i# to Ste# "9
29 If you have additional conditions to test, write one or more elif statements.
?se the instructions in the section IAddin$ another condition to an if bloc/6I
earlier in this cha#ter9
"9 (n the next line, type else!9
3his te8t should line u# 0ith if9 Eou mi$ht ha7e to dedent if youCre ty#in$ in a
#ro$ram that formats 4ython code automatically9
!9 +o to the next line, indent four spaces, and type at least one instruction.
&" go$o-t = "no9! 92at are yo- 9aiting forV"
'e sure to indent any additional instruction lines four s#aces9
Eour com#leted code bloc/ mi$ht loo/ li/e this)
if 9eat2er == "raining"!
(ring$-m(rella = "yes"
elif 9eat2er == "9indy"!
(ring$TacAet = "yes"
else!
go$o-t = "no9! 92at are yo- 9aiting forV"
-AA
#obining tests
Eou donCt al0ays ha7e to 0rite elif statements 0hen you 0ant to test multi#le
conditions9 &f you 0ant to #erform the same action in more than one condition6 you can
combine the conditions in a sin$le if or elif statement9
3he code sni##ets in .istin$s -01- and -012 both test for the same conditions6 but the
e8am#le combinin$ multi#le conditions in the if statement is easier to read)
.istin$ -01-) elif bloc/
if age # /'!
print "Oisco-nt rate"
elif age > <&!
print "Oisco-nt rate"
else!
print "Ld-lt rate"
.istin$ -012) 30o conditions in an if statement
if )age # /'* or )age > <&*!
print "Oisco-nt rate"
else!
print "Ld-lt rate"
!tayin" in the -oop
:om#uters are $ood at doin$ thin$s o7er and o7er a$ain9 <umans tend to find re#etitious
tas/s borin$9 3hatCs 0hy 0e #ro$ram 0ith loops9 .oo#s are code bloc/s that tirelessly
re#eat until the tas/ is finished9 4ython has for loo#s and 92ile loo#s9 'oth /inds of
loo#s ha7e these thin$s in common)
3hey start 0ith a /ey0ord for or 92ile%6 follo0ed by an e8#ression and endin$
0ith a colon9
3hey are code bloc/s6 and the code inside them is indented9
3@:<(&:A.
S3?FF
3he ri$ht loo# for your tas/ de#ends on se7eral factors9 See the
section I:hoosin$ Eour .oo#I later in this cha#ter9
%or a Good Tie:
A for loo# is a 0ay to re#eat some instructions a code block% a number of times9 &n this
section6 0e e8#lain the 0or/in$s of the for loo# and discuss iterables6 0hich are obFects
you can use to $enerate a for loo#9
How for works
<ereCs a basic for loo#)
-A6
for myc2ar in "2ello!"!
print "t2e ascii 5al-e of", myc2ar, "is", ord)myc2ar*
3he #recedin$ e8am#le sho0s all the im#ortant features of a for loo#)
3he /ey0ord for
A name that holds a 7alue each time throu$h the loo# this name is sometimes
called the loop target%
3he /ey0ord in
3@:<(&:A.
S3?FF
(ote that in used in a for loo# 0or/s differently from the
'oolean o#erator in see the I;et in5I section earlier in this
cha#ter%9 <ere6 in is used to se#arate the loo# tar$et from the
iterable9
An iterable a seGuence or iterator obFect%>in this case6 the strin$ ,2ello!,
A colon
An indented statement
&n a for bloc/6 4ython $i7es the name to each element in the iterable6 one at a time6 in
order9 &t reuses the name each time throu$h the loo#9 After 4ython $i7es the name to a
ne0 element6 it e8ecutes the instructions in the code bloc/9
3he e8am#le at the be$innin$ of this section com#utes and #rints the AS:&& 7alue of each
character in a strin$ usin$ the ord)* function%)
t2e ascii 5al-e of 2 is /%.
t2e ascii 5al-e of e is /%/
t2e ascii 5al-e of l is /%'
t2e ascii 5al-e of l is /%'
t2e ascii 5al-e of o is ///
t2e ascii 5al-e of ! is 33
Iterables, "etcher iterables<
A for loo# 0or/s only 0ith obFects that ha7e6 or can $enerate6 multi#le elements9 Such
obFects are called iterables because a for loo# $enerates multi#le iterations6 or re#eats6
usin$ the elements in the obFect%9
3@:<(&:A.
S3?FF
Mani#ulatin$ an obFect 0ith a for loo# is also called iterating over a
se7uence or looping through a se7uence9
<ere are some of 4ythonCs obFects that can be used 0ith a for loo#)
for element in range)/,.*! + range
print element
for element in [/, 2, 3]! + list
print element
for Aey in {,one,!/, ,t9o,!2}! + dictionary
print Aey
for line in open)"myfile"txt"*! + text file
-A+
print line
for 5al-e in mydict"iter5al-es)*! + iterator o(Tect
print 5al-e
for Aey, 5al-e in mydict"iteritems)*! + t-ple -npacAing
print Aey, 5al-e
Home on the ran"e
&tCs common to use a for loo# to iterate o7er a seGuence of numbers by usin$ the function
range)*6 0hich $enerates lists of arithmetic #ro$ressions9
3o use the range)* function6 feed it the follo0in$ ar$uments)
DeGuired) An inte$er s#ecifyin$ the end of the ran$e
D@M@M'@D 3he range)* function $enerates a list of numbers up to but not
including the end1of1ran$e number9 For e8am#le6 if you 0ant a list
of all the sin$le di$its6 you 0ould use -06 not *6 as the end of the
ran$e)
>>> range)/%*
[%, /, 2, 3, ., &, <, E, ', =]
M#tional) An inte$er s#ecifyin$ the start of the ran$e9 &t $oes before the inte$er
that s#ecifies the end of the ran$e9 &f left out6 it defaults to 09
3his e8am#le sho0s a range)* function 0ith a startin$ number of 29
>>> range)2, /%*
[2, 3, ., &, <, E, ', =]
Jptional! Ln integer to add to t2e c-rrent n-m(er
M#tional) An inte$er to add to the current number to $enerate the ne8t number9
3his is called a ste#9% &f left out6 it defaults to -9
&f you include the ste#6 you must include the start1of1ran$e number9
3his e8am#le sho0s a range)* function 0ith a ste# of "9
>>> range)%, /%, 3*
[%, 3, <, =]
3i# Eou can use range)* 0ith ne$ati7e numbers6 too6 as sho0n here)
>>> range)7/%, 7/%%, 73%*
[7/%, 7.%, 7E%]
Howdy, xran"e1r<
-A,
3he xrange)* function is similar to range)*6 but instead of returnin$ a list6 it returns an
iterator obFect that $enerates the numbers in the ran$e9
.oo#s usin$ xrange)* are a bit faster and use less memory9
Eou can see 4ythonCs internal difference bet0een range)* and xrange)* by com#arin$
the follo0in$9 (ote that 4ython con7erts the 8ran$e% obFect to an internal IeGui7alentI)
>>> range)%, /%, 3*
[%, 3, <, =]
>>> xrange)%, /%, 3*
xrange)%, /2, 3*
&sin" the ran"e56 function with a list
3o iterate o7er the inde8 numbers of a seGuence such as a list%6 use a for statement 0ith
range)* and len)* 0hich finds the number of elements in a seGuence%6 li/e this)
for i in range)len)list$name**!
3he en-merate)* function6 ne0 in 4ython 29"6 0or/s similarly9 See the I(umbered and
a##ro7edI section6 later in this cha#ter6 to find out more9
Warnin$ &t isnCt safe to modify the list youCre iteratin$ o7er5 3o du#licate selected items
or other0ise modify items in a list youCre usin$ in a loo#6 either
?se a 92ile loo# see the ne8t section%9
:reate a ne0 list to store the modifications6 as sho0n in this e8am#le)
>>> man$from$st$i5es = [,9i5es,, ,sacAs,, ,cats,, ,Aits,]
>>> ne9list = []
>>> for i in man$from$st$i5es!
""" ne9list := [,E,, i]
"""
>>> ne9list
[,E,, ,9i5es,, ,E,, ,sacAs,, ,E,, ,cats,, ,E,, ,Aits,]
*hiling A&ay
A 92ile loo# #erforms an action an indefinite number of times6 as lon$ as a condition is
true9
Warnin$ &f the condition is al0ays true6 the 92ile loo# ne7er sto#s unless a ret-rn or
(reaA statement is encountered6 or an e8ce#tion is raised%9
3o 0rite a 92ile loo#6 follo0 these ste#s)
-A*
-9 Type 92ile6 a Boolean expression, and a colon 5 ! 6.
<ereCs an e8am#le)
92ile n-m(er$of$(ottles > %!
29 (n the next line, type four spaces and then type the action you want
repeated, like this,
3" 92ile n-m(er$of$(ottles > %!
." print n-m(er$of$(ottles, "(ottles of (eer on t2e 9all"
&f you add more lines6 be sure to indent them9
3i# A 92ile loo# is useful in a #ro$ram that 0aits for user in#ut9 3he #ro$ram in .istin$
-01" re#orts on the results of se7eral Icoin tosses9I 3he 92ile loo# chec/s 0hether
the user has as/ed to Guit the #ro$ram9
.istin$ -01") :oin toss #ro$ram
import random
2eadco-nt = tailco-nt = %
-serinp-t = ,,
print "1o9 tossing a coin""""
92ile -serinp-t"lo9er)* != "N"!
flip = random"c2oice)[,2eads,, ,tails,]*
if flip == ,2eads,!
2eadco-nt := /
print "2eads! t2e n-m(er of 2eads is no9 Dd" D 2eadco-nt
else!
tailco-nt := /
print "tails! t2e n-m(er of tails is no9 Dd" D tailco-nt
print "Cress ,N, to N-it",
-serinp-t = ra9$inp-t)"or anot2er Aey to toss again!"*
print "t2e total n-m(er of 2eads!", 2eadco-nt
print "t2e total n-m(er of tails!", tailco-nt
-60
#hoosin" %our -oop
4ython has t0o /inds of loo#s) $hile loo#s and for loo#s9 &n $eneral you should use a for
loo#6 but there are a fe0 s#ecific situations 0here a 92ile loo# is better9 3his section
e8#lains 0hen each ty#e 0or/s best9
*hat for"s for
4ython em#hasizes for loo#s6 0hich are 7ery fle8ible and #o0erful9
D@M@M'@D Most of the time6 0hen you 0ant to re#eat a bloc/ of code more than once6
you should use a for loo#9 ?se for loo#s in the follo0in$ situations)
*hen you want to iterate throu"h a se=uence one item at a
time
*hen you want to repeat a loop a certain number of times
*hen you're readin" a text file
D@M@M'@D A for statement has t0o #arts)
An iterable6 0hich #ro7ides elements one at a time
&n this for statement6 the iterable is range)/%*)
>>> for i in range)/%*!
A loo# tar$et a name%6 0hich is $i7en to each 7alue that the
iterator $enerates
&n the abo7e for statement6 the loo# tar$et is i9
3he for loo# is so fle8ible because there are many 0ays to use the loo# tar$et name in
the body of the loo#9 For e8am#le6 you can $i7e a ne0 7alue to the name inside the loo#
as lon$ as you donCt mind if that 7alue $oes a0ay 0hen the loo# restarts%9 3his code
$i7es a ne0 7alue to i)
>>> for i in range)&*!
""" if i # 3!
""" i = "more spam!"
""" else!
""" i = "(learg2!"
""" print i,
"""
more spam! more spam! more spam! (learg2! (learg2!
Warnin$ 3he other side of the coin is that if you $i7e a ne0 7alue to the loo# tar$et6 you
canCt e8#ect the 7alue to stic/9 3his code doesnCt #rint 0hat you mi$ht e8#ect6
-6-
because i $ets reassi$ned to a number 0hen the loo# restarts)
>>> for i in range)&*!
""" print i,
""" if i # 3!
""" i = "more spam!"
""" else!
""" i = "(learg2!"
"""
% / 2 3 .
*hy to &hile
3he 92ile loo# is useful for these situations)
2vent1driven pro"rams, Many #ro$rams 0ait for a user to ta/e an action such
as clic/in$ a mouse button%6 and then res#ond to the action9 Mften6 a main 92ile
loo# collects user actions events% and sends them to chun/s of code that act on
#articular e7ents event handlers%9
-oops that need to run an undetermined number of times, &f you donCt /no0
ho0 much data you need to #rocess>for e8am#le6 0hen youCre readin$ a binary
file>you mi$ht use a 92ile loo# that re#eats its action until it detects that there
is no more data9
rocessin" a container ob>ect while modifyin" it, 3he spider"py #ro$ram in
:ha#ter ! includes this /ind of 92ile bloc/9 3he bloc/ uses the pop)* list method
to remo7es items from the linAs$to$process list9 A 92ile loo# is used because
no matter ho0 lon$ linAs$to$process is6 it 0ill run as lon$ as there are items
left in the list9 <o0e7er6 itCs im#ortant that the 92ile bloc/ remo7es items from
the list9 &f it didnCt6 the code 0ould run fore7er9
92ile self"$linAs$to$process!
-rl = self"$linAs$to$process"pop)*
self"log)"Betrie5ing! " : -rl*
self"process$page)-rl*
3he 92ile loo#6 because it includes a 'oolean e8#ression6 can sto# une8#ectedly if it
encounters data such as 1one% that e7aluates as false9 3his 92ile loo# sto#s in the
middle of #rocessin$ the list because data is $i7en the 7alue 1one6 0hich e7aluates as
false)
>>> def process)a*!
""" print a,
"""
>>> mylist = [/, 2, 1one, ., &]
>>> data = mylist"pop)*
>>> 92ile data!
""" process)data*
""" data = mylist"pop)*
"""
& .
-62
3he correct 0ay to #rocess items from a list in a 0hile loo# is as follo0s)
>>> mylist = [/, 2, 1one, ., &]
>>> 92ile mylist!
""" process)mylist"pop)**
"""
& . 1one 2 /
-oopy !tatements and 'unctions
3his section describes some s#ecial statements that 0or/ 0ith loo#s9 &t also details
se7eral useful functions that 0or/ 0ith loo#s9 Some of these statements and functions
also 0or/ 0ith other code bloc/s9
)seful looping stateents
Se7eral statements hel# you $et the most out of loo#s9 @ach of the follo0in$ statements
0or/s as #art of a loo# code bloc/ and $oes on a line by itself9
Take a break9or else<
3he /ey0ord (reaA sto#s the #rocessin$ of a loo# and e8its the loo#9 Any code in the
bloc/ that comes after the (reaA statement is i$nored any else clause is also s/i##ed%9
&f the loo# is nested inside another code bloc/6 the #ro$ram $oes bac/ to the bloc/ that
the loo# 0as nested in9
&n a loo#6 the (reaA is usually #art of a nested if statement9 &n the follo0in$ e8am#le6
(reaA e8its the if loo# 0hen the element has the 7alue ,n,9
>>> y = "astring"
>>> for i in y!
""" if i == ,n,!
""" print "(roAe!"
""" (reaA
""" print i,
""" print ";",
"""
a ; s ; t ; r ; i ; (roAe!
%ou and who elseC
.oo#s6 li/e if statements6 can ha7e an else clause that e8ecutes 0hen the condition in
the loo# e7aluates as false9 Mne 0ay to use the else clause is 0ith a nested if sub1bloc/
that has a (reaA statement in it9 &t 0or/s li/e this)
-9 @ach time around the loo#6 the condition in the if statement is tested9
-6"
29 &f the condition is true6 the (reaA statement causes e7erythin$ else in the loo#
includin$ the else bloc/% to be s/i##ed9 :ontrol e8its the loo#9
"9 &f the condition is false6 the loo# starts o7er a$ain9
!9 &f the loo# iterator runs out before the if condition e7aluates as true6 the for
loo#Cs else clause $ets e8ecuted9
3he follo0in$ code determines 0hether a strin$ in this case6 ,larc2,% contains any
lo0ercase 7o0els)
mystring = "larc2"
5o9els = set),aeio-,*
for a in mystring!
if a in 5o9els!
print mystring, "2as a 5o9el"
(reaA
else!
print mystring, "does not 2a5e a 5o9el"
3he result is)
larc2 2as a 5o9el
-et's continue
When 4ython sees a contin-e statement in the middle of a loo# code bloc/6 it s/i#s the
rest of the code in the bloc/ and $oes bac/ to the to# of the loo#9 <ereCs ho0 it 0or/s9
3his code bloc/ #rints both IAI and Ifi7eI because the print n, statement is e8ecuted
each time throu$h the loo#9
>>> for n in range)/%*!
""" if n == &!
""" print "fi5e",
""" print n,
"""
% / 2 3 . fi5e & < E ' =
When 0e add contin-e to the if bloc/6 4ython s/i#s the last print n, statement 0hen
the loo# condition e7aluates as trueH therefore6 it #rints only Ifi7eI)
>>> for n in range)/%*!
""" if n == &!
""" print "fi5e",
""" contin-e
""" print n,
"""
% / 2 3 . fi5e < E ' =
3i# ?se contin-e statements s#arin$ly because they can ma/e your code hard to read6
es#ecially if they are nested in com#le8 structures9
-6!
Thanks, but I'll pass
3he pass /ey0ord tells the 4ython inter#reter to do nothin$9 &t is usually used in class
statements6 but youCll also find it useful in 92ile or for loo#s 0hen the #ro$ram reGuires
no action9 &n this e8am#le6 the arg-ment$clinic)* function does nothin$ if its ar$ument
is $reater than or eGual to A)
>>> def arg-ment$clinic)min-tes*!
""" if min-tes >= &!
""" pass
""" else!
""" print "1o it isn,t!"
"""
>>> min-tes = .
>>> arg-ment$clinic)min-tes*
1o it isn,t!
>>> min-tes = &
>>> arg-ment$clinic)min-tes* + not2ing 2appens
>>>
3i# 3he pass statement is also useful 0hen you are startin$ to build a #ro$ram9 Eou can
create the s/eleton of the #ro$ramCs code bloc/s by usin$ pass and then fill in 0hat
the bloc/s do later9 'ut donCt for$et to ta/e out pass5%
3his e8am#le code 0ill e7entually sol7e all the 0orldCs #roblems6 but for no0 itCs Fust a
#laceholder)
def grand$sol-tion)pro(lems*!
""" sol5e all t2e 9orld,s pro(lems 2ere """
+ We 9ill fig-re t2is o-t someday
pass
Loopy functions
3his section describes some functions that are es#ecially useful 0hen 0or/in$ 0ith
loo#s9
3@:<(&:A.
S3?FF
All the functions described in this section 0or/ 0ith iterables in
$eneral6 not Fust 0ith seGuences9
?umbered and approved
&n 4ython 29" and later6 use the en-merate)* function in a loo# to return the inde8
#osition% and 7alue of each element9 Follo0 these ste#s)
-9 Type the followin", substitutin" your own iterable for the example list,
2" for i, 5 in en-merate)[,tic,, ,tac,, ,toe,]*!
"9 (n the next line, indent four spaces and type some code that you want
ython to execute.
-6A
." for i, 5 in en-merate)[,tic,, ,tac,, ,toe,]*!
&" print i, 5
<ereCs the result of the code)
% tic
/ tac
2 toe
3@:<(&:A.
S3?FF
3echnically s#ea/in$6 en-merate)* returns an iterator that creates an
inde86 7alue% tu#le for each data item9 3he tu#les $enerated by the
#recedin$ code are )%, ,tic,*, )/, ,tac,*, )2, ,toe,*9
The two1loop tan"o
3he Qip)* function6 ne0 in 4ython 2906 loo#s o7er t0o or more seGuences at the same
time9 &t returns a list of tu#les in 0hich an item in each seGuence is #aired 0ith the
corres#ondin$ items in the other seGuences9
3i# 3hin/ of a zi##er zi##in$ to$ether t0o sets of teeth5
3o 0rite a for statement that zi#s t0o seGuences6 ty#e the follo0in$ substitutin$ your
o0n seGuence names%)
for a, ( in Qip)seN-ence$one, seN-ence$t9o*!
3he follo0in$ e8am#le uses the Qip)* function in a for statement to e8#lain the rules of
the $ame Iroc/6 #a#er6 scissorsI)
>>> mylist/ = [,rocA,, ,paper,, ,scissors,]
>>> mylist2 = [,scissors,, ,rocA,, ,paper,]
>>> mylist3 = [,paper,, ,scissors,, ,rocA,]
>>> for a, (, c in Qip)mylist/, mylist2, mylist3*!
""" print "Ds (eats Ds (-t not Ds" D )a,(,c*
"""
rocA (eats scissors (-t not paper
paper (eats rocA (-t not scissors
scissors (eats paper (-t not rocA
+entlemen and ladies, unpack your tuples
A for loo# is a con7enient 0ay to unpack the 7alues in a tu#le9 A canonical e8am#le of
tu#le un#ac/in$ uses the iteritems)* method of dictionaries6 0hich first turns each
/ey)7alue #air into a t0o1item tu#le9
3i# We use items)* in this e8am#le to sho0 ho0 the transformation to a list of t0o1item
tu#les ta/es #lace9 &n actual code you should use the iteritems)* method instead9
3he result is the same6 but itCs more efficient9
3his e8am#le sho0s ho0 un#ac/in$ 0or/s)
-66
>>> c2eese$in5entory = {,Bed Windsor,! ,t2e 5an (roAe do9n,,
,Haerp2illy,! ,on order,, ,Bed 3eicester,! ,fres2 o-t,}
>>> c2eese$in5entory"items)*
[),Bed Windsor,, ,t2e 5an (roAe do9n,*, ),Haerp2illy,, ,on
order,*, ),Bed 3eicester,, ,fres2 o-t,*]
Eou can use the follo0in$ for loo# 0ith the iteritems)* method to un#ac/ the tu#le
elements)
>>> for Aey, 5al-e in c2eese$in5entory"iteritems)*!
""" print Aey, "!", 5al-e
"""
Bed Windsor ! t2e 5an (roAe do9n
Haerp2illy ! on order
Bed 3eicester ! fres2 o-t
+oin" backward
3he re5ersed)* function returns an iterator that $oes throu$h the seGuence items
bac/0ard9 3he re5ersed)* function is ne0 in 4ython 29!9
3he follo0in$ e8am#le uses re5ersed)* 0ith the range)* function as its ar$ument to
count do0n throu$h the seGuence)
for f in re5ersed)range)/, //**!
print f,
print "(lastoff!"
<ereCs 0hat it #rints)
/% = ' E < & . 3 2 / (lastoff!
(ut of sorts
3he sorted)* function returns a ne0 sorted list 0hile lea7in$ the source list unchan$ed9
&t 0or/s 0ith both lists and immutable seGuence obFects9 3his function is ne0 in 4ython
29!9
>>> mylist = [/, /%, 2, =, 3, ', ., E, &, <]
>>> myne9list = sorted)mylist*
>>> myne9list
[/, 2, 3, ., &, <, E, ', =, /%]
>>> mylist + original list is -nc2anged
[/, /%, 2, =, 3, ', ., E, &, <]
3@:<(&:A.
S3?FF
&n contrast6 the list method sort)* chan$es the ori$inal list in #lace9
>>> mylist"sort)*
>>> mylist
[/, 2, 3, ., &, <, E, ', =, /%]
-6+
3o use the sorted)* function in a for statement6 ty#e the follo0in$ substitutin$ the
name of your seGuence obFect%)
>>> myt-ple = )"an", "african", "or", "e-ropean", "s9allo9", "V"*
>>> for f in sorted)myt-ple*!
""" print f,
"""
V african an e-ropean or s9allo9
#hapter .., 'un with 'unctions
. function is a bloc/ of code that #erforms a s#ecific action and returns a result9
&n this cha#ter6 you find out about callin$ functions6 0ritin$ functions and their
docstrin$s6 and #assin$ ar$uments to functions9 EouCre also inducted into the arcane
mysteries of the names#ace9
I -ove #hunky #ode
<ereCs ho0 functions ma/e your life easier as a #ro$rammer and ma/e thin$s easier on
#eo#le 0ho read and use your code6 too)
'unctions let you write code only once.
When you create code that you 0ant to reuse6 be sure to define it as a function9
3his sa7es you from ty#in$ the same code o7er a$ain 0hen you 0ant to use it in
another #ro$ram9
'unctions hide unnecessary complexity from the user.
(o one e7er has to care 0hat this function does unless itCs included in your
#ro$ram9 And e7en if someone does use the function6 they donCt ha7e to care ho$
it 0or/s6 as lon$ as it #erforms the 0ay its documentation says it 0ill9
'unctions make your code easier to understand.
&f you name a function accordin$ to 0hat its code does6 then someone readin$ the
#ro$ram Fust has to loo/ at the name to $et an understandin$ of 0hat the #ro$ram
is doin$ at that #oint9 Mf course6 you should also include comments in your
#ro$ram and comments and docstrin$s in your function to further e8#lain 0hatCs
ha##enin$9%
'unctions help you or"ani)e your pro"ram lo"ically.
-6,
Writin$ most of your code chun/s in functions hel#s you or$anize your #ro$ramCs
structure lo$ically6 and that can ma/e your #ro$rams easier to understand9
#alling a function
?sin$ a function is /no0n as calling the function9 When you call a function6 the
functionCs code runs and returns a result9
3o call a function6 follo0 these ste#s)
-9 Type a name to hold the result of the function followed by an e=uals si"n 5!6.
Eou donCt ha7e to do this6 but usually you 0ant to do somethin$ 0ith the function
result6 and itCs easier if a name has been $i7en to the result9
29 Type the name of the function.
"9 In parentheses, "ive the function information about what you want it to act
on.
;i7in$ a function information is called passing arguments9 Some functions donCt
ta/e any ar$uments6 but most do9
Eou can #ass the function a 7alue or a name9
&n the follo0in$ e8am#le6 the s-m)* function is $i7en one ar$ument) the name of a list
p-rc2ases%9 3he name total is $i7en to the result)
arameters versus ar"uments
&n this cha#ter6 0e use the terms parameter and argument 0hen discussin$ the data that a
function needs to do its 0or/9 <ereCs a 0ay to understand the difference)
A parameter is a #laceholder for the data9 When you are 0ritin$ a function6 you use
#arameters to tell the function to e8#ect data to be #assed9 3hen you use the #arameter
name inside the function to stand for that data9
An argumentis the actual data that3s passed 0hen you call the function9 <ereCs an
e8am#le)
>>> def a$f-nction)a$param*!
""" print "my parameter is a$param
and my arg-ment is",
repr)a$param*
"""
>>> a$f-nction)"my arg"*
my parameter is a$param and my arg-ment
is "my arg"
-6*
3@:<(&:A.
S3?FF
'unctions versus methods
WhatCs the difference bet0een a function and a method2 (ot much9 A
method is a /ind of function>both #erform a s#ecific action on some
information you $i7e them9 <o0e7er6 a method is associated 0ith a
#articular obFect and #erforms its action on that obFect9 Mften6 that
obFect is a data ty#e9 For e8am#le6 a list comes 0ith the append)*
method6 0hich lets you add items to the end of the list9 Whene7er
you create a ne0 list obFect by usin$ a statement such as my$list =
[/,2,3]6 the obFect my$list has access to all the methods for list
obFects9
Functions6 on the other hand6 stand on their o0n9 Eou donCt ha7e to
create an obFect in order to use one9
Eou call functions and methods usin$ different synta8)
3o use a function6 ty#e the function name)
my$f-nction)x*
3o use a method6 a##end it to the name of the obFect6
se#aratin$ the name and the method 0ith a dot6 li/e this)
my$list"append)x*
>>> p-rc2ases = [/3"%2, 2"==, .".&]
>>> total = s-m)p-rc2ases*
>>> print total
2%".<
1efining a %unction
?nless youCre usin$ a built1in function6 you need to define the function before you can
call it9 3hese are the basic ste#s for 0ritin$ a function)
-9 (n the first line, write the def statement for the function.
Follo0 these ste#s)
a9 "rite def and the function name.
b9 /n parentheses, add the names of parameters that the function $ill act on.
,eparate parameters $ith commas.
&f your function doesnCt ha7e #arameters6 it still needs the #arentheses9
c9 &ype a colon at the end of the statement.
Eour def statement mi$ht loo/ somethin$ li/e this)
-+0
def my$f-nction)arg/, arg2*!
29 5(ptional, but very important6 (n the next lines, write the docstrin" for the
function.
Follo0 these ste#s)
a9 On a ne$ line, indent four spaces and type three 7uotation marks %a triple
7uote4.
b9 &ype an e!planation of $hat the function does and ho$ to use it.
See the sidebar IWhatCs u#6 Doc2I to find out 0hat to include9
c9 On the ne!t line, indent four spaces and type a triple 7uote9
Eour function and its docstrin$ mi$ht loo/ somethin$ li/e this)
def my$f-nction)arg/, arg2*!
"""
Cerform my f-nction on arg/ -sing arg2" Bet-rn a list"
"""
"9 *rite the function's code.
&ndent each line of code four s#aces9 &f your function contains another
subordinate or nested code bloc/6 such as an if statement6 that bloc/Cs code must
be indented an additional four s#aces6 and so on9%
When youCre finished6 the function mi$ht loo/ li/e this but containin$ 4ython
code6 of course5%)
def my$f-nction)arg/, arg2*!
"""
my f-nction does t2is
"""
do somet2ing
if t2is!
do t2at
ret-rn somet2ing else
Gi$ing another nae to a function
After you im#ort a function6 you can $i7e it a ne0 name9 3he additional name refers to
the same obFect at the same location as the old name6 as this e8am#le sho0s)
>>> tinyf-nction
#f-nction tinyf-nction at %x</.3%>
>>> myf-nc = tinyf-nction
-+-
>>> myf-nc
#f-nction tinyf-nction at %x</.3%>
3i# &tCs im#ortant to remember the difference bet0een giving a ne$ name to a function
and calling a function $hile giving a name to the result. When you call a function6
you add #arentheses at the end of the function name9 3hese e8am#les sho0 the
difference)
>>> myf-nction = tinymod-le"tinyf-nction + gi5ing a ne9 name to a
f-nction
>>> myres-lt = tinymod-le"tinyf-nction)2* + calling a f-nction
*hat's up, :ocC
&f the first code in a function is a strin$6 it becomes the docstrin$ for the function9 3he
docstrin$ defines 0hat the function does and 0or/s 0ith 4ythonCs 2elp)* utility9 We
recommend 0ritin$ doc1strin$s for all your functions9
A docstrin$ usually s#ans multi#le lines6 so it be$ins and ends 0ith tri#le Guotes9
'y con7ention amon$ 4ython #ro$rammers6 a basic docstrin$ for a function loo/s li/e
this)
-9 3he first line the only line for a sim#le function% is a descri#tion of 0hat the
function does6 or a summary if the function does se7eral thin$s or has se7eral
o#tions9
For e8am#le6 ta/e this function that #rints somethin$)
def printme)me*!
print me
3his function does only one thin$6 so the docstrin$ mi$ht loo/ li/e this)
"""Crint t2e arg-ment"""
29 &f the function needs more than one line of documentation6 the second line should
be blan/9
"9 3he third and subseGuent lines e8#lain o#tions6 defaults for o#tional ar$uments6
and other usa$e notes9
Say 0e chan$e the printme)* function so it ta/es an o#tional ar$ument>a list of
0ords not to #rint)
def printme)me, (ad$9ord$list=1one*!
if (ad$9ord$list and me in (ad$9ord$list!
print "Ho9 dare yo-!"
else!
print me
-+2
We mi$ht $i7e it the follo0in$ docstrin$)
"""
Crint arg-ment" Jptionally c2ecA against list of 9ords not to print"
Xey9ord arg-ment!
(ad$9ord$list 77 list of 9ords not to print )defa-lt 1one*
"""
Eou can find out more about 4ythonCs docstrin$ con7entions by readin$ 4ython
@nhancement 4ro#osal 4@4% 2A+ at)
htt#)LL0009#ython9or$Lde7L#e#sL#e#102A+
/eturning $alues fro a function
(ames you define inside a function disa##ear after the function e8its9 3o $et 7alues out of
a function6 you must do t0o thin$s)
Include a ret-rn statement in the function with a name or value, like this,
def my$f-nction)(*!
a = ( : .2
ret"rn #
&se the function in an expression or in an assi"nment statement,
my$5al-e = my$f-nction)3*
3he name on the left side of the eGuals si$n in the assi$nment statement no0
stores the 7alue that the function returned6 as sho0n)
>>> my$5al-e = my$f-nction)3*
>>> print my$5al-e
.&
&f a function has no ret-rn statement6 or if the ret-rn statement doesnCt s#ecify 0hat to
return6 the function returns 1one a built1in name%9 <ereCs a function 0ithout a ret-rn
statement9
>>> def no$ret-rn$f-nction)(*!
""" print (
"""
>>> my$5al-e = no$ret-rn$f-nction)3*
3
>>> print my$5al-e
1one
So 0hy 0ould you e7er 0ant a function 0ithout a ret-rn statement2 Some functions
mani#ulate an item in #lace instead of returnin$ a ne0 item9 Some functions #erform an
-+"
action that doesnCt need to return a result for e8am#le6 sa7in$ a messa$e to a lo$%9 Such a
function mi$ht not need a ret-rn statement9
3i# 3he ret-rn statement not only #asses a 7alue out of the function6 it also sto#s
4ython from runnin$ the rest of the code in the function9 So ret-rn either should be
the last statement in the function or should be used in conditional code6 such as an if
bloc/6 to Fum# out of the function 0hen a certain condition is met9
Ar"ument #linic, assin" :ata
4arameters are #laceholders for information that you $i7e to a function so it can carry out
an action9
3here are many 0ays to s#ecify a functionCs #arameters9 3his section introduces
s#ecifyin$ #ositional #arameters6 default17alue #arameters6 and arbitrary numbers of
#arameters9 &t also describes ho0 to a7oid the Guir/s of default17alue #arameters and
mutable obFects9
Introducing paraeters and arguents
<ere are a fe0 thin$s to /no0 about function #arameters)
A function can have any number of parameters 5or no parameters6.
*hen you define a function, you specify how many parameters it has.
&t can ha7e a s#ecific number of #arameters or an indefinite number of
#arameters9
%ou can define default values for any or all of the parameters.
When you call a function6 you need to $i7e it a 7alue corres#ondin$ to each #arameter9
3his actual 7alue is called an argument.% <ere are a fe0 thin$s to /no0 about ar$uments)
Ar"uments are passed when you call the function.
If the parameter has a default value, you don't have to pass an ar"ument.
%ou can pass an ar"ument as a literal or as a name.
&f you #ass an ar$ument as a name6 the function acts on the obFect the name refers
to9
Specifying arguents &hen you call a function
3he basic 0ay of s#ecifyin$ ar$uments is to pass them 0hen you call the function9
-+!
3o #ass an ar$ument6 ty#e it bet0een the #arentheses of a function 0hen you call the
function6 li/e this)
>>> a$f-nction(my#rg)
3he follo0in$ e8am#le creates a function that #rints the 7alue of its ar$ument6 $i7es a
name to a tu#le6 and #asses the name to the function)
>>> def a$f-nction)x*!
""" print "yo- passed me t2e arg-ment", repr)x*
"""
>>> myarg = )/,2,3*
>>> a$f-nction)myarg*
yo- passed me t2e arg-ment )/, 2, 3*
3he follo0in$ e8am#le #asses the same function a strin$ literal 0ithout definin$ a name)
>>> a$f-nction)"2ello"*
yo- passed me t2e arg-ment ,2ello,
Specifying arguents &ith key&ords and default $alues
A function can ha7e default 7alues for some of the #arameters9 Such default17alue
#arameters are sometimes called key$ord arguments9% <ere are some circumstances in
0hich default17alue #arameters are useful)
A particular value will be used more often than others.
For e8am#le6 4ythonCs ro-nd)* function ta/es a reGuired number ar$ument and
an o#tional #recision ar$ument9 3he #recision #arameter defaults to 0 because6
most of the time6 0hen you round a number6 you 0ant the inte$er closest to the
number rather than a decimal9
The function is desi"ned to work in more than one way, dependin" on which
ar"uments are passed.
For e8am#le6 the open)* function o#ens a file for readin$ by default9 &f you 0ant
to o#en a file for 0ritin$6 you need to s#ecify a mode ar$ument6 ,9,9
When addin$ default17alue #arameters to function definitions6 follo0 these rules)
-9 Type any parameters that won't have keywords first.
4arameters 0ithout /ey0ords are called positional9
29 Type keyword ar"uments in the form keyword=value9
-+A
3he follo0in$ function definition has one reGuired #ositional% #arameter ingredient%
and t0o default17alue #arameters)
def recipes)ingredient, ser5ings=., mode="Wegetarian"*!
When callin$ a function that includes default17alue #arameters6 follo0 these rules)
-9 ass values for all positional parameters first.
?se the same order in 0hich the functionCs #arameters are defined9
29 ass values or names for any default1value parameters whose default values
you want to override.
<ere are some additional $uidelines for default17alue #arameters)
DonCt #ass information for default17alue #arameters 0hose default 7alues you
0ant to use9 For e8am#le6 this function call uses both defaults)
>>> recipes),s2iitaAe m-s2rooms,*
&f you include default17alue #arameters in the order in 0hich they 0ere defined6
you can Fust #ass their 7alues6 li/e this)
>>> recipes),ar-g-la,, ', "Harni5ore"*
&f you are includin$ default17alue #arameters in a different order or s/i##in$ some
#arameters6 use the keyword=value format6 as sho0n here)
>>> recipes),ar-g-la,, mode="1o pean-ts"*
A$oiding the 3uirks of default $alues
Default 7alues are useful as function #arameters6 but you need to be a0are of their Guir/s9
Immutable default values stay the same
4ython e7aluates default 7alues in function definitions only once>0hen the function is
defined6 that is6 0hen the def statement first runs9 Another 0ay to say this is that 4ython
binds the default to the parameter9% def is an e8ecutable statementH this is 0hy you ha7e
to define a function before you can call it9 After that6 if you chan$e the 7alue of a name
you used as a default in the function definition6 the function 0ill continue to use the
ori$inal 7alue 0hen the 7alue is an immutable data ty#e9 &n the e8am#le belo06 the def is
#assed the 7alue of the inte$er myn-m9 When myn-m is chan$ed later6 the function still
uses the old 7alue)
>>> myn-m = ' + Gntegers are imm-ta(le"
>>> def f)arg=myn-m*!
""" ret-rn arg
"""
>>> f)*
'
-+6
>>> myn-m = /% + We,re
>>> f)* + """(-t t2e f-nction still -ses '"
'
4utable default values can chan"e
When you use a mutable obFect such as a list6 dictionary6 or class instance% as a default
and chan$e the obFect inside the function6 then the ne8t time you call the function6 the
function uses the chan$ed obFect9 For e8am#le6 the follo0in$ function creates a list that
accumulates the ar$uments #assed to it9
>>> def f)N, mylist=[]*!
""" mylist"append)N*
""" ret-rn mylist
"""
>>> print f),a,*
[,a,]
>>> print f),(,*
[,a,, ,(,]
>>> print f),c,*
[,a,, ,(,, ,c,]
<ereCs 0hatCs $oin$ on)
-9 When the function is defined6 it creates an em#ty list obFect named mylist9
29 &nside the function6 the append)* method adds an item to the list9
3he append)* method chan$es the contents of the list in place: that is6 mylist
still refers to the same obFect after bein$ chan$ed9
"9 When the function is called a$ain6 it $rabs the same list from the same #lace and
uses the ne0 contents9
Assi"nment 5L6 always chan"es
Assi$nment al0ays causes a name to refer to a different obFect that is6 it rebinds%6 e7en if
the obFect is mutable9 So if you use assi$nment =% inside the function rather than the
append)* method6 mylist doesnCt accumulate ar$uments9
>>> def f)N, mylist=[]*!
""" mylist = mylist : N
""" ret-rn mylist
"""
>>> a = [/]
>>> f)a*
[/]
>>> a = [2]
>>> f)a*
[2]
-++
Au"mented assi"nment 5ML6 doesn't rebind mutable data types
Au$mented assi$nment o#erators := and ;=% rebind 0hen used 0ith an immutable data
ty#e6 but donCt rebind 0hen used 0ith a mutable data ty#e6 such as a list9 When used 0ith
a list6 := is the eGui7alent of the extend)* methodH it adds items to the end of a list9 So if
0e re0rite the function to use au$mented assi$nment6 the function e8tends mylist
0hich is mutable% by addin$ the ar$ument to the end of the list)
>>> def f)N, mylist=[]*!
""" mylist := N
""" ret-rn mylist
"""
>>> a = [/]
>>> f)a*
[/]
>>> a = [2]
>>> f)a*
[/, 2]
*orkin" on a copy of a mutable ob>ect
'ecause usin$ mutable obFects as ar$uments can ha7e une8#ected results6 youCre better
off o#eratin$ on a co#y of the obFect9 3his function creates a ne0 co#y of its list and then
adds a 7alue to each list element)
def f)mylist, data*!
ne9list = mylist[!] + maAe a copy of mylist
for i in range)len)mylist**!
ne9list[i] = mylist[i] : data
ret-rn ne9list
:allin$ the abo7e function doesnCt chan$e the ori$inal list)
>>> alist = [/, 2, 3]
>>> x = f)alist, &*
>>> print x
[<, E, ']
>>> print alist
[/, 2, 3]
&sin" a default value of ?one to redefine a mutable ob>ect
&f you 0ant a function to redefine a list as em#ty each time you call it6 use a default
#arameter of mylist=1one9 1one is immutable6 so the function 0onCt be referrin$ to a list
obFect that mi$ht chan$e9
3he follo0in$ code $i7es the #arameter mylist a default 7alue of 1one in its def
statement and then redefines mylist as an em#ty list inside the function)
-+,
def f)a, mylist=1one*!
if mylist is 1one!
mylist = []
mylist"append)a*
ret-rn mylist
When you call the abo7e function se7eral times6 it doesnCt accumulate ar$uments)
>>> a = /
>>> f)a*
[/]
>>> a = 2
>>> f)a*
[2]
Specifying a function &ith an arbitrary nuber of arguents
3o #ass an arbitrary number of ar$uments to a function6 use the #arameter ;args in the
def statement for the function9 <ere are ti#s and rules for usin$ ;args)
3he #arameter ;args stands for a set of #ositional ar$uments that arenCt e8#licitly
named in the function definition9
When you call the function and #ass it ar$uments6 ;args automatically creates a
tu#le out of the ar$uments9
3he #arameter ;args must come last in the function definition or ne8t1to1last if
there is also a ;;A9args #arameter%9
3o #ass an arbitrary number of keyword=value #airs to a function6 use the #arameter
;;A9args in the functionCs def statement9 <ereCs ho0 it 0or/s)
When you call the function and #ass the Aey9ord=5al-e #airs6 ;;A9args
automatically creates a dictionary from them9
3he #arameter ;;A9args must come last in the function definition9
3@:<(&:A.
S3?FF
3he names donCt ha7e to be ;args or ;;A9argsH 4ython cares only
that the ; or ;; o#erator comes first in the name9 'ut 4ythonistas
al0ays use ;args and ;;A9args9 3his consistency ma/es #ro$rams
easier to read by humans9
3his e8am#le #ro$ram sho0s ho0 ;args and ;;A9args 0or/ in a function that also has
t0o #ositional #arameters)
def a$f-nction)a, (, ;args, ;;A9args*!
print "a is", a
print "( is", (
print ";args is t2is t-ple!", args
print ";;A9args is t2is dictionary!", A9args
&t #roduces the follo0in$ result)
-+*
>>> a$f-nction)/, ,2,, ,t2ree,, ,(lind,, ,mice,, see="2o9", t2ey="r-n"*
a is /
( is 2
;args is t2is t-ple! ),t2ree,, ,(lind,, ,mice,*
;;A9args is t2is dictionary! {,see,! ,2o9,, ,t2ey,! ,r-n,}
)npacking arguents
3o #ass the elements of a list6 tu#le6 or dictionary as ar$uments in a function6 use the ;
o#erator for lists and tu#les% or the ;; o#erator for dictionaries% 0hen callin$ the
function to unpack the elements9
3o un#ac/6 you need these thin$s)
A function
3he function can ha7e any /ind of #arameters>#ositional or default6 ;args6 or
;;A9argsS or e7en no #arameters at all9
A list, tuple, or dictionary with the same number of elements as the number
of parameters in the function
&npackin" lists or tuples
3o #ass the elements of a list or tu#le as ar$uments in a function6 use this line of code
substitutin$ your function name and list or tu#le name%)
myf-nction)*my_list_or_t"ple*
3he follo0in$ code defines and calls a function6 #assin$ a list as its ar$ument6 usin$ the ;
o#erator)
>>> def f-nc$9it2$t2ree$args)a, (, c*!
""" print "t2e arg-ments are!", a, (, c
"""
>>> 9eapons = [,fear,, ,s-rprise,, ,r-t2less efficiency,]
>>> f-nc$9it2$t2ree$args);9eapons*
t2e arg-ments are! fear s-rprise r-t2less efficiency
&npackin" dictionaries
3o #ass the /ey)7alue #airs of a dictionary to a function6 use this line of code substitutin$
your function name and dict name%)
myf-nction)**mydict*
-,0
3he dictionary /eys must ha7e the same names as the functionCs #arameters9 3he
follo0in$ code #asses a dictionary to f-nc$9it2$t2ree$args)* usin$ the ;; o#erator9
3he dictionaryCs /eys match the ar$ument names)
>>> not$in$stocA = {"a"!"Bed 3eicester", "("!"Farls(erg",
"c"!"Hamem(ert"}
>>> f-nc$9it2$t2ree$args);;not$in$stocA*
t2e arg-ments are! Bed 3eicester Farls(erg Hamem(ert
*hat's in a ?amespace
A namespace, also called a symbol table or scope, is stora$e for the names of obFects
4ython /no0s about9
D@M@M'@D 3he most im#ortant thin$ to remember about function namespaces is this)
When you $i7e a name to a 7alue6 you are al0ays assi$nin$ 0ithin the
functionCs local names#ace unless
Eou e8#licitly say that the name is $lobal
Eou s#ecify that the name is an attribute of a #articular obFect
1isco$ering &here Python looks for naes
4ython has three basic layers of names#aces6 listed here in order of most s#ecific to most
$eneral9 4ython loo/s for names in this order and sto#s loo/in$ as soon as it finds the
name)
-9 .ocal names defined inside a class6 function6 or method%
29 ;lobal names defined inside a module>often function names and class names6
but can be other names6 too%
"9 'uilt1in names that are al0ays a7ailable%
&f 4ython canCt find a name in any of those #laces6 it raises a 1ame6rror e8ce#tion9 &f you
$et an Lttri(-te6rror6 that means that 4ython found the leftmost first% name but didnCt
find the name after the dot9 @ach obFect has its o0n names#ace that 4ython searches9 For
more information about obFect names#aces6 see :ha#ters -2 and -"9
)nderstanding function naespaces
When 4ython encounters a function definition in a chun/ of code6 it e!ecutes the
definition9 3hat #re#ares the function for bein$ called later9 When 4ython e8ecutes the
definition6 the follo0in$ thin$s ha##en)
3he functionCs name is stored in the current names#ace9
-,-
&f the function is #art of a module6 this is the moduleCs names#ace9 &f the function
0as im#orted directly6 it mi$ht be the main names#ace in interacti7e mode9
4ython creates a ne0 names#ace for storin$ any local names defined 0ithin the
function9
D@M@M'@D 4ython searches from local to $lobal for names6 but it doesnCt search from
$lobal to local9 3hat means $lobal names#aces donCt /no0 about the names
inside local names#aces9
3his beha7ior of 4ythonCs is useful for hidin$ com#le8ity9 &t also #re7ents bu$s that mi$ht
occur if all names 0ere automatically $lobal>if that 0ere true6 it 0ould be easy to use a
name t0ice 0ithout realizin$ it6 and une8#ected thin$s mi$ht ha##en9 When names are
automatically local6 they are less li/ely to conflict9
How local and "lobal names work with assi"nment
(ames are automatically local unless you tell 4ython to treat them as $lobal%9 3his
means that if you $i7e a name to a 7alue usin$ an eGuals si$n% inside a function6 0hen
you e8it the function6 4ython 0ill for$et about the name9 <ereCs a #ro$ram that
demonstrates this feature of names#aces usin$ ;rouchoCs famous sayin$9 Eou could ty#e
it into a te8t file and sa7e it 0ith the name gro-c2o"py)
+ gro-c2o"py
a$(ooA = "man,s (est friend"
print "o-tside of a dog, a (ooA is", a$(ooA
def a$dog)*!
a$(ooA = "too darA to read"
print "inside of a dog, it,s", a$(ooA
a$dog)*
print "9e,re (acA o-tside of t2e dog again"
print "and a (ooA is again", a$(ooA
Fi$ure --1- illustrates $lobal and local names9
-,2
Fi$ure --1-) A $lobal and a local name assi$nment9
&f 0e run the #ro$ram from the command line6 it has this out#ut)
D pyt2on gro-c2o"py
o-tside of a dog, a (ooA is man,s (est friend
inside of a dog, it,s too darA to read
9e,re (acA o-tside of t2e dog again
and a (ooA is again man,s (est friend
How local and "lobal names work with references
'ecause 4ython searches for names first locally and then $lobally6 you can reference a
$lobal name inside the function>assumin$ the function doesnCt contain a local name
thatCs the same as the $lobal name9 3his e8am#le #ro$ram 0e call it (rig2tdog"py% and
Fi$ure --12 sho0 that a function can access the name a$(ooA defined outside the
function)
+ (rig2tdog"py
a$(ooA = "man,s (est friend"
print "o-tside of a dog, a (ooA is", a$(ooA
def a$(rig2t$dog)*!
print "inside of 0HG@ dog, a (ooA is still", a$(ooA
a$(rig2t$dog)*
-,"
Fi$ure --12) A $lobal name referenced locally9
3his code6 run on the command line6 #roduces the follo0in$ result)
D pyt2on (rig2tdog"py
o-tside of a dog, a (ooA is man,s (est friend
inside of 0HG@ dog, a (ooA is still man,s (est friend
3@:<(&:A.
S3?FF
Eou can also define a function inside another function9 3his creates a
ne0 local names#ace for the nested function9 3his names#ace is
called a nested scope. (ested sco#es are mostly useful for functional
#ro$rammin$9 :ha#ter -6 has some information on functional
#ro$rammin$9% (ested sco#es 0or/ differently in 4ython 29- and
later than they 0or/ in earlier 7ersions9 We donCt co7er them in this
boo/9
Think globally! act locally
&tCs easy to e8#licitly tell a function that a name is $lobal) Eou Fust use the /ey0ord
glo(al 0ith the name9 &n the e8am#le #ro$ram belo06 0hich 0e call colors"py6
definin$ the name eggcolor as $lobal allo0s the function to chan$e the 7alue of that
name6 and the chan$ed 7alue remains in effect after 4ython e8its the function9 &n contrast6
the name meat6 0hich isnCt declared $lobal6 is chan$ed only inside the function9
+ colors"py
eggcolor = "green"
meat = "2am"
print eggcolor, "eggs and", meat
def (reaAfast)*!
glo(al eggcolor
eggcolor = "red"
meat = "(acon"
print eggcolor, "eggs and", meat
(reaAfast)*
-,!
print eggcolor, "eggs and", meat
&f you run this code on the command line6 you $et the follo0in$ result)
D pyt2on colors"py
green eggs and 2am
red eggs and (acon
red eggs and 2am
D@M@M'@D &tCs usually a 'ad &dea to use $lobal names because they are more li/ely to
cause name conflicts in com#le8 #ro$rams9
Sorting out odule naespaces
@ach module a file 0ith 4ython code6 endin$ in "py% has its o0n names#ace9 All the
functions defined in the module use the moduleCs names#ace as their $lobal names#ace9
3he functions 0onCt loo/ else0here e8ce#t for the built1in names#ace% for names that are
referenced 0ithin them9 3his means that the author of a module can use $lobal names in a
module 0ithout 0orryin$ about their conflictin$ 0ith $lobal names used else0here9 &n
other 0ords6 0hene7er you see I$lobalI used in 4ython6 thin/ Imodule $lobal9I
3i# For more about modules6 see :ha#ter -29
When a module im#orts another module6 the im#orted moduleCs name becomes #art of
the im#ortin$ moduleCs names#ace9 Eou can then access its functions and other
information by usin$ the module name6 a dot6 and the function name6 the same 0ay you
can 0hen you im#ort a module into interacti7e mode9
&ma$ine you ha7e a module called mymod"py that does nothin$ but im#ort another
module)
+ mod-le ,mymod,
import mat2
&f you im#ort this module into interacti7e mode6 you can #rint the mat2"pi constant by
ty#in$ this code)
>>> import mymod
>>> mymod"mat2"pi
3"/./&=2<&3&=
'ut the name mat2 and its constant pi are /no0n only inside the module9 3he
surroundin$ names#ace of interacti7e mode still doesnCt /no0 about the mat2 module)
>>> print mat2"pi
0race(acA )most recent call last*!
>ile "#stdin>", line /, in #mod-le>
1ame6rror! name ,mat2, is not defined
3@:<(&:A.
S3?FF
&tCs #ossible to mani#ulate a moduleCs $lobal names by usin$ the
same notation used to refer to its functions6 modulename.itemname9
-,A
Eou should do this only if you /no0 0hat youCre doin$9 &tCs an
ad7anced feature 0e donCt co7er in this boo/9%
#hapter .7, Buildin" Applications with
4odules and acka"es
4ython #ro7ides 0ays to or$anize code into files and directories)
'odules are the files that contain the buildin$ bloc/s and $lue of your #ro$ram6
includin$ functions6 import statements6 classes6 and so on9
Packages or$anize modules in directory hierarchies that hel# users understand
ho0 e7erythin$ 0or/s to$ether9
3his cha#ter sho0s you ho0 you can use modules and #ac/a$es to or$anize your #roFects
and ta/e ad7anta$e of all the tools a7ailable for 4ython9
4odular -ivin", !torin" %our #ode in 'iles
4ython stron$ly encoura$es you to or$anize function definitions and other statements in
se#arate files9 3hese files are called modules. A module is a te8t file containin$ 4ython
definitions and statements9 3he filename is the module name 0ith the suffi8 "py
a##ended9
3@:<(&:A.
S3?FF
Some 4ython modules are 0ritten in lan$ua$es other than 4ython6
most commonly : or :NN9 Such a module is called an e!tension
module.
&n the follo0in$ sections6 0e $i7e you the lo0do0n on creatin$ your o0n modules6
$ettin$ access to the modules that come 0ith 4ython6 and usin$ modules in your
#ro$rams9
3@:<(&:A.
S3?FF
Better codin" throu"h modularity
Modules ha7e se7eral benefits that let you code more efficiently and
s#end less time stom#in$ bu$s)
4odules save time.
Eou can 0rite code once and use it in many #ro$rams9
4odules hide complexity.
-,6
@ach module creates its o0n names#ace6 so its names 0onCt
conflict 0ith names defined else0here9
4odules make debu""in" easier.
Storin$ chun/s of code in modules ma/es it #ossible to 0rite
shorter #ro$rams9 4ythonCs debu$$in$ tools s#ecify 0hich
module an error is found in9
Iporting a odule or its contents
3o access a moduleCs code6 you im#ort the module into either
Interactive mode
3i# &m#ortin$ a module in interacti7e mode is co7ered in :ha#ter 29
Another ython pro"ram
'oth these 0ays of im#ortin$ are similar9 3he only difference is 0here you ty#e
the import statement9
3he follo0in$ sections co7er ho0 to im#ort a module6 0hat 4ython does 0hen you
im#ort a module6 and 0ays of accessin$ the $oodies in a module9
Importin" a module
3o im#ort a module in interacti7e mode6 or in another 4ython #ro$ram6 ty#e import
follo0ed by the name of the module 0ithout the "py suffi86 li/e so)
import mod-lename
Initiali)in" a module
&m#ortin$ a module for the first time in a #articular #ro$ram6 or in interacti7e mode6
causes 4ython to #erform a series of actions called initiali2ing the module9 <ereCs ho0
initializin$ 0or/s)
-9 4ython creates a module names#ace that stores the names defined in the module9
29 4ython runs the code in the module9
"9 4ython stores the name of the module in the local names#ace9
3@:<(&:A.
S3?FF
&nitializin$ ha##ens only the first time the module is im#orted9 &f you
im#ort the module a$ain6 the module code doesnCt run6 but the im#ort
u#dates the current names#ace9
-,+
Accessin" functions and other items inside a module
When you im#ort a module6 all the functions and other information in the module
collecti7ely called the moduleCs attributes% are a7ailable to you9
3o access a module attribute6 ty#e the module name6 a dot6 and the attribute name9 &f you
are callin$ one of the moduleCs functions see :ha#ter --% or creatin$ a class instance see
:ha#ter -"%6 also #ass any ar$uments that are reGuired9 For e8am#le)
mymod-le"myf-nction)x*
3@:<(&:A.
S3?FF
(ther ways of accessin" module attributes
Eou can im#ort attributes from a module into the local names#ace
directly by usin$ one of the follo0in$ lines of code)
3o im#ort a s#ecific item directly6 ty#e from6 the module
name6 import6 and the item name6 as sho0n)
from mymod-le import myf-nction
3o im#ort all of a moduleCs functions directly6 ty#e from6 the
module name6 import6 and an asteris/ ;%6 a 0ildcard
character that stands for Iall9I For e8am#le)
from mymod-le import ;
Warnin$5 Most of the time itCs a 'ad &dea to use the import ; synta8
because
Eou canCt control 0hat youCre im#ortin$9
Eou ris/ #ollutin$ the local names#ace 0ith all the names
from the im#orted modules6 0hich ma/es debu$$in$ much
more difficult9
When you access module attributes throu$h the module name6 youCre
less li/ely to run into conflicts bet0een the names inside one module
and the names inside another module9 And your code 0ill be easier to
read9
For e8am#le6 you mi$ht ha7e t0o modules that each ha7e a function
to combine t0o 7alues in some 0ay9 &f each moduleCs function 0ere
named com(ine)*6 then if you tried to access each of these functions
directly6 you mi$ht accidentally use the 0ron$ one6 or readers of your
code mi$ht not /no0 0hich function you 0ere usin$ at any #articular
time9 'ut if you al0ays use mod-lename"com(ine)*6 then 0hich
function youCre usin$ is clear9
3hereCs one situation6 ho0e7er6 0hen itCs a##ro#riate to im#ort an
item from a module directly9 3hatCs 0hen a module is a 0ra##er
-,,
around a sin$le class or function9 For e8am#le6 the module
c@tringGJ contains a sin$le function called @tringGJ)*9 See
:ha#ter -* for details9% 3he sim#lest 0ay to use @tringGJ)* is to
im#ort it 0ith this line of code)
from c@tringGJ import @tringGJ
3i# &f you donCt remember the names of the functions inside a module6 use the dir)*
command in interacti7e mode to list them6 li/e this)
>>> dir)mymod-le*
Gi$ing a local nae to a odule
&n this section6 you find out t0o 0ays to $i7e a local name to a module or a moduleCs
functions) Eou can $i7e an e8tra name to a module or function6 or you can $i7e a module
a different name 0hen you im#ort it9
3his section also e8#lains the difference bet0een $i7in$ an e8tra name to a function and
callin$ the function 0hile $i7in$ a name to its result9
+ivin" an extra name to a module or function
3o $i7e an e8tra name to a function or module so that both names refer to the same
thin$%6 first ma/e sure the obFect has been im#ortedH then ty#e the ne0 name6 =6 and the
name of the function or module6 li/e this)
myf-nction = tinymod-le"tinyf-nction
Importin" a module usin" a different name
3o im#ort a module but not a function% usin$ a different name6 ty#e import6 the module
name6 as6 and the name you 0ant to use9 For e8am#le)
import tinymod-le as mymod
3i# Althou$h itCs not common6 you can also combine the from import and import as
synta86 as follo0s)
from tinymod-le import myf-nction as a$f-nction
D@M@M'@D &f you im#ort a module usin$ a different name6 the local names#ace 0onCt
/no0 the real module nameH it 0ill /no0 the module only by the name
you used to im#ort it9
D@M@M'@D 3he synta8 for $i7in$ a local name to a function is different from the
synta8 for callin$ a function and $i7in$ a name to the result9 When you
call a function6 you add #arentheses at the end of the function name)
-,*
myres-lt = tinymod-le"tinyf-nction)2*
/ules for &riting and naing odules
3he same rules a##ly to namin$ modules that a##ly to namin$ other 4ython obFects)
<y#hens and s#aces arenCt allo0ed6 and you canCt use a 4ython /ey0ord as the name9 &f
you do any of these thin$s6 youCll $et errors9
3he 4ython community uses the follo0in$ con7entions for 0ritin$ modules9 3he
con7entions ma/e 4ython code more standard and easier to read)
4odules have short, lowercase names.
4odules that exist mostly to export a sin"le class are named after the class.
&n this case6 the module and class ha7e the same name6 but the module name is
lo0ercase and the class name is ca#italized9
All import statements come at the be"innin" of a module.
The name of a non1public module 5or function, class, method, name, or
attribute6 should be"in with a sin"le leadin" underscore.
For e8am#le)
$my1onC-(licod-le
A non1#ublic name is not im#orted 0hen you ty#e from modulename import ;6
althou$h you can still im#ort it e8#licitly9 See :ha#ter -" for more about non1
#ublic names9
Warnin$ A7oid $i7in$ your module file the same name as a standard 4ython module9
Mther0ise6 the 0ron$ module mi$ht be im#orted9
(odule! odule! &here is the odule;
4ython /ee#s trac/ of module files and other files it needs in se7eral 0ays9
Mn most o#eratin$ systems6 the CP0HJ1CL0H en7ironment 7ariable lists the #aths
directories or folders% 0here 4ythonCs modules are stored9 CP0HJ1CL0H uses the same
format as the shell 7ariable CL0H9 3he format de#ends on the o#eratin$ system you are
usin$9 For e8am#le6 on ?(&O or Mac MS6 4ython mi$ht be installed in
8-sr8li(8pyt2on8 or 8-sr8local8li(8pyt2on89 Mn Windo0s it mi$ht be installed in
H!ZCrogram >ilesZCyt2on2&9
When you im#ort a module6 4ython searches #ossible module locations in the follo0in$
seGuence until 4ython finds a file 0ith that name and the suffi8 "py)
-*0
-9 3he current directory
29 3he directories listed in CP0HJ1CL0H
"9 3he directories in the 4A3< en7ironment 7ariable 0hich de#ends on your
o#eratin$ system and account setu#%
All the search #aths that 4ython /no0s are stored in the list sys"pat26 0hich is #art of
the sys module9 3his list stores these #ath names)
3he directory containin$ the in#ut scri#t or the current directory%
3he contents of the CP0HJ1CL0H 7ariable
3he default search #ath
3o modify sys"pat26 you use list methods9 Eou need to im#ort the sys module first9 3he
follo0in$ e8am#le a##ends a #ath to sys"pat2)
>>> import sys
>>> sys"pat2"append),8-fs8g-ido8li(8pyt2on,*
Warnin$ 'e careful not to remo7e im#ortant directories such as the directory in 0hich
4ython is installed6 or any subordinate directories% from the search #ath6 or
4ython may lose trac/ of its modules9
%inding &hat"s in standard odules
4ython comes 0ith a library of standard modules9 Many of these modules are co7ered in
4art &B and else0here in this boo/9
3i# &f you 0ant to find out the $ory details about any of the standard modules that 0e
donCt co7er6 see the 4ython .ibrary Deference at htt#)LL0009#ython9or$ itCs usually
also included 0hen you install 4ythonH instructions for installin$ 4ython
documentation are in A##endi8 A%9
3he built1in function dir)* returns a sorted list of the names a module defines9 3o use it6
ty#e dir follo0ed by the module name in #arentheses6 li/e this)
>>> dir)mod-lename*
3o $et hel# on a named item in a module6 ty#e 2elpH then ty#e in #arentheses% the
module name6 a dot6 and the item name9 For e8am#le)
>>> 2elp)mod-lename"f-nctionname*
-*-
*rappin" It &p in a acka"e
A #ac/a$e is a collection of related modules stored in a directory and subdirectories9 3his
section e8#lains 0hy #ac/a$es are useful6 the reGuirements for settin$ u# #ac/a$es6 and
the 0ays to im#ort #ac/a$es9
A NNnameNN is a NNnameNN, except when it's NNmainNN
Within a module6 the moduleCs name as a strin$% is stored in the $lobal name $$name$$9
'ut thereCs an e8ce#tion to this) &f the module file is run as a #ro$ram from the command
line6 4ython sets the moduleCs $$name$$ to ,$$main$$,9
3his feature is useful for includin$ code usually test code% in a module file that runs only
0hen you start the module from the command line by ty#in$ pyt2on modulename "py%9
3he code loo/s li/e this)
if $$name$$ == ,$$main$$,!
do something
When you include code that runs only under these circumstances6 you donCt ha7e to
remo7e the code 0hen you im#ort the module9 3hatCs because 0hen a module is
im#orted6 $$name$$ is the moduleCs name6 not ,$$main$$,9
3his code $oes outside any function definitions or im#ort statements in the module6
usually at the 7ery end of the module9
<ereCs an e8am#le of a small function and some test code for it9
def f)x*!
y = x;;x
ret-rn y
if $$name$$ == "$$main$$"!
print "testing""""
print "passing t2e 5al-e 2"
Q = f)2*
print "t2e f-nction ret-rns", Q
The purposes of packages
4ac/a$es are another 0ay of ta/in$ ad7anta$e of 4ythonCs ability to hide com#le8ity9
3i# 4ac/a$es ha7e some useful features)
-*2
A packa"e has its own namespace.
(ames defined 0ithin a #ac/a$e>includin$ module names>0onCt conflict
0ith names defined else0here9
acka"es can contain modules.
acka"es can contain subpacka"es.
4ac/a$es are ideal for storin$ and or$anizin$ a lot of related features9
/e3uireents for packages
3hese are the reGuirements and $uidelines for #ac/a$es)
A packa"e lives inside a directory, and its subpacka"es live inside
subdirectories of that directory 5"enerally speakin"6.
!tore the packa"e's main directory in either
o Mne of the t0o s#ecial #ac/a$e directories on your com#uter
o 3he directory of your a##lication
See IAbout s#ecial #ac/a$e directories6I later in this section9
0ust as a module file names the module, a packa"e directory names the
packa"e.
3hat allo0s you to im#ort items in the #ac/a$e by usin$ the import
packagename.modulename format9
A packa"e directory must contain a file called $$init$$"py9
&n the sim#lest case6 $$init$$"py is Fust an em#ty file9 'ut it often contains code
that initializes the #ac/a$e9 For e8am#le6 the best 0ay to im#ort s#ecific
submodules 0ith a #articular #ac/a$e or sub#ac/a$e is to load them in the
$$init$$"py file9
3he $$init$$"py file sometimes contains the $$all$$ list see ne8t item%9
Include an index of your packa"e's contents in a list named $$all$$9
3his list hel#s 4ython code run on different o#eratin$ systems9 When you include
module names in $$all$$6 the command from packagename import ; im#orts
only those modules9
3@:<(&:A.
S3?FF
&f no one 0ill e7er import ; from your #ac/a$e6 you donCt
need the list9
-*"
About special packa"e directories
When 4ython is installed6 it sets u# s#ecific directories for #ac/a$es9 &f you store
#ac/a$es in one of these s#ecial #ac/a$e directories6 youCre less li/ely to run into trouble
if your #ac/a$e names are the same as module names else0here on the 4ython #ath9
3here are t0o s#ecial directories for #ac/a$es)
li(8site7pyt2on8
3his directory is for #ac/a$es that 0or/ the same 0ay 0ith all 4ython 7ersions9
li(8pyt2onXX8site7pacAages8
3his directory is for #ac/a$es that de#end on the 4ython 7ersion9 55 chan$es to
re#resent the 4ython 7ersion number9
2xample packa"e structure
<ereCs an e8am#le structure for a #ac/a$e that handles $ra#hics9 More #ac/a$e hel# is
a7ailable at htt#)LL0009#ython9or$LdocLessaysL#ac/a$es9html9
grap2ics8 0op7le5el pacAage
$$init$$"py GnitialiQe t2e grap2ics pacAage
formats8 @-(pacAage for file format con5ersions
$$init$$"py
gifread"py
gif9rite"py
"""
effects8 @-(pacAage for grap2ics effects
$$init$$"py
specAle"py
s9irl"py
in5ert"py
"""
filters8 @-(pacAage for filters
$$init$$"py
noise"py
masA"py
"""
Iporting ites fro packages
Eou can im#ort items from #ac/a$es in se7eral 0ays9
3@:<(&:A.
S3?FF
&m#ortin$ 0ith #ac/a$es 0or/s in much the same 0ay as im#ortin$
0ith modules6 e8ce#t that you sometimes ha7e to include more le7els
of names9
-*!
Individual modules and packa"es
Eou can im#ort indi7idual modules and #ac/a$es in a cou#le of 0ays6 de#endin$ on ho0
they 0ill be used in your #ro$ram9
Importing !y %ull name
&f youCll be usin$ many #arts of a com#le8 #ac/a$e6 use the follo0in$ format to im#ort an
indi7idual item by its full name)
import grap2ics"effects"in5ert
3his ty#e of #ac/a$e im#ort statement has a cou#le of rules)
@ach name e8ce#t for the last must be a #ac/a$e9
3he last name must be a module or a #ac/a$eH it canCt be a class6 function6 or
named obFect attribute%9
3@:<(&:A.
S3?FF
3o call a function from a module im#orted by its full name6 ty#e the
full name of the module and then the function name6 li/e this)
grap2ics"filters"noise"edges)args*
Import !y using %rom
&f youCll be usin$ only a fe0 #arts of a #ac/a$e6 use the follo0in$ format to im#ort an
indi7idual item by its full name)
from grap2ics"filters"noise import edges
3i# 3his format 0or/s for im#ortin$ any of the follo0in$ obFects defined in the #ac/a$e)
submodule, subpackage, function, class, and named ob-ect.
<ere are a cou#le of e8am#les of im#ortin$ an indi7idual item)
3he follo0in$ e8am#le im#orts the noise module)
from grap2ics"filters import noise
&f you im#ort noise this 0ay6 you use this code to call its function)
noise"edges)args*
3he follo0in$ e8am#le im#orts the edges)* function)
from grap2ics"filters"noise import edges
&f you im#ort edges)* this 0ay6 you use this code to call it)
edges)args*
-*A
Import all items from a packa"e
&f youCre sure that it 0onCt cause names#ace conflicts for e8am#le6 if youCre usin$ one
small #ac/a$e in your #ro$ram and not im#ortin$ other modules or #ac/a$es%6 use the
follo0in$ format to im#ort all the items from a #ac/a$e)
from grap2ics import ;
?sin$ import ; im#orts the items in the $$all$$ list9
3@:<(&:A.
S3?FF
&f no $$all$$ list is defined6 the import ; statement im#orts these
names from the #ac/a$e)
3he #ac/a$e name
(ames found in import statements in the file
(ames of modules and sub#ac/a$es but not submodules%
(ames defined in $$init$$"py files
3he import ; statement does not im#ort any names 0ith a sin$le underscore #refi8 for
e8am#le6 $mymod-le%9
When #ac/a$es contain sub#ac/a$es6 thereCs no shortcut to refer to submodules of siblin$
#ac/a$es #ac/a$es defined at the same le7el%9 Eou need to use the full name of the
sub#ac/a$e packagename.subpackagename%9 4ython 29A has a shortcut for im#ortin$
from siblin$ modules9 3he shortcut is #art of the absolute im#ortin$ feature6 0hich is
co7ered in the follo0in$ section9
Absolute imports in ython 7.B
&n 4ython 29! and earlier6 ty#e the 0hole #ac/a$e and sub#ac/a$e hierarchy 0hen you
im#ort from a #ac/a$e for e8am#le6 from grap2ics"filters import noise%9 &f you
donCt6 someone readin$ your im#ort statement has no 0ay of /no0in$ 0hether it im#orts
another module from the #ac/a$e or a to#1le7el #ac/a$e thatCs #art of the 4ython
distribution9
3@:<(&:A.
S3?FF
Althou$h itCs #ossible to use relati7e im#orts in 4ython 29! and
earlier6 the rules are too com#licated to document in this boo/9
Absolute im#ortin$6 ne0 in 4ython 29A6 addresses this #roblem9 3hereCs no ambi$uity
about 0hich module is bein$ im#orted because 4ython searches for im#orted modules or
#ac/a$es on sys"pat2 only9 3o im#ort siblin$ modules in a #ac/a$e6 use relati7e im#orts
see the follo0in$ section%9 3o access absolute im#ortin$6 ty#e this code into your module
or into interacti7e mode)
from $$f-t-re$$ import a(sol-te$import
-*6
/elative imports in ython 7.B
When you turn on absolute im#ortin$ in 4ython 29A by usin$ from $$f-t-re$$ import
a(sol-te$import%6 ne0 synta8 is a7ailable for im#ortin$ other modules in the #ac/a$e
directory your module is in9 Follo0 these rules)
?se the fromMimport synta89
3y#e one or more dots in front of the name of the #ac/a$e or module youCre
im#ortin$ from)
o 3he first dot stands for the current moduleCs #ac/a$e9
o 3he second dot stands for the #ac/a$e containin$ the current moduleCs
#ac/a$e the #arent #ac/a$e%9
o 3he third dot stands for the #arent of the #arent6 and so on9
<ere are some relati7e im#ort statements9 &f you 0rite the s9irl"py module in the
#ac/a$e hierarchy described in I@8am#le #ac/a$e structure6I in this cha#ter%6 you
im#ort other modules in the #ac/a$e as follo0s)
3his statement im#orts s9irl"py Cs siblin$ module6 specAle"py)
from " import specAle
3his statement im#orts the gifread module)
from "formats import gifread
3his statement im#orts the edges)* function from the noise module in the
filters #ac/a$e)
from "filters"noise import edges
#hapter .$, +ettin" #lassy
(verview
3his cha#ter is about classes, a tool for creatin$ ne0 ty#es of obFects in 4ython9 When
you create a class6 you #ut data also called state% and beha7ior in one suitcase9 4ro$rams
based on these suitcases of data1#lus1beha7ior are easily customized9
4ython also uses classes to or$anize many of its o0n features9 For e8am#le6 4ythonCs
built1in data ty#es6 such as strin$s and lists6 are classes9 3he use of classes hides
com#le8ity from users 0ho donCt care about it9 Eou can use a list 0ithout 0orryin$ about
ho0 lists 0or/6 and you can #erform an action such as sortin$ a list 0ithout 0orryin$
about ho0 sortin$ 0or/s9 And if you e7er had to 0rite a sort function6 you /no0 thatCs a
$ood thin$9%
&n this cha#ter6 you find out ho0 to create and use these 4ython obFects)
-*+
:lasses6 subclasses6 and su#erclasses
:lass methods
:lass instances
:lass and instance attributes
Eou also find out 0hy classes are im#ortant to 4ythonCs modular desi$n9
Alley1((< !ome (b>ect1(riented ro"rammin"
#oncepts
4ython is a lan$ua$e that su##orts ob-ect-oriented programming OOP for short%9 &n MM4
#arlance6 ob-ects are bundles of data and actions methods% that o#erate on the data9 MM4
#ro$rams are built around such bundles9
3@:<(&:A.
S3?FF
&n contrast6 lan$ua$es such as : focus on procedural programming,
and the SchemeL<as/ell lan$ua$es focus on functional
programming. 4ython is a balanced lan$ua$e 0ith $ood su##ort for
both #rocedural and MM4 styles and some su##ort for functional
#ro$rammin$9
MbFects and functions arenCt mutually e8clusi7e>in 4ython6 methods are Fust functions
attached to obFects9 'ut considerin$ a #ro$ram in terms of dataLaction bundles is a
different a##roach than creatin$ data obFects and then usin$ se#arate actions on them9
We donCt describe obFect1oriented #ro$rammin$ in detail in this boo/6 but the follo0in$
conce#ts are im#ortant for understandin$ 4ython classes9
0b'ects
&n obFect1oriented #ro$rammin$6 an ob-ect is a thin$ that bundles data and beha7ior9
3o use a real10orld e8am#le6 ima$ine you ha7e a do$ you mi$ht not ha7e to ima$ine%9
3he do$ belon$s to a breed9
D@M@M'@D &f the do$ 0ere a 4ython obFect6 the breed 0ould be its type or
class9 As sho0n in I?sin$ multi#le inheritance6I later in this
cha#ter6 you can e7en create ImuttI ty#es9
3he do$ has characteristics associated 0ith it) its name6 a$e6 and so on9
D@M@M'@D &f the do$ 0ere a 4ython obFect6 these characteristics 0ould be its
data attributes9
3he do$ can do actions>run6 sit6 bar/6 mutilate shoes6 and so on9
D@M@M'@D &f your do$ 0ere a 4ython obFect6 these 0ould be its method
attributes. All of the attributes I$o 0ithI the do$ 0here7er it $oes9
-*,
.i/e0ise in 4ython6 if you ha7e an obFect>say6 a list>you use the name of the s#ecific
list obFect to access both
&ts data the items in the list%
3he actions that list obFects can do slice6 a##end6 and so on%
Inheriting! o$erriding! and extending
Subclasses inherit beha7iors6 abilities6 and #ro#erties from the $eneral parent% classes
they are based on9 For e8am#le6 .abrador Detrie7ers are a subclass of all do$s and inherit
the #ro#erties and abilities of do$s9
When a subclass chan$es the 0ay a #arent class 0or/s or chan$es its data attributes6 the
subclass is overriding the #ro#erties of the #arent class9 3he .ab is $entler than some
other /inds of do$s6 so a .ab class mi$ht be said to override the $entleness Guality of
do$s in $eneral9
When your subclass includes ne0 data or methods6 it is e!tending the #ro#erties of the
#arent class9 A .ab has the built1in s/ill of retrie7in$9 A .ab class mi$ht be said to e!tend
the abilities of do$s to include retrie7in$9
3i# Mne 0ay to thin/ of inheritance is to use the #hrase is a. A .ab is a do$H that means a
.ab inherits #ro#erties and abilities of do$s9
Polyorphis and duck+typing
&n some lan$ua$es6 a sin$le function or method 0or/s only 0ith s#ecific ty#es of obFects9
&n 4ython6 many tools 0or/ 0ith more than one ty#e of obFect9 For e8am#le6 the in
/ey0ord 0hich calls the s#ecial method $$contains$$)*% acts on lists6 tu#les6 and
dictionaries>obFects of different ty#es9 3his feature is called polymorphism9
4irror, mirror on the wall, what's the ob>ect of this allC
4ython terminolo$y $ets a little confusin$ at times6 es#ecially 0ith all the different thin$s
that the 0ord ob-ect can stand for9 &n 4ython6 the 0ord ob-ect refers to any item in a
4ython #ro$ram that has its o0n stora$e s#ace>from a sim#le name to a multi1me$abyte
dictionary or any of its elements% to a function6 module6 class6 or instance9 'ut in obFect1
oriented #ro$rammin$6 the 0ord ob-ect refers to any item that combines data and
beha7ior9 3hese items alon$ 0ith other items% are also obFects in 4ython9 S#ecifically6
they are called classes%or class ob-ects% and instances or instance ob-ects%9 Any
obFections2
4ythonCs #olymor#hism is based on names9 For e8am#le6 file obFects ha7e three basic
methods named read)*6 9rite)*6 and close)*9 &f you create a class that has those three
-**
methods6 then e7en thou$h your class isnCt officially a file obFect6 you can use instances
of your class in many of the #laces that 4ython e8#ects a file obFect9 4ythonistas often
call this feature duck-typing because itCs li/e the sayin$6 I&f it loo/s li/e a duc/ and
Guac/s li/e a duc/6 it must be a duc/9I &n #ro$rammin$ terms) &f this obFect has the
attributes of a file and the read)*6 9rite)*6 and close)* methods of a file6 the #ro$ram
acce#ts it in cases 0here the data ty#e file is called for9
?ow #lass, for Instance @
&n this section6 0e e8#lain ho0 classes fit in 0ith other 4ython obFects and6 in $eneral
terms6 ho0 classes and class instances 0or/ to$ether9 3hese are the most im#ortant thin$s
to remember)
A class is a $eneral descri#tion of somethin$9
A class instance is a s#ecific e8am#le of 0hat the class describes9
&n order to do thin$s 0ith a class6 you usually need to create an instance9
#lasses! odules! and functions
4ython classes are similar to 4ython modules and functions in these 0ays)
:lasses6 modules6 and functions all contain assi$nments and statements9
'oth classes and modules usually include function definitions>code bloc/s that
be$in 0ith a def statement and #erform some action9 &n classes6 these are called
methods rather than functions9%
:lasses6 modules6 and functions all ha7e their o0n names#aces9
Eou call classes and functions by usin$ the name follo0ed by #arentheses9 :allin$
a class does one thin$) &t creates an instance of the class9
A class is like a teplate
A class is a tem#late for data and beha7ior9 Eou use the tem#late to ma/e s#ecific obFects
called instances. 3he instances share the attributes of the class9
Say you ha7e a co#y of the American =ennel :lub breed standard for a .abrador
Detrie7er9 3he A=: document is li/e a class because it describes the Gualities and
abilities a .ab has9 3he data attributes for the .ab breed>its coat Guality6 size6 eye color6
and so on>are described in the class6 and so are the method attributes>its ability to
retrie7e6 to s0im6 and so on9
A 4ython class definin$ a .abrador Detrie7er mi$ht include this code)
class 3a(!
9eig2t = <% + data attri(-te
def retrie5e)self, t2ing*! + met2od attri(-te
ret-rn)t2ing*
200
(ote that the A=: breed standard isnCt a do$6 it is Fust a descri#tion of the do$9 .i/e0ise6
in 4ython6 the class isnCt the actual obFect you mani#ulate9 3o 0or/ 0ith the class6 you
first tell the class to create an instance9 3his is 0here our e8am#le brea/s do0n>you
canCt use the A=: breed standard document to create a do$H you need a cou#le of other
do$s for that5%
Tellin" the difference between a class ob>ect and a function ob>ect
3he 0ay you create a class instance loo/s su#erficially similar to the 0ay you name the
result of a function9 &n fact6 if you donCt /no0 0hether the obFect is a class or a function6
you mi$ht not be able to tell 0hich is bein$ used by loo/in$ at the assi$nment9 :lasses
freGuently do not ta/e ar$uments6 and functions usually do6 but this isnCt uni7ersally true9%
>>> fido = 3a()* + creating a class instance
>>> y = len),mystring,* + calling a f-nction
3o determine 0hat /ind of obFect is associated 0ith a name6 use the type)* function6 li/e
this)
>>> type)fido*
#type ,instance,>
>>> type)y*
#type ,int,>
>>> type )3a(*
#type ,classo(T,>
>>> type)len*
#type ,(-iltin$f-nction$or$met2od,>
(ote that a function can return an instance9
>>> def foo)*!
""" ret-rn 3a()*
"""
>>> type)foo)**
#type ,instance,>
An instance is a copy ade fro the teplate
An instance is a s#ecific obFect6 0ith its o0n name6 that has the same attributes as the
class you use to create it9
3i# &f you $et a .ab from the animal shelter and name her 4ilot6 then your do$ is li/e an
instance of the .abrador Detrie7er class9 She has her o0n name6 but she shares the
attributes of .abrador Detrie7ers in $eneral9
So if you 0ere to describe 4ilot in 4ython code6 you 0ould ty#e this6 0hich creates an
instance pilot% of the 3a()* class)
20-
>>> pilot = 3a()*
All about class and instance attributes
@ach thin$ that you name inside a class6 0hether itCs a method6 data6 or some other /ind
of code6 is an attribute of the class9
D@M@M'@D When you create an instance of the class6 it starts 0ith the same attributes
as the class9 Eou can add or alter instance attributes after that9
3o see the 7alue of a class or instance data attribute6 ty#e the class or instance name
follo0ed by a dot and the attribute name6 li/e this)
>>> 3a("9eig2t
<%
>>> pilot"9eig2t
<%
?se the same code to see ho0 4ython internally identifies a method attribute)
>>> 3a("retrie5e
#-n(o-nd met2od 3a("retrie5e>
>>> pilot"retrie5e
#(o-nd met2od 3a("retrie5e of #$$main$$"3a( instance at %x<23%%>>
3i# MbFect1oriented #ro$rammers say that 3a( has a 0ei$ht and has a retrie5e)*
method9 See the sidebar6 IWhen (ot to ?se &nheritance6I later in this cha#ter9
4akin" and #allin" #lasses
3he follo0in$ sections co7er the synta8 of 0ritin$ a class6 callin$ a class to create an
instance6 and usin$ a method 7ia an instance9 3hese6 alon$ 0ith assi$nin$ and chan$in$
attributes discussed in the #re7ious section%6 are the most common class and instance
tas/s9
&n this section6 0e use the follo0in$ e8am#le class)
class 1ame0ag!
def $$init$$)self, myname*!
self"myname = myname
def say)self*!
print "Hello, my name is", self"myname
202
#reating a class
3o 0rite the first line of a class definition6 ty#e class6 the name of the class6 and a colon6
li/e this)
class 1ame0ag!
3i# 'y con7ention6 class names are ca#italized and ha7e embedded ca#s9 'y contrast6
function and method names are usually all lo0ercase9 3his ma/es it easier to tell at a
$lance 0hich obFect is bein$ used at a #articular #oint in a #ro$ram9
*ritin" an NNinitNN56 method
Most classes ha7e an $$init$$)* method9 3his method runs 0hen you ma/e an instance
of a class9 &t s#ecifies ho0 many ar$uments the instance needs and #uts the instance into
a /no0n initial state9
"ules
A fe0 /ey thin$s to /no0 about $$init$$)* methods)
The $$init$$)* method is a special method called an initiali/er9
&n 4ython6 s#ecial methods be$in and end 0ith t0o underscore characters9
It's conventional for the $$init$$)* method to come first in the class
definition.
The $$init$$)* method, like other methods, needs a self parameter 5which
stands for the instance6, plus any other parameters that the class needs to
create an instance.
0asi syntax
3o 0rite a basic $$init$$)* method that includes only the self #arameter6 follo0 these
ste#s)
-9 (n the line beneath your class definition header, indent four spaces.
29 Type def $$init$$)self*!
,rguments
Many classes reGuire you to #ass ar$uments 0hen you ma/e instances from them9
:reatin$ attributes for these #arameters in the $$init$$)* method lets other methods
access them easily9
3o 0rite an $$init$$)* method that includes #arameter attributes6 follo0 these ste#s)
20"
-9 (n the line beneath your class definition header, indent four spaces and type
this,
2" def $$init$$)self
"9 (n the same line, type a comma and the name of a parameter, like this,
." def $$init$$)self, myn#me
A9 /epeat !tep 7 for each parameter you want to include.
69 Type an end parenthesis and a colon, *!
Eour def statement mi$ht loo/ li/e this)
def $$init$$)self, myname*!
+9 +o to the next line and indent four spaces past the def statement.
,9 #reate an attribute for the first parameter by typin" self6 a dot, the name of
the attribute, an e=uals si"n, and the name of the parameter, like so,
=" def $$init$$)self, myname*!
/%" self.myn#me ! myn#me
--9 /epeat !tep E for each parameter in the def statement.
*ritin" more methods
3he $$init$$)* method usually $i7es the class only some initial information9 Most
classes also include methods that #erform actions9 Eou create methods by usin$ the def
statement6 e8actly the 0ay you 0rite a function9
3o 0rite a method6 follo0 these ste#s)
-9 4akin" sure to indent A spaces, type def6 the name of the method, and )self6
like this,
2" def say)self
3"
!9 5(ptional6 Type any additional parameters the method re=uires. !eparate
parameters with commas.
DonCt use attributes as #arameters>you access them throu$h self9
A9 2nd the line by typin" *!9
69 (n the next line, indent four more spaces and type the code for the function.
&f your code references class or instance attributes6 be sure to use the self"name
synta89
3his e8am#le code includes only the self #arameter9
def say)self*!
print "Hello, my name is", self"myname
ure selfishness
20!
When you 0or/ 0ith 4ython classes6 you encounter the name self a lot9
3he easiest 0ay to understand self is this) self stands for the instance that you create
from a class9 For e8am#le6 if you create an instance named pilot from the class named
3a()*6 self stands for pilot9
When you are 0ritin$ a class and you define a method in that class6 you need to e8#licitly
include the self #arameter in the method definition6 as in the e8am#le belo09 Mther0ise
the method 0ouldnCt ha7e any 0ay of /no0in$ 0hich instance to use9
class 1ame0ag!
def $$init$$)self, myname*!
self"myname = myname
When you $i7e a name to a 7alue in a method definition6 you use self" in front of the
name9 3he line self"myname = myname ma/es the myname #arameter automatically
a7ailable to other instance methods9 3hat means you can call those methods 0ithout
#assin$ myname as an ar$ument9 (ote the follo0in$ code)
def say)self*!
print "Hello, my name is", self"myname
&f this 0ere a function6 you 0ould ha7e to #ass myname as an ar$ument or else 4ython
0ould com#lain that myname isnCt defined9 'ut because itCs a method and youC7e already
defined self"myname in the $$init$$ statement6 the method can fi$ure out the 7alue of
myname by usin$ the self obFect9
3he self #arameter also ma/es life easier 0hen you create an instance9 4ython
automatically #asses the self ar$ument for you9 For e8am#le6 if you create an instance of
1ame0ag)*6 you only ha7e to $i7e it the 7alue of myname9 &t automatically uses the name
of the instance as the 7alue for self9
Static and class methods of ne01style classes 0or/ differently9 See the :ha#ter -! for the
lo01do0n on ne01style classes9
#reatin" other class attributes
Eou can #ut any /ind of code or assi$nment into a class9 Anythin$ inside a class that you
$i7e a name to becomes an attribute of the class9
For e8am#le6 0e could add a fa5orite$color attribute to our (ame3a$ class)
class 1ame0ag!
f#$orite_color ! %&l"e%
20A
3hen if 0e create an instance of the class6 the instance also has this attribute)
>>> grail$seeAer = 1ame0ag),lancelot,*
>>> grail$seeAer"fa5orite$color
,Il-e,
#reating an instance
3o create an instance of a class 0hich is also /no0n as calling a class%6 you $i7e it a
name6 and then ty#e the class name6 #arentheses6 and any #arameters9 Follo0 these ste#s)
-9 Type a name for the instance, followed by an e=uals si"n, like this,
2" 2ello =
"9 Type the class name and ( as shown,
." 2ello = '#me(#g(
A9 If your class re=uires parameters, include values or names for them. !eparate
parameters with commas.
&f you call the 1ame0ag class created in the #re7ious section6 you need to #ass a
7alue for one #arameter6 myname9
2ello = 1ame0ag),)rth"r,
69 Type *9
3he com#leted code loo/s li/e this9 3he instance name is 2ello9
2ello = 1ame0ag),Lrt2-r,*
#alling a ethod $ia an instance
After you ha7e created an instance by usin$ code li/e instance$name =
Hlass1ame)*%6 you can #erform the actions defined by the classCs methods9
D@M@M'@D 3o call a method 7ia an instance6 ty#e this on one line)
3he name of the instance
A dot
3he name of the method
A #air of #arentheses
4ass any needed ar$uments inside the #arentheses9
For e8am#le6 to call the retrie5e)* method of the pilot instance6 ty#e this code)
>>> pilot"retrie5e)"(all"*
,(all,
D@M@M'@D Eou canCt use this code to call the method by usin$ the name of the class
206
because the class is only a tem#late for the instance obFect9 An A=: breed
standard document canCt retrie7e a ball>only the actual do$ can9 <ereCs
0hat ha##ens if you try to call the method by usin$ the name of the class)
>>> 3a("retrie5e)"(all"*
0race(acA )most recent call last*!
>ile "#stdin>", line /, in V
0ype6rror! -n(o-nd met2od retrie5e)* m-st (e called 9it2 3a(
instance as first
arg-ment )got str instance instead*
3@:<(&:A.
S3?FF
3here is actually a 0ay to call a method by usin$ the name of a class9
3his is /no0n as calling an unbound method9 &tCs mostly an ad7anced
#ro$rammin$ techniGue6 but itCs also used to e8tend methods in
subclasses see I@8tendin$ su#erclass methods6I later in this
cha#ter%9 'riefly6 you create an unbound method by usin$ the name
of the class instead of the name of the instance for e8am#le6
3a("retrie5e rather than pilot"retrie5e%9 3o call it6 you #ass it
an instance name alon$ 0ith any other ar$uments the method
reGuires>for e8am#le6 3a("retrie5e)pilot, "(all"*9
*here did self "oC
Does somethin$ seem to be missin$ in the code for callin$ a class2 3he $$init$$)*
method in the class 1ame0ag has t0o #arameters>self and myname9 Why do you need to
#ass only one #arameter 0hen you create a class instance2
3he ans0er is that self is a s#ecial #arameter that essentially #asses itself Ibehind the
scenesI 0hen you use the class instance9 So you donCt ha7e to #ass self in this statement
althou$h there are some cases in 0hich you do%9 &n fact6 if you do #ass it in this
statement6 you $et an error)
>>> 2ello = 1ame0ag)self, ,Lrt2-r,*
0race(acA )most recent call last*!
>ile "#stdin>", line /, in V
1ame6rror! name ,self, is not defined
20+
+ettin" Inside the 'actory, How #lass and Instance
?amespaces Interact
A class and an instance each ha7e a names#ace6 0hich is stora$e for the names of obFects
that 4ython /no0s about9 When you create an instance of a class6 the instance has its o0n
names#ace6 but it also shares the classCs names#ace9
Whene7er 4ython encounters a name thatCs #art of an instance6 it loo/s for the 7alue first
in the instanceCs names#ace9 &f it canCt find the 7alue there6 it loo/s in the classCs
names#ace9
3i# Function names#aces and $lobal names#aces interact in similar 0ays9 See :ha#ter
--9
So anythin$ that you do to the class names#ace such as chan$in$ an attribute of the
class% is also reflected in the instance>e8ce#t 0hen the instance has already o7erridden
that 7alue from the class names#ace9 3he ne8t section sho0s ho0 it 0or/s9
#hanging the $alues of class and instance attributes
Eou can chan$e the 7alues of a classCs attributes9 When you create an instance of your
class6 you can also chan$e or override% the 7alues of any class attributes it inherits9 3he
effect these chan$es ha7e de#ends on 0hat data ty#es the attributes are6 ho0 you chan$e
the 7alues6 and in 0hat order you chan$e the 7alues9
When 0e first define our 3a( instance6 pilot6 it has the same 7alue for 9eig2t that the
3a( class defines)
>>> 3a("9eig2t
<%
>>> pilot"9eig2t
<%
#han"in" the value of the class attribute
3o chan$e the 7alue of the class attribute6 ty#e the class name6 a dot6 the attribute name6
=6 and a ne0 7alue6 li/e this)
>>> 3a("9eig2t = &%
Warnin$ :han$in$ a classCs attributes in code outside the class and es#ecially outside
the classCs module% ma/es your code hard to read and to debu$9
20,
#han"in" the value of the instance attribute
3o chan$e the 7alue of the instance attribute6 ty#e the instance name6 a dot6 the attribute
name6 =6 and a ne0 7alue6 li/e this)
>>> pilot"9eig2t = .&
2ffects of chan"in" attribute values
3he effects of chan$in$ attribute 7alues are as follo0s)
&f you chan$e the class attribute first6 the instance 7alue also chan$es)
>>> 3a("9eig2t = &%
>>> pilot"9eig2t
&%
&f you chan$e the instance attribute first6 the class 7alue does not chan$e)
>>> pilot"9eig2t = .&
>>> 3a("9eig2t
<%
Warnin$ &f the attribute is a mutable data ty#e6 such as a list6 and you use a
method that chan$es the attribute in #lace such as the append)* method
of lists>see :ha#ter ,%6 then chan$in$ the instance attribute does
chan$e the class attribute9 'e sure to o#erate only on co#ies of mutable
instance attributes or a7oid usin$ chan$e1in1#lace methods on them9
;i7in$ a ne0 7alue to the instance attribute overrides the 7alue it ori$inally $ets
from the class9 So if you chan$e the class attribute after you chan$e the instance
attribute6 the instance attribute does not chan$e9
>>> pilot"9eig2t
.&
>>> 3a("9eig2t = /%%
>>> pilot"9eig2t
.&
Adding an attribute to an instance
&n IMa/in$ and :allin$ :lasses6I earlier in this cha#ter6 0e describe ho0 to add an
attribute to a class6 and the #recedin$ section describes ho0 to chan$e the 7alue of a class
or instance attribute9 Eou can also add a data attribute and 7alue% to an instance that
youC7e created9 Just 0rite code li/e this)
>>> pilot"coat$color = "yello9"
D@M@M'@D 3his attribute is stored in the instanceCs names#ace6 so the class 0onCt
/no0 about this attribute6 as illustrated here)
>>> 3a("coat$color
0race(acA )most recent call last*!
>ile "#stdin>", line /, in V
Lttri(-te6rror! class 3a( 2as no attri(-te ,coat$color,
20*
#lass and Instance #onventions
4ython #ro$rammers use some im#ortant con7entions 0hen 0ritin$ and im#lementin$
classes9 4ython 0ill understand your code if you donCt use them6 but humans 0ho ha7e to
read your code and fi$ure out 0hat it does 0ill a##reciate your usin$ them9 Some
con7entions are as follo0s)
Modules that e8ist mostly to e8#ort a sin$le class should be named after the class9
:lass names be$in 0ith an u##ercase letter9 Attribute and method names be$in
0ith lo0ercase letters9
3he name self is used to refer to the name of a class instance9
3@:<(&:A.
S3?FF
A brief privacy note
When youCre 0ritin$ a class attribute6 you s#ecify that it is private by
namin$ it 0ith a sin$le leadin$ underscore9 ?nli/e #ri7ate attributes
in modules and #ac/a$es6 ho0e7er6 4ython doesnCt treat #ri7ate
attributes in classes in any s#ecial 0ay9 3he sin$le leadin$ underscore
is Fust a con7ention that says to #eo#le readin$ your code6 IdonCt
touch this5I 3hat is6 donCt try to re0rite it6 chan$e it6 or use it in any
0ay6 or else you mi$ht brea/ somethin$9%
4ri7ate attributes are most often used in lar$e class hierarchies but
you should use them any time you 0ant to inform users of your class
that this is not #art of the #ublic interface%9
&f you name a class attribute 0ith t$o leadin$ underscores6 that tells
4ython to use name mangling so that base classes and subclasses
canCt see the attribute unless they manually do the name man$lin$
themsel7es%9 3his is an ad7anced #ro$rammin$ tool because itCs hard
to use 0ith inheritance9
Attributes 0ith t0o leadin$ and t0o trailin$ underscores li/e
$$getattr$$% arenCt #ri7ate9 3hey are s#ecial methods discussed
earlier in this cha#ter%9
Some MM4 #urists deni$rate 4ythonCs #ri7acy mechanisms because
the #ri7acy mechanisms donCt enforce #ri7acy9 4ythonistas counter
that anyone 0ho really 0ants to brea/ #ri7acy can do it6 and itCs more
im#ortant to document thin$s>and then $et on 0ith your 0or/9 &n
$eneral6 4ythonistas say6 I4ython is a lan$ua$e for consentin$
adults9I 3hat is6 4ython usually 0onCt #re7ent you from doin$
somethin$ stu#id or com#le8 if you really 0ant to9%
2-0
Inheritin" the 'arm, (verridin" and 2xtendin" #lasses
Eou can chan$e the features of a class or create ne0 features by creatin$ a subclass or
child class% that inherits attributes from its #arent class and then chan$es some of the
attributes inherited from the #arent class9
3@:<(&:A.
S3?FF
:lasses that subclasses inherit from are called parent classes,
superclasses6 or base classes.
#reating a subclass
3o create a class that inherits from another class6 you ty#e class6 the name of the
subclass6 and then6 in #arentheses6 the name of the su#erclass the subclass inherits from9
3o create a subclass of the 1ame0ag class 0e 0or/ 0ith earlier6 you use a class statement
li/e this)
class SeeA1ame0ag)1ame0ag*!
3i# 4ython 29A adds a ne0 feature) classes 0ithout base classes can use em#ty
#arentheses the same 0ay functions can ha7e no ar$uments%9 3hat is6 you donCt ha7e
to s#ecify a su#erclass in the #arentheses9 For e8am#le6 you could Fust ty#e class
H)*!
&n $eneral6 0hen you create a subclass6 you create only 0hat you 0ant to o7erride or
e8tend about the su#erclassCs beha7ior9 So you donCt need to 0rite an $$init$$)*
statement unless you 0ant to ma/e chan$es to the su#erclassCs $$init$$)* statement9
0$erriding superclass ethods
3o create a subclass that o7errides a method of a su#erclass6 you 0rite a method 0ith the
same name as the su#erclass method you 0ant to o7erride6 and you $i7e this method
different beha7ior9
&n the follo0in$ e8am#le6 the subclass defines a say)* method that beha7es differently
from the su#erclassCs say)* method9
class SeeA1ame0ag)1ame0ag*!
def say)self*!
print "02e c-rrent 5al-e of my name is", self"myname
3i#
*hen not to use inheritance
&n some cases6 0ritin$ a subclass that inherits from a su#erclass isnCt the best
solution9 Su##ose you are #lannin$ a conference6 and you created a Begistration
class9 Eou mi$ht thin/ of ma/in$ Begistration inherit from 1ame0ag so you can
2--
use the say)* method9 'ut itCs better to ma/e a nameta$ attribute for the re$istration
class that uses 1ame0ag as a 7alue6 li/e this)
class Begistration!
def $$init$$)self, name*!
self"name = name
self"nametag = 1ame0ag)name*
?sin$ an attribute that refers to an instance of a different class is called ob-ect
composition9 Mne 0ay to describe 0hatCs $oin$ on in the abo7e e8am#le is to say that
an instance of the Begistration class has an attribute that is an instance of the
1ame0ag class9 3o #ut it in material10orld terms6 if you #roduce a bunch of
re$istration #ac/ets for a conference6 each #ac/et is li/e an instance of the
Begistration class6 and each #ac/a$e has a name ta$ in it6 0hich is an instance of
the 1ame0ag class9
<ereCs 0hat ha##ens 0hen you create an instance of the class and the subclass and call
each instanceCs say)* method)
>>> s-perclass$inst = 1ame0ag),3-cy,*
>>> s-(class$inst = SeeA1ame0ag),3in-s,*
>>> s-perclass$inst"say)*
Hello, my name is 3-cy
>>> s-(class$inst"say)*
02e c-rrent 5al-e of my name is 3in-s
,xtending superclass ethods
3here are t0o basic 0ays to e8tend su#erclass methods9 Eou can
@8tend a method that already e8ists in the su#erclass
Write a ne0 method that uses data from a su#erclass method
2xtendin" an existin" superclass method
@8tendin$ an e8istin$ method of a su#erclass is similar to o7erridin$6 e8ce#t that you
also call the su#erclass method in the e8tended code9 Eou e8tend a su#erclass method if
you 0ant the method to #erform as 0ritten and you also 0ant to mani#ulate its result in
some 0ay9
3o create a subclass that e8tends the methods of a su#erclass6 follo0 these ste#s)
-9 :efine a method with the same name as the superclass method you're
extendin".
2" class WeirdSreeting)1ame0ag*!
2-2
3" def say)self*!
!9 In that method's code, call the superclass method by typin" the super1class
name, a dot, the superclass method name, and its ar"ument in parentheses.
&" class WeirdSreeting)1ame0ag*!
<" def say)self*!
E" '#me(#g.s#y(self)
,9 erform some additional manipulations, either on the value returned by the
superclass method or on the subclass's instance data.
3he follo0in$ subclass e8tends the say)* method of 1ame0ag)
class WeirdSreeting)1ame0ag*!
def say)self*!
1ame0ag"say)self*
print self"myname"s9apcase)*
Eou mi$ht call it li/e this)
>>> greet = WeirdSreeting),0otoro,*
>>> greet"say)*
Hello, my name is 0otoro
tJ0JBJ
Addin" a new method
Eou 0rite a ne0 subclass method if you 0ant the subclass to add some com#letely ne0
beha7ior to a #arent class9 3he follo0in$ e8am#le creates a ne0 subclass of 1ame0ag and
also creates a method on the subclass that e8tends the #arent class by sa7in$ the 1ame0ag
data in a database9 Note: 3his code is incom#leteH it 0or/s only if a database file has
been created and o#ened9%
class Cersistent1ame0ag)1ame0ag*!
def sa5e)self*!
d("9rite)picAle"d-mps)self**
)sing ultiple inheritance
A class 0ith more than one su#erclass inherits its attributes from all its su#erclasses9 Such
a class uses multiple inheritance9
D@M@M'@D 3o 0rite a subclass that uses multi#le inheritance6 ty#e the names of more
than one su#erclass in a class statement9 For e8am#le)
class @-(Hlass1ame)@-perHlass/, @-perHlass2*!
And hereCs ho0 you 0ould create a ImuttI ty#e of do$ this do$ is #robably easier to
create as a 4ython class than in real life%)
class HeinQ&EOog)H2i2-a2-a, Srey2o-nd, astiff, Oalmatian*!
2-"
2aespace searching in classes and superclasses
Subclasses ha7e access to the names#aces of all the classes they inherit from9 When a
subclass inherits from #arent classes6 a names#ace hierarchy is created9 When other
classes not direct #arents of the subclass% $et into the act6 the names#ace structure is
called a frame$ork9
When you are 0or/in$ 0ith classes6 4ython searches for names in the follo0in$ order
and sto#s searchin$ as soon as it finds the name)
-9 &nstance
29 :lass
"9 Su#erclasses that the class inherits from
3i# 3his is similar to ho0 function and module names#aces 0or/>4ython searches the
functionCs local names#ace first6 and then the moduleCs $lobal names#ace6 and then
the main or built1in names#ace9
3his $i7es rise to the follo0in$ beha7iors)
Inheritin" from a sin"le superclass
3he names and definitions in a subclass or its instance o7erride the same names and
definitions in the su#erclass9
Inheritin" from multiple superclasses
When a class has more than one su#erclass6 4ython searches first the instance6 and then
the subclass6 as usual9 When 4ython starts searchin$ the su#erclasses6 it uses a method
called depth-first, left-to-right searching6 0hich searches in the follo0in$ order)
-9 .eftmost su#erclass in the class statement
For e8am#le6 in the class statement
0otallyIiQarreSreeeting)WeirdSreeting, 1ame0ag*6 searchin$ 0ould
be$in 0ith WeirdSreeting6 the leftmost su#erclass named9
29 Subclasses of the leftmost class
"9 (e8t class to the ri$ht in the class statement
!9 Subclasses of this class
A9 And so on S
<ere is a sim#le e8am#le)
class L!
def foo)self*!
2-!
print "foo called in L"
class I!
def foo)self*!
print "foo called in I"
def (ar)self*!
print "(ar called in I"
class H)L,I*!
pass
&f you im#ort the #recedin$ e8am#le into interacti7e mode6 hereCs 0hat you $et 0hen you
create an instance of H and use it9
-9 3he instance of H loo/s in class L first and finds the foo)* method there9
&t e8ecutes that and ne7er sees ICs foo)* method9
29 When you call the (ar)* method6 the instance of H doesnCt find it in class L6 so it
loo/s in class I9
3his e8am#le sho0s the result)
>>> x = H)* + Hreate an instance of H
>>> x"foo)* + Hall t2e foo)* met2od
foo called in L
>>> x"(ar)* + Hall t2e (ar)* met2od
(ar called in I
0perator interception and o$erloading
Eou can chan$e the 0ay 4ython o#erators 0or/ in your classes9 3his a##lies both to
e8#ression o#erators such as x ; 3% and o#erations on obFects #rintin$6 callin$ a
function6 and so on%9 &nheritance is one tric/ that ma/es this #ossible6 but 4ython also has
some s#ecial tools to hel#)
Hookin" into the power of special methods
Methods 0ith names that be$in and end 0ith t0o underscores>s#ecial methods>are
also called hooks. 3hey ha7e s#ecial beha7iors9 When you use an o#erator such as :6
4ython calls the s#ecial method $$add$$ behind the scenes9 &f you im#lement the
$$add$$)* method for your class6 4ython 0ill call it each time you use the : o#erator
0ith instances of your class9
3i# 3hese methods are #ro7ided so you can ma/e your o0n class instances 0or/ the
same 0ay 4ythonCs built1in ty#es do9 &tCs $ood #ractice not to chan$e the beha7ior of
the o#erators in 0ays users and readers of your #ro$ram donCt e8#ect such as
chan$in$ $$add$$ so that it subtracts5%9
2-A
3able -"1- lists some of the o#erator o7erloadin$ methods9 3o see them in the
documentation for most of 4ythonCs built1in data ty#es6 ty#e 2elp)data_type*6
substitutin$ the name of the data ty#e you need hel# 0ith>for e8am#le6 2elp)list*9
3able -"1-) Some :ommonly ?sed S#ecial Methods
M#en table as s#readsheet
4ethod &sed 'or !yntax
$$add$$)* : U : P
$$eN$$)*6 $$neN$$)* and
others
:om#arin$ U == P6 U != P6 and so on
$$getattr$$)*
.oo/in$ u#
U"attri(-te
$$getitem$$)*
Detrie7in$ an inde8 or
/ey
U[Aey]6 U[index]6 U in P6 used in
loo#s and tests
$$init$$)*
&nitializin$ obFects
yHlass)*
$$iter$$)*
:reates iterables iter)U*6 for i in U!
$$repr$$)*
4rintin$6 con7ertin$ print U6 U
$$setattr$$)*
Assi$nin$ a 7alue to an
attribute
U"attr$name = 5al-e
$$setitem$$)*
Assi$nin$ an inde8 or
/ey
U[Aey] = 5al-e
U[index] = 5al-e
$$str$$)*
:on7ertin$ obFects to
strin$s
str)U*
For a com#lete list of s#ecial methods6 see IS#ecial method namesI in the 4ython
.an$ua$e Deference) htt#)LL0009docs9#ython9or$LrefLs#ecialnames9html9
!uper strin"s
3he follo0in$ e8am#le class includes a method that o7errides 4ythonCs builtin
$$str$$)* method9 &t chec/s 0hether one or t0o names 0ere #assed and returns the
names in first nameRlast name order)
class 6mployee!
def $$init$$)self, lastname, firstname=1one*!
self"lastname = lastname
self"firstname = firstname
def $$str$$)self*!
if self"firstname!
ret-rn "Ds Ds" D )self"firstname, self"lastname*
else!
ret-rn self"lastname
2-6
When you create instances of this class and #rint them6 the beha7ior of str)* chan$es9
3he print command calls str)object*9%
>>> a = 6mployee),La2Q,*
>>> ( = 6mployee),ar-c2,, ,@tef,*
>>> c = ,ar-c2, @tef,
>>> print a
La2Q
>>> print (
@tef ar-c2
>>> print c
ar-c2, @tef
*hen to +o to #lass
Eou mi$ht benefit from creatin$ classes in your #ro$ram in the follo0in$ situations)
!everal functions use the same parameters.
&f your #ro$ramCs functions #ass a lot of ar$uments bac/ and forth6 consider
creatin$ a class and ma/in$ the functions into methods9 Methods can access their
data throu$h the instance self%6 so you donCt ha7e to #ass the data9
Eou could do this by usin$ $lobals6 but thereCs more of a dan$er that thin$s 0ould
$et chan$ed 0hen you donCt 0ant them to9 Furthermore6 $lobals donCt allo0 you to
create multi#le instances9
%our pro"ram carries out the same operation multiple times.
:lasses are ideal for this #ur#ose because each instance you create is se#arate
from the others6 and you donCt ha7e to reset e7erythin$ to #erform the o#erations
a$ain9
%our pro"ram is event1driven.
An e7ent1dri7en #ro$ram often has a function that collects e7ents user actions%
and sends them to other functions to be #rocessed9 &f this e7ent function is a
method of an Lpplication class6 a##lication information doesnCt ha7e to be
#assed as ar$uments9
%our pro"ram uses a persistent database.
:lass instances can be stored on dis/ by usin$ the picAle or s2el5e modules9
See :ha#ter -*9% 3he picAle feature creates an e8ternal file and sa7es an obFect
in it9 picAle can also create a strin$ suitable for storin$ in a database9 3he s2el5e
feature I#ic/lesI obFects by usin$ a database that you access 0ith /eys9
2-+
%ou want a data type that bundles several items.
An em#ty class definition can store data attributes9 For more information6 see
:ha#ter 2-9
#hapter .A, Introducin" ?ew1!tyle
#lasses
&f youCre ne0 to 4ython and you ha7e read :ha#ter -" on classes6 you mi$ht be $roanin$
at the #ros#ect of yet another cha#ter on classes9 Actually6 almost e7erythin$ in :ha#ter
-" also a##lies to the ne01style classes 0e discuss in this cha#ter9 (e01style classes
sim#lify 4ythonCs obFect hierarchy in 0ays that sa7e you effort if you 0ant to ma/e
chan$es to a built1in data ty#e9 3hey come 0ith some ne0 features that are of interest
mostly in ad7anced #ro$rammin$9 We discuss them here so youCll reco$nize them in
#ro$rams9
3his cha#ter #ro7ides an introduction to ne01style classes and their features9 Eou also
find out about method resolution order and t0o features you shouldn3t use>static
methods and the $$slots$$ class attribute9
An (b>ect's (b>ect, Intro to ?ew1!tyle #lasses
3he #rimary difference bet0een a classic or old-style% class and a ne$-style class is that
a ne01style class al0ays inherits from o(Tect6 either directly class
myclass)o(Tect*!% or indirectly by inheritin$ from one of 4ythonCs built1in ty#es
class myclass)dict*!%6 0hich inherits from o(Tect9
3he ori$inal #ur#ose of ne01style classes 0as to allo0 subclasses based on built1in
4ython data ty#es6 such as dict and list9 3hey also fi8 some #roblems 0ith classic
classes and add a bunch of ne0 features such as #ro#erties%9
(e01style classes 0ere introduced in 4ython 292H ho0e7er6 they 0ere some0hat
e8#erimental in 2926 and subtle but si$nificant chan$es 0ere made in 4ython 29"9 &n case
youCre curious6 most of the chan$es in7ol7ed multi#le inheritance features9% &f youCre
usin$ 4ython 2926 stic/ 0ith classic classes9
#lassic #oke or ?ew #okeC
3here is a contro7ersy in the 4ython community about ne01style classes9 Some #eo#le
say you should use them for all the classes in your code9 Mthers say you should use them
only 0here a re$ular class 0onCt do9 3he contro7ersy is based on the follo0in$ facts)
/e"ular classes will "o away in ython $.K.
2-,
(ot to 0orry>the 7ast maFority of 4ython code 0ritten 0ith re$ular classes 0ill
still 0or/ because classic class code isnCt that different from ne01style class code9
ython 7.. and earlier don't support new1style classes at all, and new1style
classes work differently in 7.7 than in 7.$ and later.
So if you use ne01style classes6 your code mi$ht ha7e #roblems runnin$ in
4ython 292 and earlier9
We focus on classic classes in this boo/ because theyCre still the default and 0ill remain
the default until 4ython "90H in addition6 the 7ast maFority of code in e8istence includin$
the 4ython library% still uses classic classes9
'efore 4ython 2926 data ty#es could be created only by usin$ the : #ro$rammin$
lan$ua$e6 and classes couldnCt inherit from built1in or third1#arty data ty#es9 &f you
0anted a ne0 obFect that 0or/ed /ind of li/e a 4ython dict6 for e8am#le6 you had to 0rite
the 0hole thin$ from scratch9 Eou couldnCt 0rite a subclass based on dict because : ty#es
0ere 7ery different from 4ython classes s#ecifically6 classes 0ere all of the class ty#e6
and class instances 0ere all of the instance ty#e%9 4ython 292 introduced the type;class
unification in the form of ne01style classes9
&n the ne01style class tutorial found at htt#)LL0009#ython9or$6 ;uido Ban Dossum
4ythonCs creator% #ro7ides an e8am#le of a ne01style class that e8tends the dict ty#e by
#ro7idin$ a default 7alue 0hen a /ey is missin$)
class defa-ltdict)dict*!
def $$init$$)self, defa-lt=1one*!
dict"$$init$$)self*
self"defa-lt = defa-lt
def $$getitem$$)self, Aey*!
try!
ret-rn dict"$$getitem$$)self, Aey*
except Xey6rror!
ret-rn self"defa-lt
3he documentation for ne01style classes hasnCt yet been inte$rated 0ith the rest of the
official 4ython documentation at htt#)LL0009#ython9or$9 <ereCs 0here to find
information about ne01style classes)
htt#)LL0009#ython9or$LdocLne0styleL
,$erything coes fro ob'ect
3he basic rule for a ne01style class is that it has to inherit from o(Tect9 We /no0 0hat
youCre thin/in$) IMh no6 another use of the term ob-ect5I% &n this case6 o(Tect is a built1in
2-*
ty#e9 All the 4ython built1in data ty#es are really subclasses of o(Tect9 &f you ty#e
2elp)$$(-iltin$$* into the 4ython inter#reter6 #art of 0hat you see is a hierarchy of
classes6 startin$ 0ith o(Tect)
Help on (-ilt7in mod-le $$(-iltin$$!
1L6
$$(-iltin$$ 7 I-ilt7in f-nctions, exceptions, and ot2er o(Tects"
[""""]
H3L@@6@
o(Tect
(asestring
str
-nicode
(-ffer
["""]
(ethods of inheritance
3here are t0o 0ays to set u# inheritance for a ne01style class9 Eou can inherit directly
from obFect6 or you can inherit from a built1in data ty#e9
Inheritin" from ob>ect
3o 0rite a class that inherits directly from o(Tect6 start 0ith this line)
class yHlass)o(Tect*!
3o ma/e all classes in a sin$le module ne01style classes6 #ut this statement at the to# of
the module)
$$metaclass$$ = type
Inheritin" from a built1in data type
Eour class can inherit from a built1in data ty#e 0hich inherits from o(Tect%9
?se the follo0in$ method if you 0ant somethin$ that 0or/s a lot li/e a builtin data ty#e
but has a fe0 differences li/e ;uidoCs defa-ltdict e8am#le%9
Warnin$ Some functions return a s#ecific data ty#e9 &f you #ass an instance of your
subclass to a function that returns a s#ecific data ty#e6 it may return an instance
of a built1in ty#e rather than an instance of your subclass9
&n this e8am#le6 0e create a subclass of list and an instance of that subclass9 When 0e
use the append)* method on the instance6 the chan$ed instance still belon$s to the
subclass 0e created9 'ut 0hen 0e use the list)* function 0ith the instance6 it returns an
obFect of the list ty#e9
220
>>> class y3ist)list*! pass
"""
>>> ell = y3ist)*
>>> ell"append)/*
>>> ell
[/]
>>> type)ell*
#class ,$$main$$"y3ist,>
>>> ell2 = list)ell*
>>> ell2
[/]
>>> type)ell2*
#type ,list,>
?ew Improved #lass 'eatures
(e01style classes #ro7ide se7eral enhancements o7er old1style classes)
The $$getattri(-te$$)* method
A new return value for type)*
The s-per)* function
The property type
#lass methods, includin" the $$ne9$$)* class method
!tatic methods
The $$slots$$ class attribute
Getcher attributes4
With classic classes6 one of the tools of subclassin$ is the s#ecial method
$$getattr$$)*6 0hich returns an attribute of an obFect9 (e01style classes add the
s#ecial method $$getattri(-te$$)*9 3he difference bet0een $$getattr$$)* and
$$getattri(-te$$)* is that $$getattr$$)* is called only if an attribute isnCt found6 but
$$getattri(-te$$)* is called for all attributes9
.ey! baby! &hat"s your type;
(e01style classes $i7e you more s#ecific information about the instances you create from
a class)
&f you create an instance of an old1style class6 the 7alue returned by
type)myinst* is #type ,instance,>9
&f you create an instance of a ne01style class6 the 7alue returned by
type)myinst* is the same as the instance attribute myinst"$$class$$9
22-
#alling the right superclass ethod
3he s-per)* function #ro7ides a 0ay to $et at a subclassCs su#erclasses 0hen you are
usin$ multi#le inheritance9 Eou usually use it in a method to call a method in the
su#erclass9 3o use it6 ty#e s-per)classname, objectname*9 &t returns a bound su#erclass
obFect9
<ereCs ho0 to use the s-per)* function to re1create the e8am#le 1ame0ag class code in
:ha#ter -"9 (ote that you must con7ert 1ame0ag to a ne01style class first9% 3he only
difference is that you use s-per)* instead of e8#licitly usin$ the 1ame0ag class9 (otice
that 0hen you use s-per)*6 the class name ar$ument must be the current class9
3i# ?sin$ s-per)* ma/es it easier to chan$e the name of a #arent class9 Eou only need
to chan$e the class definition6 not all the su#erclass calls in the methods9
@8tended method usin$ classic% 1ame0ag su#erclass)
class WeirdSreeting)1ame0ag*!
def say)self*!
1ame0ag"say)self*
print self"myname"s9apcase)*
@8tended method usin$ the ne01style 1ame0ag class and s-per)* function)
class WeirdSreeting)1ame0ag*!
def say)self*!
s"per(*eird+reeting, self).s#y()
print self"myname"s9apcase)*
3he s-per)* function has some limitations)
It is a ooperative function, @ach #arent class that defines the method must also
call s-per)*9
It works simply only when all methods have the same signature: 3hat is6 they
must ha7e the same #arameter lists9 &t is #ossible to use s-per)* 0hen methods
ha7e different si$natures6 but difficult9%
3he traditional 0ay of callin$ a su#erclassCs method is to use the name of the su#erclass
you 0ant 0hen callin$ the method)
x = y@-perclass"its$met2od)*
That"s y property4
Properties are attributes that you mana$e by usin$ 7arious get and set methods6 so they
are also called managed attributes. 'efore ne01style classes came alon$6 the only 0ay to
222
customize attribute mana$ement 0as to o7erride $$getattr$$ and $$setattr$$9 3his
0as tric/y and caused #erformance #roblems9
!et up properties
3o set u# #ro#erties in your class6 follo0 these ste#s)
-9 :efine a new1style class and an $$init$$)* method.
?sually6 you set u# a #ri7ate attribute self"$x in this e8am#le% because itCs only
used behind the scenes by the methods9 See :ha#ter -" for more on #ri7ate
attributes9
class yHlass)o(Tect*!
def $$init$$)self*!
self"$x = %
29 :efine class methods that "et, set, or delete an attribute.
3i# &tCs a $ood idea for the names of these methods to include the 0ords get6 set6
and del so itCs clear 0hatCs $oin$ on9 For e8am#le)
def getx)self*!
print "Setting $x"
ret-rn self"$x
def setx)self, x*!
if x # %!
raise Wal-e6rror)"1egati5e 5al-es pro2i(ited! Dr" D
x*
self"$x = x
print "@etting $x"
"9 :efine a property and, in parentheses, pass the names of the methods you
defined, as in the followin" example.
(ote that you do not call the methods donCt include #arentheses after the method
names%9
3o include a doc attribute for a #ro#erty 0hich sho0s u# 0hen you as/ for hel#
on the #ro#erty%6 ty#e doc= and a strin$ inside the #arentheses9
x = property)getx, setx, doc="no negati5e x"*
22"
&se properties
3o use a #ro#erty6 create an instance of the class9 3o assi$n and retrie7e 7alues6 #retend
that the #ro#erty is a re$ular attribute>4ython automatically calls the methods for you9
3hese e8am#les sho0 ho0)
>>> my$inst = yHlass)*
>>> my$inst"x
Setting $x
%
>>> my$inst"x = /%
@etting $x
>>> foo = my$inst"x
Setting $x
>>> foo
/%
>>> print yHlass"x"$$doc$$
no negati5e x
>>> my$inst"x = 7/%
0race(acA )most recent call last*!
>ile "#stdin>", line /, in V
>ile "a"py", line //, in setx
raise Wal-e6rror)"1egati5e 5al-es pro2i(ited! Dr" D x*
Wal-e6rror! 1egati5e 5al-es pro2i(ited! 7/%
*hen only class atters
:lass methods are #assed the class of the obFect but not the obFect itself9 3heyCre useful
0hen
A method needs to /no0 only the class name and doesnCt care about any
underlyin$ data9
Eou call methods directly on the class and you 0ant to return an instance for
e8am#le6 the dict"fromAeys)* method%9
3he follo0in$ sections describe creatin$ and callin$ class methods9 Eou also find out
about the s#ecial $$ne9$$)* method9
#reatin" class methods
Eou #ut class methods in your classes by definin$ a method call it g)*% and then ty#in$
the code g = classmet2od)g*9
&n this e8am#le6 the g)* class method returns the name of the class and a 7alue x that itCs
#assed)
class 6)o(Tect*!
def g)cls, x*!
ret-rn cls"$$name$$, x
g = classmet2od)g*
22!
3i# Another 0ay to create class methods is to use decorator synta!6 li/e this)
Yclassmet2od
def g)cls, x*!
We tell you more about decorators in :ha#ter -69
3@:<(&:A.
S3?FF
4ython #ro$rammers con7entionally use the name cls or
occasionally Alass% to refer to the name of the class that $ets #assed
to a class method9 3hese 0eird names are used because class itself
is a /ey0ord and canCt be used as the name of an obFect9% 3his is
similar to the con7ention of usin$ self to refer to the instance that a
class creates9
#allin" class methods
3o call a class method6 use the class name6 a dot6 and the method name9
3his e8am#le calls the class method g)* from class 6 created by the #recedin$ class
definition9
>>> print 6"g)3*
),6,, 3*
Eou can also call a class method on an instance6 li/e this)
>>> print 6)*"g)3*
),6,, 3*
*hat's new in class methods
3he $$ne9$$)* class method deser7es s#ecial mention9
When you create an instance 0ith an old1style class6 4ython first creates the instance and
then calls the $$init$$)* method to initialize it9 'ut this doesnCt 0or/ 0ell for
immutable instances or 0ith metaclasses9 So ne01style classes ha7e a class method6
$$ne9$$)*6 that returns the instance9 Eou can define both $$ne9$$)* and $$init$$)* in
a class9 &f you define both6 $$init$$)* is called after $$ne9$$)*9
:lasses that use $$ne9$$)* are different in se7eral 0ays from classes that only use
$$init$$)*)
3he $$ne9$$)* method creates a ne0 instance6 so it ta/es a cls #arameter9
An $$init$$)* method ta/es a self #arameter because it is called after the
instance is created9
22A
3he $$ne9$$)* method must call its base classCs $$ne9$$)* method to create the
instance9
With an $$init$$)* method6 you can either call the base classCs $$init$$)* or
o7erride it9
3he $$ne9$$)* method must e8#licitly return the created instance 7ia a ret-rn
statement9
An $$init$$)* method doesnCt return anythin$9
<ereCs a tri7ial e8am#le of a $$ne9$$)* method)
class deri5ed$from)str*!
def $$ne9$$)cls, ;args, ;;A9args*!
instance = str"$$ne9$$)deri5ed$from, ;args, ;;A9args*
ret-rn instance
?nless you need to ha7e a $$ne9$$)* method in your class6 itCs con7entional to use
$$init$$)* to #rocess the #assed1in ar$uments and then raise an e8ce#tion if they are
incorrect9 &tCs a 'ad &dea to use $$ne9$$)* to return an error 7alue rather than a correctly
created instance9 &tCs o/ay for $$ne9$$ to raise an e8ce#tion6 thou$h9%
#utting through the static about ethods
Static methods are basically functions attached to a class rather than to a module9 3he
difference bet0een a static method and a class method is that a static method has no cls
#arameter9 When a static method is called in a #ro$ram6 it loo/s Fust li/e a function call9
A static method be$ins 0ith a def statement6 has no self or cls #arameter6 and ends
0ith name_of_method = staticmet2od)name_of_method*9 3he line Ystaticmet2od
before the def statement is decorator synta8 that also indicates a static method9 See
:ha#ter -6 for more information about decorators9
Warnin$ DonCt use static methodsH they add unnecessary com#le8ity to your #ro$rams9
?se functions if you 0ant to do thin$s 0ith classes that donCt reGuire a class
instance9
1on"t use the slots achine
3he $$slots$$ class attribute #ro7ides a 0ay of restrictin$ the attributes of class
instances9 &tCs intended as an ad7anced memory1sa7in$ techniGue for classes that 0ill
ha7e millions yes6 millions% of instances9 &t doesnCt 0or/ 0ell 0ith inheritance9
Warnin$ Sim#le rule) &f you donCt understand metaclasses6 donCt use $$slots$$9
&f our 0arnin$ isnCt enou$h6 ta/e ;uidoCs 0ord for it)
226
__slots__ is a terrible hack $ith nasty, hard-to-fathom side effects that should only be
used by programmers at grandmaster and $i2ard levels. )nfortunately it has gained an
enormous undeserved popularity amongst the novices and apprentices, $ho should kno$
better than to use this magic incantation casually.
Island of :r. 4/(
&n 4ython6 MDM doesnCt stand for 'eals (eady to Open< it stands for 'ethod (esolution
Order.
3i# 3he im#ortant thin$ to remember about MDM is this) &f you use ne01style classes6
you donCt ha7e to 0orry about method resolution order6 because 4ython handles it
automatically9
MDM matters 0hen you 0rite a class that inherits from more than one #arent class9 For
e8am#le6 assume your class inherits from t0o #arent classes6 and each #arent class has a
9rite)* method9 &f your class calls a 9rite)* method6 4ython loo/s for it first in the
class itself and then in the #arent classes9 'ut ho0 0ill 4ython /no0 0hich 9rite)*
method to use2 And more im#ortant6 ho0 0ill you /no0 0hich one $ets used2 &f you
$uess 0ron$6 you mi$ht $et the dreaded ?ne8#ected Desults9
With classic classes see :ha#ter -"%6 4ython loo/s for the method in this 0ay)
-9 &t loo/s in the #arent class listed first leftmost% in the class definition9
29 &t loo/s at that classCs #arent classes6 and so on until there are no more #arent
classes9
"9 &t $oes to the ne8t class listed in the class definition6 and all of its #arent classes6
and so on9
3@:<(&:A.
S3?FF
3his is called depth first and then left to right order9
?nfortunately6 this order doesnCt 0or/ 0ith ne01style classes6 because they all inherit
from o(Tect9 &f this order 0ere used6 the same #arent class 0ould be e8amined multi#le
times6 and youCd still be in the #osition of not /no0in$ 0hich classCs method 0as
disco7ered first9 @7en 0orse6 sometimes a #arent class is su##osed to be o7erridden by a
subclass6 but the #arent classCs method $ets called instead5
So the 0ay MDM 0or/s in ne01style classes is this)
A ne01style class has an $$mro$$ attribute that #rescribes the order for searchin$ #arent
classes9 &t is created by a com#licated al$orithm that $uarantees that each #arent class
e8ists only once in the $$mro$$9
22+
'est of all6 4ython $enerates a classCs $$mro$$ attribute automatically6 so you donCt ha7e
to 0orry about the Icom#licated al$orithm5I Just e8amine the $$mro$$ attribute to see in
0hat order the su#erclasses are searched9
3i# 3he al$orithm does still 0or/ left1to1ri$ht6 so if you 0ant a #articular su#er1class to
$et its method called first6 ma/e sure that itCs listed first in the classes you inherit
from9
2xplodin" %our Head with 4etaclasses
4ython 292 and later ha7e a functionality called metaclasses9 Just as you use a class to
create instances6 you use a metaclass to create classes9
/oles
D@M@M'@D A metaclass is Fust a class>but it creates classes rather than class
instances9 &n other 0ords6 it really is turtles all the 0ay do0n9
A metaclass doesnCt really create the class9 &nstead6 4ython e8ecutes the class code Fust
li/e initializin$ a module% and then #asses the class to the metaclass9 3he metaclass can
do 0hate7er it li/es6 and the class name is $i7en to 0hat it returns9
3i# 3he 4ython communityCs 0isdom about metaclasses is summed u# by 4ython $uru
3im 4eters) IMetaclasses are dee#er ma$ic than **[ of users should e7er 0orry
about9 &f you 0onder 0hether you need them6 you donCt9I 3herefore6 read this section
to find out 0hat a metaclass is and ho0 to reco$nize one 0hen you see it6 but donCt
0orry about ho0 to #ut one into your o0n #ro$ram9
&n a metaclass6 the first ar$ument for a method is con7entionally called cls rather than
self9 Metaclass methods o#erate on the class that the metaclass creates9 A metaclass
doesnCt create instances6 and if you try to create an instance of a metaclass6 you $et an
error that loo/s li/e this)
0race(acA )most recent call last*!
0ype6rror! -n(o-nd met2od ["""]
3he default metaclass for all ne01style classes is type)*9 Sur#rise5 type is really a
metaclass instead of the function 0eC7e been callin$ it all alon$9 &t Fust does a $ood Fob of
dis$uisin$ itself until you need to use its other ca#abilities9%
Applications
3o use a metaclass6 use the $$metaclass$$ name either
&n the body of a class
22,
At the to# of a module if you 0ant all the classes in that module to use that
metaclass%9
<ereCs an e8am#le that sets u# a class to use a metaclass9 (otice that you only refer to
yetaHlassH you donCt call it there are no #arentheses%9
class yHlass!
$$metaclass$$ = yetaHlass
@7en if you 0ant to 0rite code that creates multi#le classes6 you rarely need to use
metaclasses9 A function can return a class9 A function that returns a class is called a class
factory9 An e8am#le of a class factory called class$9it2$met2od)* can be found in this
article on metaclass #ro$rammin$ in 4ython)
htt#)LL00091-2,9ibm9comLde7elo#er0or/sLlinu8LlibraryLl1#ymeta9html
3he classo(T)* function of the ne9 module also acts as a class factory9
#hapter .B, 'eelin" 2xceptional
4eo#le 0ho use your 4ython #ro$rams donCt al0ays do e8actly 0hat you 0ant them to
do6 and sometimes a #ro$ram $ets in#ut it doesnCt e8#ect9 4ython has built1in tools for
handlin$ these situations9
3his cha#ter describes some of 4ythonCs error1handlin$ features6 includin$ e8ce#tions and
code bloc/s you 0rite to ca#ture e8ce#tions9 We also introduce you to 0ritin$ your o0n
e8ce#tion handlers and discuss t0o 0ays of a##roachin$ error handlin$9
All about !pecial Handlin"
&n 4ython6 as in @n$lish6 an e!ception is a s#ecial case>somethin$ that needs s#ecial
handlin$9 When the 4ython inter#reter encounters somethin$ une8#ected6 it $enerates an
internal messa$e6 or raises an e!ception9 Eou 0rite code to tell 4ython 0hat to do 0hen
an e8ce#tion occurs9 4ythonCs built1in e8ce#tion code usually dis#lays an on1screen error
messa$e containin$ information about 0hat /ind of #roblem occurred and 0here9
4ython also uses e8ce#tions for control flo0 in non1error conditions6 es#ecially in for
loo#s6 0hich 0e discuss in :ha#ter -09
;I didn't think they would do that<;
When you 0rite #ro$rams for other #eo#le to use6 es#ecially #ro$rams that as/ users to
#ro7ide in#ut of some /ind6 users may $i7e your #ro$ram in#ut it doesnCt e8#ect9
22*
For e8am#le6 if your #ro$ram as/s the user to in#ut a number6 she mi$ht accidentally ty#e
a letter instead9 &f your code is set u# to 0or/ only 0ith a number6 this in#ut 0ould cause
your code to fail9 4ro#er e8ce#tion handlin$ catches in#ut of the 0ron$ ty#e6 #rints a
friendly reminder about 0hat in#ut to use6 and lets the user try a$ain9 3hat 0ay6 the user
can correct the #roblem easily instead of ha7in$ to restart the #ro$ram or loo/ at a
confusin$ error messa$e such as this)
0ype6rror! cannot concatenate ,str, and
,int, o(Tects
<ereCs an e8am#le of 4ythonCs error handlin$9 &f you for$et a colon 0hen 0ritin$ a for
statement6 4ython #rints an error messa$e called @yntax6rror)
>>> for x in range)&*
>ile "#stdin>", line /
for x in range)&*
?
@yntax6rror! in5alid syntax
3his e!ception handling functionality ma/es debu$$in$ and usin$ #ro$rams easier9
Without it6 a #ro$ram 0ould Fust mysteriously Guit 0hene7er it encountered a bu$9
EouC7e #robably used #ro$rams that do that and /no0 ho0 frustratin$ it can be5
'ut 4ython ta/es e8ce#tion handlin$ e7en further) Eou can 0rite your o0n e8ce#tion
handlers based on 4ythonCs built1in ones9 For e8am#le6 if you donCt 0ant an error messa$e
dis#layed to the screen6 you can desi$n your #ro$ram to catch s#ecific errors your users
mi$ht ma/e and then 0rite e8ce#tions that $i7e users hints about ho0 to use your
#ro$ram correctly9
Tryin" Thin"s (ut
4ython #ro$rammers handle e8ce#tions in a code bloc/ that be$ins 0ith the 0ord try9
'loc/s that use try are often nested inside each other to #ro7ide com#lete error handlin$9
3here are t0o /inds of try bloc/s)
try8except) &f e8ce#tions occur in the try code6 the except clause ca#tures and
handles the e8ce#tions9
try8finally) 3he try code and the finally code al0ays run9 &f e8ce#tions
occur in the try code6 the finally clause re1raises the e8ce#tions for handlin$
else0here in your #ro$ram9
&n 4ython 29A6 these t0o ty#es can be combined into one bloc/)
try8except8else8finally9 See ItryLe8ce#tLelseLfinally) 3o$ether at last6I later in this
cha#ter9
3he follo0in$ sections describe the inner 0or/in$s of try bloc/s9
2"0
)sing try9except stateents
Eou use a try8except bloc/ if you 0ant the except code to run only if there is an
e8ce#tion9 &t loo/s li/e this)
try!
some code
except SomeException
exception!handling code
Trackin" down tracebacks
4ython /ee#s trac/ of 0hat your #ro$ram does9 @ach time you call a function6 4ython
adds the call to the call stack9 @ach function call adds another layer to the call stac/9
When an e8ce#tion is raised6 4ython stores the call stac/ as a traceback that tells you
e8actly 0here the error occurred and the seGuence of calls that $ot you there9 &t doesnCt6
ho0e7er6 list the function #arameters>you need to do your o0n lo$$in$ for that9%
3he follo0in$ e8am#le #ro$ram contains an error9 When you run it6 4ython raises an
e8ce#tion6 0hich $enerates a tracebac/)
+++ 02ree f-nctions t2at call eac2 ot2er
def a)*!
()*
def ()*!
c)*
def c)*!
/8% + Lttempt at di5ision (y
Qero, 9ill raise exception
+++ B-n f-nction a)*
a)*
When you run the #ro$ram6 the tracebac/ lists the calls in the order they 0ere recei7ed)
>ile "a"py", line =, in #mod-le>
a)*
>ile "a"py", line 3, in a
()*
>ile "a"py", line &, in (
c)*
>ile "a"py", line E, in c
/8% + Lttempt at di5ision (y
Qero, 9ill raise exception
KeroOi5ision6rror! integer di5ision or
mod-lo (y Qero
&t 0or/s as follo0s)
-9 4ython runs the code in the try clause9
2"-
29 &f no e8ce#tion is found6 4ython finishes the try bloc/ and doesnCt run the code in
the except bloc/9
"9 &f an e8ce#tion occurs in the try clause6 4ython s/i#s the rest of the try code and
chec/s the e8ce#tion a$ainst the e8ce#tions named in the except statement9 &f
there is a match6 4ython runs the except clause9
!9 &f an e8ce#tion occurs in the try clause that doesnCt match the except statement6
4ython loo/s for a matchin$ e8ce#tion handler in any code that the try bloc/ is
nested in9 &f 4ython doesnCt find a matchin$ e8ce#tion handler6 then 4ython uses
its built1in e8ce#tion handler that is6 it #rints those $ee/s#ea/ error messa$es you
sa0 earlier%9
Handlin" multiple exceptions
Eou can #rocess se7eral different /inds of e8ce#tions by usin$ try8except code9 &f you
0ant to catch more than one /ind of e8ce#tion in a try8except bloc/6 there are t0o
0ays to do it)
To handle all the exceptions the same way, list multiple exception classes
within a sin"le except clause.
4ass the e8ce#tions as a tu#le in #arentheses6 se#arated by commas%6 li/e this)
except )B-ntime6rror, 0ype6rror, 1ame6rror*!
To handle each exception a different way, use multiple except clauses.
3hese clauses are tested in orderH if thereCs a match in a clause6 the rest of the
clauses are s/i##ed9 <ereCs 0hat t0o except clauses mi$ht loo/ li/e)
except B-ntime6rror!
some code
except "ypeError
some different code
Follo0in$ is a small #ro$ram that chec/s for correct user in#ut9 &t uses a try8except
bloc/ 0ith a sin$le except clause that catches t0o /inds of e8ce#tions9
try!
x = ra9$inp-t)"6nter an integer! "*
y = int)x*
print "Po-r n-m(er 9as", y
except )0ype6rror, Wal-e6rror*!
print "02at didn,t
rocessin" exception ar"uments
Some e8ce#tions use ar$uments associated data6 li/e 0hat functions and methods use%)
2"2
@8ce#tions usually ha7e a strin$ ar$ument describin$ 0hat sort of #roblem
occurred9 <ereCs an e8am#le)
try!
x = int)x*
except 0ype6rror!
raise 0ype6rror)"Dr is not a 5alid integer" D x*
A fe0 e8ce#tions also ha7e a tu#le ar$ument that stores se7eral bits of
information about the #roblem9 3his is an ad7anced feature 0e donCt co7er in this
boo/9
3o #rint or lo$ the details of an e8ce#tion6 s#ecify a name in the except statement to hold
the e8ce#tion instance9 &n the follo0in$ e8am#le6 the name detail is used to store the
e8ce#tion instance)
>>> try!
""" x = /8%
""" except KeroOi5ision6rror, detail!
""" print "Jops,", detail
"""
Jops, integer di5ision or mod-lo (y Qero
Warnin$ &f you use more than one e8ce#tion class in your e8ce#t statement6 #ut them in
#arentheses to differentiate from the e8ce#tion instance name9 Mther0ise youCll
$et an une8#ected result9
:ealin" with exceptional exceptions
When usin$ multi#le except clauses6 set u# the last clause to catch any une8#ected
e8ce#tions9 A Icatch1allI except clause commonly called a bare except% should $o at
the hi$hest le7el of your #ro$ram9 &t should include code that lo$s the errors it catches9 &t
could also #rint the error and send it to a lo$$in$ function and then re1raise the
e8ce#tion9% .o$$in$ is co7ered in :ha#ter -+9
Warnin$ &nclude code in your except clause that lo$s or #rints the errors the except
clause catches9 Mther0ise6 4ython s0allo0s all the une8#ected e8ce#tions and
you 0onCt /no0 0hat /inds of errors your #ro$ram is really $eneratin$6 0hich
ma/es debu$$in$ harder9
*hen it comes to ;or else;
A try8except bloc/ can ha7e an else clause9 3he else clause runs only if the try bloc/
runs successfully and com#letely)
(o e8ce#tion is raised9
(o ret-rn or other bloc/1e8itin$ statement is e8ecuted9
?se an else clause for code that needs to run if the try clause doesnCt raise an e8ce#tion9
4ut it after all the except clauses6 li/e this)
2""
try!
some code
except SomeException
exception!handling code
else!
necessary code
)sing try9finally stateents
&f you need some code that al$ays runs as #art of a try bloc/6 and you 0ant to handle
e8ce#tions else0here in your #ro$ram6 use the try8finally bloc/9
3he finally bloc/ is $ood for cleanin$ u#>closin$ o#en files and releasin$ memory6
for e8am#le9 4ython usually does these thin$s for you automatically6 but in #ro$rams
handlin$ lots of data6 itCs useful to clean u# as you $o alon$9%
3@:<(&:A.
S3?FF
3he try8finally bloc/ follo0s these ste#s)
-9 4ython runs the code in the try bloc/9
29 &f 4ython finds no errors6 it lea7es the try bloc/ and runs the
code in the finally bloc/9
"9 &f a ret-rn6 (reaA6 or contin-e statement is e8ecuted in the
try bloc/6 the finally clause runs on the 0ay out9
!9 &f an e8ce#tion occurs in the try bloc/6 4ython s/i#s the rest
of the bloc/ but saves the e!ception. &t runs the finally
clause and then sends the e8ce#tion u# to hi$her le7els of
your #ro$ram that is6 it re-raises the e!ception%6 0here you
should ha7e e8ce#tion1handlin$ code9
A try8finally bloc/ loo/s li/e this)
try!
some code
finally!
cleanup code
Warnin$ When you use a finally clause6 donCt #ut initializin$ statements for e8am#le6
assi$nment statements% in the try bloc/9 &n the follo0in$ code6 if open)* raises
an e8ce#tion6 the name f doesnCt actually $et created9 3hen 0hen the finally
clause runs6 4ython 0ill #roduce a 1ame6rror com#lainin$ that f doesnCt e8ist9
try!
f = open),c2eese$list,*
process)f* + 02is line is pse-do7code
finally!
f"close)*
3o a7oid the #roblem6 #ut the open)* line before the try8finally bloc/6 li/e this)
f = open),c2eese$list,*
2"!
try!
process)f* + 02is line is pse-do7code
finally!
f"close)*
'ecause itCs usually a $ood idea to do error1chec/in$ 0hene7er you try to o#en a file6 you
mi$ht 0ant to 0ra# the open)* line in a se#arate try8except statement9
try9except9else9finally: Together at last
Startin$ in 4ython 29A6 you can 0rite a sin$le bloc/ that includes all the com#onents
discussed in the #re7ious sections9 3his code 0or/s the same 0ay as the old code did6 and
the $uidelines for ho0 to use the statements are the same>the only difference is that you
donCt ha7e to nest t0o bloc/s if you 0ant to use both except statements and a finally
statement to$ether9
3he follo0in$ e8am#le uses try6 except6 and finally to$ether to create a function that
tries to read a file and return its contents9 'ut if thereCs an error readin$ the file6 the
function returns 1one instead9 Whether or not the file read is successful6 the function
closes the file and returns a 7alue)
def safe$read)filename*!
f = open)filename*
try!
data = f"read)*
except GJ6rror!
data = 1one
finally!
f"close)*
ret-rn data
It's easier to ask for"iveness than permission
@arly in the history of #ro$rammin$6 errors 0ere often handled by tryin$ to $uess e7ery
error that mi$ht occur and then 0ritin$ a lot of if statements to #rocess them9 3his
#hiloso#hy of error handlin$ is called I.oo/ 'efore Eou .ea#I .'E. for short%9 3his is
e8#ensi7e in com#uter #rocessin$ #o0er because the error chec/in$ occurs e7ery time
the user does somethin$ enters data6 clic/s somethin$ on the screen6 chooses from a
menu6 and so on%9
3he try statement lets you assume that users 0ill use your #ro$ram correctly9 &t handles
errors only 0hen they actually occur9 3his #hiloso#hy of error handlin$ is called I@asier
to As/ For$i7eness than 4ermissionI @AF4 for short%9 4ro#s to Ale8 Martelli for the
#hrasin$ and tireless #romotion of the #hiloso#hy6 and to Admiral ;race Murray1<o##er6
architect of :obol6 0ho brou$ht the conce#t into com#uters albeit in a different conte8t
>she 0as commentin$ on a techieCs relations 0ith bureaucrats%9
@AF4 is more efficient than .'E. in se7eral 0ays)
2"A
Eou can handle all the errors in one #lace the except statements%9
Eou can handle une8#ected errors by usin$ an except statement 0ith a Icatch1allI
error handler9
Eou handle errors only 0hen they occur and not chec/ for errors e7ery time the
#ro$ram $ets some in#ut9
/aisin" %our #ode to ?ew -evels
When your #ro$ram determines an error condition such as de#ositin$ a ne$ati7e amount
to a ban/ account%6 the raise statement lets you force an e8ce#tion e7en if 4ython
doesnCt detect an error9 Daisin$ an e8ce#tion I#ushes the error hi$herI in your #ro$ramCs
code hierarchy9
3o 0rite a basic raise statement6 ty#e the 0ord raise and the ty#e of e8ce#tion to raise9
3his e8am#le raises the Wal-e6rror e8ce#tion)
raise Wal-e6rror
3o include a 7alue 0ith an e8ce#tion6 s#ecify the 7alue in #arentheses after the e8ce#tion9
3he 7alue is usually a strin$ that $i7es a messa$e about the error6 as in this e8am#le)
raise 1ame6rror),W2at,s yo-r name againV,*
Eou mi$ht also see this code 0ritten in the old style6 0ith a comma rather than
#arentheses for e8am#le6 raise 1ame6rror, ,W2at,s yo-r name againV,%6 but
#arentheses are #referred6 and the comma form 0onCt be 7alid in 4ython "90 and later9
3@:<(&:A.
S3?FF
A raise statement can also include a tracebac/ obFect6 but 0e donCt
co7er that in this boo/9
Sometimes6 0hen 4ython has already raised an e8ce#tion6 you mi$ht 0ant to do
somethin$ 0ith it no0 and also force hi$her le7els of the #ro$ram to deal 0ith it in
$ee/s#ea/6 this is /no0n as Iallo0in$ the e8ce#tion to #ro#a$ate u# the callin$ stac/I%9
3o sa7e6 or re1raise6 an e8ce#tion6 ty#e raise on a line by itself9 For e8am#le6 if you 0ant
to send an e8ce#tion to a lo$$in$ function and re1raise it6 your code mi$ht loo/
somethin$ li/e this)
try!
some code
except Wal-e6rror!
log$pro(lem)* + replace t2is 9it2 yo-r logging code
raise
D@M@M'@D ?se a bare raise only 0hen there is an e8istin$ e8ce#tion>other0ise you
$et an e8ce#tion5
2"6
4akin" %our ro"ram 2xceptional
4ythonCs e8ce#tions are #retty $eneral9 :reatin$ your o0n e8ce#tions sim#lifies your
error handlin$9 &tCs much easier for users and other #ro$rammers to understand your code
0hen you use an e8ce#tion that describes 0hat the error is6 both at the #oint 0here the
error occurs and 0hen you handle it in an except clause9
3o create your o0n e8ce#tions6 0rite a ne0 e8ce#tion class or class hierarchy thatCs
deri7ed from 4ythonCs 6xception class or one of its subclasses9
*riting a base class for your exceptions
<ere are some ti#s for 0ritin$ an e8ce#tion class)
?se as a base class the 4ython e8ce#tion that most closely resembles the
e8ce#tion youCre 0ritin$9
Most e8ce#tions ha7e names that end in 6rror to maintain consistency 0ith
4ythonCs built1in error handlin$9 Eours should6 too9
=ee# e8ce#tion classes sim#le9 3hey should mostly only include attributes that
can store information about the error9 Se#arate handlers can e8tract this
information and act on it9
For e8am#le6 to define a class based on 4ythonCs 6xception class6 0rite somethin$ li/e
this)
class yod-le6rror)6xception*!
pass
1e$eloping an exception hierarchy
&f youCre creatin$ more than one /ind of e8ce#tion>#erha#s as #art of a module that
handles se7eral related functions>$ood #ractice is to start by creatin$ a base class for the
e8ce#tions9 ?sually6 the base class doesnCt do anythin$9 3he code loo/s somethin$ li/e
this)
class yod-le6rror)6xception*!
"""Iase class for my exceptions""""
pass
3o ma/e a subclass based on the base class6 follo0 these ste#s)
-9 ?ame the subclass after its correspondin" module or class, plus ;2rror;, like
this,
2" class Gnp-t6rror
3@:<(&:A.
S3?FF
3his hel#s you /ee# trac/ of 0hich modules your errors are
comin$ from9
2"+
"9 ut the base class in parentheses in the class definition, and type a colon.
." class Gnp-t6rror (,y,od"le-rror):
A9 (n the next line, indent four spaces, and write code for what you want the
exception to do.
<" class Gnp-t6rror)yod-le6rror*!
E" p#ss
#hapter .E, Tacklin" !ome Advanced
'eatures
3his cha#ter describes some of the ad7anced features of 4ython6 as 0ell as some of the
ne0 features of 4ython 29A9
&f you donCt understand #arts of this cha#ter6 donCt 0orry9 WeCre describin$ these thin$s
mainly so you 0ill reco$nize them if you see them in someone elseCs code9 As you 0or/
0ith 4ython6 your understandin$ 0ill im#ro7e until youCre ready to use these features
yourself5
*hat's That IdiomC
3able -61- sho0s you ho0 some of the ad7anced and ne0 features loo/ in 4ython code9
All these features are described later in this cha#ter9
3able -61-) Deco$nizin$ Ad7anced 4ython :odin$ &dioms
M#en table as s#readsheet
8eyword or
2xample
'eature :escription
[U for U in
mylist]
.ist com#rehension
listcom#%
:reates a ne0 listH allo0s
mani#ulation of list items%
))x;2* for _ in
)2, ., <**
;enerator e8#ression
$ene8#%
:reates a ne0 iterator obFectH more
efficient than a listcom#
yield in a function
or method
;enerator Ma/es a function that remembers
0here it left off
9it2 exp as name!
9it2 statement Adds conte8t mana$ement to
try8finally bloc/s%
U if H else P
:onditional e8#ression @asier truth testin$
Y
Decorator :reates function 0ra##er
lam(da a, (! a;;(
.ambda e8#ression or
anonymous function
An e8#ression that returns a function
2",
*hat to :o ?ext, Iterators and +enerators
.ists are a useful 4ython feature that can ta/e u# a lot of memory and #rocessin$ time9
/terators are tools that hel# ma/e lists more efficient6 and generators ma/e creatin$
iterators easy9
The itertools library
3he itertools library6 0hich 0as introduced in 4ython 29"6 includes functions for
ma/in$ lists more efficient both to 0rite and to use9 @ach of the itertools functions
creates a different ty#e of iterator obFect9 3he obFects are desi$ned to 0or/ to$ether9 3hey
also 0or/ 0ith the tools in the operator module to #roduce hi$h1s#eed results9
<ere are some sim#le e8am#les of itertools code9
3his itertools"imap)* code ta/es t0o lists and #asses their elements one element
from each list at a time% to a lam(da function see the IMary had a little lambdaI section6
later in this cha#ter%6 0hich multi#lies the elements)
>>> n = [/, 3, <, ., 3, <, 3, 23.]
>>> m = [/, 2, 3, &, <, E, ', =]
>>> for i in itertools"imap))lam(da x, y! x;y*, m, n*!
""" print i,
"""
/ < /' 2% /' .2 2. 2/%<
The inner lives of iterators
&terators are used throu$hout 4ython6 and if you understand them6 you 0ill ha7e a $ood
$ras# on 0hat 4ython is all about9
An iterator is a 4ython obFect that #roduces elements one at a time9 An iterable is an
obFect that can create an iterator9 For e8am#le6 the iterator created from a list returns the
list elements one at a time6 in order9 3he iterator for a dict returns the /eys one at a time6
unordered9 .ists and dicts are e8am#les of iterables9
As if thin$s 0erenCt confusin$ enou$h6 some iterables such as files% are also iterators9
For e8am#le6 a file sim#ly returns itself 0hen as/ed for an iterator technically6 it returns
self%9 3his means that all iterators are also iterables9
Finally6 you can create functions and classes that either create iterators or are themsel7es
iterators9
&terators are useful because they sim#lify code and reduce memory usa$e9 3hey can also
ma/e a##lications more res#onsi7e because they do only the calculations needed to
#roduce one element at a time instead of doin$ the 0or/ of #roducin$ all the elements
u#front9
2"*
3he official 4ython tutorial a7ailable on the 4ython Web site at htt#)LL0009#ython9or$
contains further e8#lanation of the inner 0or/in$s of iterators)
0ehind the scenes, the for statement calls iter%4 on the container ob-ect. &he function
returns an iterator ob-ect that defines the method ne!t%4 $hich accesses elements in the
container one at a time. "hen there are no more elements, ne!t%4 raises a ,top/teration
e!ception $hich tells the for loop to terminate. &his e!ample sho$s ho$ it all $orks9
>>> s = ,a(c,
>>> it = iter)s*
>>> it
#iterator o(Tect at %x%%L/OI&%>
>>> it"next)*
a,
>>> it"next)*
(,
>>> it"next)*
c,
>>> it"next)*
0race(acA )most recent call last*!
>ile "#stdin>", line /, in #mod-le>
@topGteration
3his itertools"co-nt)* code creates an iterator that returns consecuti7e inte$ers
be$innin$ 0ith the ar$ument you s#ecify 0 by default%9 Eou can use
itertools"co-nt)* 0ith itertools"imap)* in the #recedin$ e8am#le to $enerate one
of the lists6 li/e this)
>>> n = [/, 3, <, ., 3, <, 3, 23.]
>>> for i in itertools"imap))lam(da x, y! x;y*, n, itertools"co-nt)**!
""" print i,
"""
% 3 /2 /2 /2 3% /' /<3'
3he itertools"islice)* function 0or/s on a slice of a list of numbers9 3his code
creates a list of consecuti7e numbers bet0een 0 and 6- xrange)<2*%9 &t then ta/es a
slice of that list the ar$ument < s#ecifies the be$innin$ of the slice and the ar$ument <2
s#ecifies the end% and #rints e7ery third number from the slice the ar$ument 3 s#ecifies
e7ery third number%)
>>> for i in itertools"islice)xrange)<2*, <, <2, 3*!
""" print i,
"""
< = /2 /& /' 2/ 2. 2E 3% 33 3< 3= .2 .& .' &/ &. &E <%
Generators: yield for faster processing
=enerators loo/ li/e functions>in fact6 they are a s#ecial /ind of function9 3hey use the
yield statement to return data 0hereas re$ular functions use the ret-rn statement%9
2!0
;enerators are a con7enient 0ay to create iterators because they sim#lify the record1
/ee#in$ needed for returnin$ each element9 3hat is6 you donCt ha7e to /ee# chec/in$ and
reassi$nin$ 7alues because the $enerator remembers the 7alues it 0as assi$ned last9%
;enerators are different from functions in t0o other 0ays)
:allin$ a $enerator al0ays returns the same thin$) an iterator usually called a
generator iterator or geniter%9
:allin$ a function can return any data ty#e6 de#endin$ on 0hat the function does9
When 4ython encounters a yield statement6 it doesnCt e8it the $enerator iterator9
&nstead6 it remembers 0hat 0as ha##enin$ inside the function includin$ any
7alues that 0ere assi$ned%9
When 4ython encounters a ret-rn statement6 it e8its the function6 and 7alues
assi$ned in the function are for$otten9
'ecause they ma/e iterators6 $enerators are usually used in loo#s9 <ereCs ho0 a $enerator
0or/s)
-9 When you call a $enerator6 you $et a ne0 iterator9
29 When you call the next)* method of the $enerator iterator6 the body of the
$enerator function e8ecutes until it hits a yield statement9
3he for statement calls an iteratorCs next)* method automatically9
"9 3he yield statement #asses a ne0 7alue to the next)* method9 3he $enerator
iterator doesnCt e8it9 &nstead6 it #reser7es or remembers the state of the function
body9
!9 3he next)* method $ets called a$ain9 'ecause the iterator remembers the state of
the function6 the 7alues that 0ere in effect before6 remain in effect9
A9 3he cycle re#eats until 4ython runs off the end of the $enerator6 a ret-rn
statement is e8ecuted6 or an e8ce#tion is raised9
69 &f the $enerator ends normally6 4ython raises @topGteration9
3@:<(&:A.
S3?FF
;enerators are sometimes called resumable functions or generator
functions. 4eo#le 0ho call them generator functions usually call the
thin$ that they return a generator rather than a generator iterator.
4ython can add to your stoc/ of ton$ue t0isters as 0ell as hel# you
become a better #ro$rammer5%
3he follo0in$ code creates a $enerator function that filters out re#eated items in a
seGuence>for e8am#le6 re#eated lines in a file9 Eou #re#are the file by readin$ it usin$
the readlines)* method and then sortin$ the resultin$ list of strin$s)
2!-
def -niN)inp-t*!
pre5 = o(Tect)*
for line in inp-t!
if line != pre5!
yield line
pre5 = line
3i# &n 4ython 29! and earlier6 you canCt use yield in the try clause of a try8finally
statement9
3@:<(&:A.
S3?FF
4ython 29A adds some esoteric features to $enerators9 We list them
here so you 0onCt be sur#rised if you see them in other #eo#leCs
#ro$rams9% All three features are ne0 methods on $eniters)
send)* allo0s you to #ass 7alues bac/ into the $enerator9
A conseGuence of this chan$e is that yield becomes an
e8#ression in 4ython 29A6 so code li/e this is allo0ed in
earlier 7ersions6 it raises an error%)
foo = )yield foo*
Startin$ in 4ython 29A6 $enerators are also called coroutines.
t2ro9)* raises an e8ce#tion inside the $enerator9
close)* forces the $enerator to e8it9
2xpression and #omprehension, -istcomps and
+enexps
.ist com#rehensions listcom#s% and $enerator e8#ressions $ene8#s% let you focus on
data rather than control structures9 See also the IFocusin$ on FunctionsI section6 later in
this cha#ter9% 3hey re#lace a for loo# 0ith an e8#ression9
.ist com#rehension and $enerator e8#ression code can loo/ intimidatin$ if you donCt
/no0 0hatCs $oin$ on9 'ut after you $et used to them6 youCll #robably find them a lot
easier and Guic/er to use than the multi1line statements they re#lace9 Demember that you
ne7er have to use a listcom# or a $ene8#6 but a lot of #ro$rammers use them6 and itCs best
to ha7e a basic understandin$ of ho0 they 0or/9
List coprehensions
A listcom# is a nifty shorthand 0ay of creatin$ a ne0 list9 Eou can create a ne0 list in
other 0ays6 such as by usin$ the map)* function6 but the listcom# synta8 is more fle8ible6
sim#ler to create6 and often easier to read9
2!2
A list com#rehension is enclosed in sGuare brac/ets and al0ays includes the /ey0ords
for and in9
As an e8am#le6 if you ha7e a list of strin$s and you 0ant to ma/e sure they are all
lo0ercase6 you could do somethin$ li/e this)
mylist = [,LIH,, ,fJJ,, ,(ar,]
tmp = []
for x in mylist!
tmp"append)x"lo9er)**
mylist = tmp
3hatCs a lot of code for a sim#le tas/5 Well6 Ja7a #ro$rammers mi$ht not thin/ itCs a lot
of code6 but 4ythonistas are s#oiled9% 4lus6 anyone readin$ the code 0ould ha7e to read
the 0hole for loo# carefully to understand that it 0as creatin$ a ne0 list9
Fortunately6 you can use a listcom# instead9 &t does the e8act same thin$ as the for loo#6
but it uses only one line of code9
#reatin" a simple listcomp
3o create a sim#le listcom#6 follo0 these ste#s)
-9 Type a name, =6 and [
2" mylist = [
"9 Type the action you want done to create each list item.
For e8am#le6 if you ha7e a list of strin$s and you 0ant to ma/e sure they are all
lo0ercase6 you mi$ht use the lo9er)* method of lists9 Eour line of code 0ould
no0 loo/ li/e this)
mylist = [x.lower()
!9 Type for and the same name you used in the action.
&" mylist = [x"lo9er)* for x
<"
+9 Type in6 the name you typed at the be"innin" of the line, and ]
'" mylist = [x"lo9er)* for x in mylist]
D@M@M'@D Eou should use a listcom# rather than a for loo# 0hen you 0ant to focus
on creatin$ a list9 3hat 0ay6 someone readin$ the code doesnCt ha7e to
e8amine the for loo# to find the #ur#ose of the code9
#reatin" a listcomp that applies a filter
EouC7e seen that you can use a listcom# to do somethin$ to each item in a list9 Another
thin$ you can do is a##ly filters to the items in a list to build a ne0 list9 3he follo0in$
listcom# #roduces a list of numbers di7isible by A by usin$ the filter if x D & == %9
(ote that it uses the e8#ression range)&%* rather than a list name9
2!"
>>> filtered$list = [x for x in range)&%* if x D & == %]
>>> filtered$list
[%, &, /%, /&, 2%, 2&, 3%, 3&, .%, .&]
3i# .istcom#s can $et com#le8 rather Guic/ly9 &f your code is $ettin$ hard to read6 use a
for loo# instead)
filtered$list = []
for x in range)&%*!
if x D & == %!
filtered$list"append)x*
Generator expressions
&n the earlier section6 I;enerators) yield for faster #rocessin$6I 0e discuss $enerators6
0hich are a s#ecial /ind of function that ma/es #rocessin$ in loo#s more efficient9
4ython 29! introduces generator e!pressions9 ;enerator e8#ressions gene!ps for short%
are a shorthand 0ay of creatin$ $enerator iterators9
;ene8#s loo/ li/e listcom#s6 but they use #arentheses rather than brac/ets9 <o0e7er6 if
the $enerator e8#ression is the only ar$ument of a function6 you can s/i# the
#arentheses9%
3he difference bet0een a $ene8# and a listcom# is)
A $ene8# creates a $enerator iterator also called a geniter%9
A listcom# creates a list9
3i# Mne 0ay to use a listcom# or a $ene8# is to $i7e it as the ar$ument to a function that
ta/es an iterable9 For e8am#le6 a $ene8# is a handy 0ay of buildin$ a dictionary by
usin$ the dict)* constructor9
3o 0rite a sim#le $ene8# that creates a dict in 0hich each number is associated 0ith its
sGuare6 follo0 these ste#s)
-9 Type the name of a function and )9
&n this e8am#le 0e use the dict)* function6 0hich creates a dictionary out of
t0o1item tu#les9
dict)
29 Type )6 a name for one item in the tuple, and a comma.
3" dict)(x,
!9 Type an expression for the other item in the tuple, and then type *
&" dict ))x, x ** 2)
69 Type for and the name of the first item in the tuple.
E" dict ))x, x ;; 2* for x
2!!
,9 Type in and a tuple representin" the values you want to assi"n to the first
item in the tuple. Then type *
=" dict))x, x ;; 2* for x in (2, ., /))
&f you ty#ed the abo7e code into interacti7e mode6 youCd $et this result)
>>> dict))x, x ;; 2* for x in )2, ., <**
{2! ., .! /<, <! 3<}
3i# &f the e8#ression 0ould e7aluate to a tu#le6 it must be in #arentheses9 3his also
a##lies to listcom#s9
*ith *hat, %our Bare HandsC 5The ower of OwithP
!tatements6
&n 4ython 29A6 the ne0 9it2 statement ma/es try8finally statements more #o0erful
and easier to use9 Maybe e7en #o0erful enou$h to slay a fierce $reen dra$on5% 3he
synta8 is as follo0s)
9it2 6UCB6@@GJ1 as 1L6!
I3JHX
3he 9it2 statement 0or/s li/e this)
6UCB6@@GJ1 returns a 7alue that the 9it2 statement uses to create a conte!t a
s#ecial /ind of 0ra##er obFect%9 3his is similar to the 0ay a for loo#
automatically creates an iterator9 3he conte8t is used to e8ecute the I3JHX9 3he
bloc/ mi$ht end normally6 $et terminated by a (reaA or ret-rn6 or raise an
e8ce#tion9 (o matter 0hich of those thin$s ha##ens6 the conte8t contains code to
clean u# after the bloc/9
3he as 1L6 #art is o#tional9 &f you include it6 you can use 1L6 in your I3JHX9
1L6 is similar to the tar$et of a for loo# that is6 the i in for i in mylist%9
3o use the 9it2 statement in 4ython 29A6 #ut this code in your module)
from $$f-t-re$$ import 9it2$statement
3@:<(&:A.
S3?FF
&n 4ython 296 and later6 the 9it2 /ey0ord 0ill automatically be
reco$nized9
3he #rotocol used by the 9it2 statement is called the conte!t management protocol, and
obFects im#lementin$ it are conte!t managers.
Eou can create a conte8t mana$er 0ith a decorated $enerator see the later section6
IDecoratin$ Eour :odeI%6 li/e this)
from contextli( import contextmanager
2!A
Ycontextmanager
def opened)filename, mode="r"*!
f = open)filename, mode*
try!
yield f
finally!
f"close)*
Eou mi$ht use the #recedin$ code as follo0s)
9it2 opened)"8etc8pass9d"* as f!
for line in f!
print line"rstrip)*
3@:<(&:A.
S3?FF
3his canonical standard% e8am#le is from the 4ython docs at
htt#)LL0009#ython9or$9 3he code creates a tem#late for o#enin$ a file
that ensures the file is closed 0hen 4ython lea7es the 9it2 bloc/9
4akin" 2xceptions for %ourself
3he 0ay you use standard e8ce#tions and 0rite custom e8ce#tions chan$es sli$htly in
4ython 29A9 <ereCs the scoo#)
4ython 29A turns all standard e8ce#tions into ne01style classes see :ha#ter -! for
more about ne01style classes% and introduces a ne0 su#erclass called
Iase6xception9 All the standard e8ce#tions youCll use in your #ro$rams still
inherit from the 6xception class6 but the 6xception class in turn is a subclass of
Iase6xception9
&n 4ython -9A throu$h 29!6 all standard e8ce#tions are subclasses of the
6xception class6 but any classic% class can be raised as an e8ce#tion9
3@:<(&:A.
S3?FF
30o e8ce#tions inherit from Iase6xception) Xey(oardGnterr-pt
and @ystem6xit9 'ut you ha7e no business catchin$ those
e8ce#tions9 Eou should let them beha7e the 0ay theyCre su##osed to
so that the user can interru#t your #ro$ram9
D@M@M'@D Any custom e8ce#tions you 0rite should be subclasses of 6xception9
See :ha#ter -A to find out ho0 to 0rite custom e8ce#tions9% Althou$h
this rule isnCt enforced no06 it 0ill be enforced in 4ython "909
Warnin$ @arly 7ersions of 4ython #rior to -9A% used strin$ e8ce#tions9 Althou$h 4ython
still lets you create strin$ e8ce#tions6 they 0onCt be su##orted in the future9 &n
4ython 29A and later6 strin$ e8ce#tions raise a CendingOeprecationWarning9
?se them only in the e8tremely unusual% case of code that needs to be
com#atible 0ith 7ersions earlier than -9A9
&nder (ne #ondition
2!6
:onditional e8#ressions 0ere added to 4ython 29A to sim#lify choosin$ one of t0o
o#tions in an e8#ression9 See :ha#ter -09%
A conditional e8#ression ta/es the follo0in$ form)
U if H else P
&t 0or/s li/e this)
-9 H is e7aluated9
29 Action de#ends on 0hether : is true)
o &f H is true6 then U is e7aluated to $i7e the result9
o &f : isnCt true6 P is e7aluated to $i7e the result9
Eou can use #arentheses in conditional e8#ressions9 'ecause #arentheses identify the
conditional e8#ression as a sin$le unit6 they ma/e it easier to read in conte8t of other
code6 as in this e8am#le)
x = )"tiger" if fr-it=="redc-rrant" else "crocodile"*
Althou$h you can 0rite nested conditional e8#ressions6 you shouldnCt because they are
hard to read9 3he follo0in$ e8am#le is too hard to read as a sin$le statement9 &t should be
0ritten as an if bloc/ instead)
+ too7complex conditional expression
cleese = )"H-ngarian to-rist" if sAetc2 == "C2rase(ooA @Aetc2" else
)"H-stomer in searc2 of parrot" if sAetc2 == "Cet @2op @Aetc2" else
">res2 fr-it sergeant"*
*
+ easier7to7read if (locA
if sAetc2 == "C2rase(ooA @Aetc2"!
cleese = "H-ngarian to-rist"
elif sAetc2 == "Cet @2op @Aetc2"!
cleese = "H-stomer in searc2 of parrot"
else!
cleese = ">res2 fr-it sergeant"
:ecoratin" %our #ode
Decorator synta! #ro7ides a 0ay of modifyin$ functions and methods9 A decorator also
called a function decorator or function $rapper% is a function that ta/es a function as an
ar$ument and returns another function9
2!+
3@:<(&:A.
S3?FF
'efore decorator synta8 made its 0ay into 4ython6 #ro$rammers
modified functions and methods by callin$ the decorator function and
assi$nin$ the result bac/ to the ori$inal name of the function9
A standard use for decorators is to im#lement a cache that stores the results of function
calls9 3his is called memoi2ing9 &t 0or/s li/e this)
-9 3he first time you call the memoized function6 the 0ra##er calls the ori$inal
function to $et the result9 &t returns the result but also stores it in the cache9
29 When the #ro$ram calls the memoized function a$ain 0ith the same ar$uments6
the stored 7alue is returned9
3his s#eeds u# the #ro$ram because the ori$inal function doesnCt run a$ain9
&f you 0rote a decorator function called memoiQe)*6 you 0ould set it u# to 0or/ 0ith a
function by usin$ the follo0in$ synta89 3he Y character indicates a decorator9 3he
decorator comes on the line before the function or method definition)
YmemoiQe
def myf-nc)myarg*!
some code
Without decorator synta86 you 0rite the code this 0ay)
def myf-nc)myarg*!
some code
myf-nc = memoiQe)myf-nc*
D@M@M'@D When you #ass myf-nc as an ar$ument to memoiQe)* in the #recedin$
code6 myf-nc doesnCt ha7e #arentheses after it9 3hat is because you arenCt
callin$ myf-nc)* as a functionH you are #assin$ the function itself as an
ar$ument9
Some memoizin$ decorator functions can be found in the 4ython :oo/boo/)
htt#)LL0009as#n9acti7estate9comLAS4(L:oo/boo/
2!,
'ocusin" on 'unctions
Se7eral 4ython tools are desi$ned s#ecifically to 0or/ 0ith functional #ro$rammin$6
0hich is a /ind of #ro$rammin$ that 0or/s 7ia functions9
;ene8#s6 listcom#s6 and the itertools module are functional #ro$rammin$ tools
co7ered earlier in this cha#ter9 3his section introduces some more esoteric tools6 mostly
so you can reco$nize them in other #eo#leCs #ro$rams9
3i# &f you made an easy A in calculus6 you mi$ht enFoy functional #ro$rammin$9 &f you
ne7er made it to the calculus le7el6 donCt 0orry9 3he more com#le8 ty#es of
functional #ro$rammin$ arenCt necessary for most ty#es of #ro$rammin$ tas/s9
4ythonCs functional #ro$rammin$ tools include these features)
lam(da expressions create functions you can define in other function calls this is
useful if you donCt 0ant to create a 0hole se#arate function%9
Hi"her1order functions ta/e functions as in#ut and out#ut a function9
3i# Decorators are an e8am#le of hi$her1order functions9
'irst1class functions include functions that can be either
o Stored in a data structure for e8am#le6 usin$ the picAle module%
o 4assed as an ar$ument to another function
-ist comprehensions, 0hich 0e discuss earlier in this cha#ter
The map)*6 red-ce)*6 Qip)*6 and filter)* functions
(ary had a little labda
3he lam(da /ey0ord creates anonymous functions>functions that donCt ha7e names9
3he name lambda comes from .&S4 #ro$rammin$9% &t creates a function obFect the same
0ay the def /ey0ord does6 but the lam(da function obFect has #lam(da> in #lace of the
name of a function6 as sho0n9
>>> lam(da x! xD& is %
#f-nction #lam(da> at %x'2//'..>
3his code uses the abo7e lambda e8#ression to ma/e a list of the numbers from 0 to +A
that are di7isible by A)
>>> filter)lam(da x! xD& is %, xrange)E<**
[%, &, /%, /&, 2%, 2&, 3%, 3&, .%, .&, &%, &&, <%, <&, E%, E&]
A cou#le of other differences bet0een lambda functions and functions created 0ith the
def /ey0ord are as follo0s)
2!*
A lam(da is an e8#ression6 not a statement6 and it can a##ear any0here that an
e8#ression is allo0ed9
A lam(da e8#ression canCt ha7e statements in it such as x = 26 or if or try
statements%9
(apping it out
Another functional #ro$rammin$ tool is the map)* function9 &n its sim#lest form6 usin$
1one as the first ar$ument6 it turns a seGuence into a list6 or multi#le lists into a sin$le list
of tu#les9
&n the follo0in$ e8am#le6 the map)* function turns a strin$ into a list of sin$le1character
strin$s)
>>> mystring = "2ello Aitty"
>>> map)1one, mystring*
[,2,, ,e,, ,l,, ,l,, ,o,, , ,, ,A,, ,i,, ,t,, ,t,, ,y,]
&f you #ass a function as the first ar$ument6 map)* #erforms the functionCs action
on each element of the list or seGuence and stores the result in another list9
&f you #ass more than one seGuence6 the function needs a #arameter for each
seGuence9
&f the seGuences ha7e different len$ths6 map)* substitutes 1one 0hen there is no
corres#ondin$ 7alue in the shorter seGuence9
3i# &f you donCt need this seGuence1e8tendin$ beha7ior6 you should use Qip)*6 listcom#s6
or $ene8#s rather than map)*9 3heyCre sim#ler9
Applying filters
3he filter)* function ta/es a function 0hich can be a lambda% and a seGuence9 &t
#rocesses each element usin$ the function and returns a seGuence of the items for 0hich
the result of the function is 3rue9 For e8am#le)
>>> data = [/%, 2, E, &]
>>> filter)lam(da x! x>&, data*
[/%, E]
3i# Eou can also filter elements 0ith listcom#s9 See I:reatin$ a listcom# that a##lies a
filter6I earlier in this cha#ter9
/eductionis
3he red-ce)* function ta/es a function 0hich can be a lambda% and a seGuence9 &t
returns a sin$le 7alue constructed as follo0s)
-9 &t calls the function by usin$ the first t0o items of the seGuence as ar$uments and
returns the result9
2A0
29 &t calls the function by usin$ the result Fust returned and the ne8t item of the
seGuence9
"9 And so on until it runs out of items9
3his e8am#le #erforms x ; y on a list of ! items)
>>> mylist = [2, ., <, ']
>>> red-ce)lam(da x, y! x;y, mylist*
3'.
3i# For most #ur#oses6 red-ce)* has been re#laced by the built1in function s-m)*9 3he
s-m)* function ta/es a seGuence of numbers and adds them6 as sho0n)
>>> s-m)range)2%**
/=%
2A-
#hapter .H, &sin" ython's rimary
!ervices
&n this cha#ter6 you find out about many of the tools built into e7ery installation of
4ython9
We call this the I#rimary ser7icesI cha#ter because almost e7ery non1tri7ial #ro$ram uses
some but not all% of the features listed in this cha#ter9
ython, Batteries Included
3he tools that come 0ith 4ython are collecti7ely called the standard library. 3he standard
library contains commands6 modules6 and #ac/a$es9
3@:<(&:A.
S3?FF
Sometimes youCll see modules or #ac/a$es that come 0ith 4ython
referred to as libraries6 0hich is short for standard library module9
3his boo/ uses module and package6 thou$h9 :ha#ter -2 e8#lains the
difference bet0een modules and #ac/a$es9
3he standard library is li/e a basic education for 4ython6 allo0in$ it to 0or/ 0ith
different o#eratin$ systems ?(&O6 Windo0s6 and Mac MS6 for e8am#le%6 to handle
many /inds of data6 to na7i$ate the &nternet6 to 0or/ 0ith different human lan$ua$e
systems6 and so on9
3here are so many features that #eo#le often say that 4ython comes 0ith Ibatteries
included9I
D@M@M'@D
/emember to import modules
4ythonCs abilities come #ac/a$ed in modules so you donCt ha7e to thin/
about tools you arenCt usin$ ri$ht no09 3o use a tool6 you usually ha7e to
im#ort the module itCs stored in9
3he recommended 0ay to im#ort modules is to im#ort the 0hole module
by ty#in$
import modulename
We describe other 0ays to im#ort tools in :ha#ter -29
D@M@M'@D 3his /ey #oint 0ill sa7e you many many many hours of codin$)
2A2
'efore you start 0ritin$ a 4ython #ro$ram to #erform a function6 chec/
0hether the function already e8ists in 4ythonCs libraries9 <undreds of
#ro$rammers ha7e de7elo#ed these tools so you donCt ha7e to9
@ach ne0 7ersion includes lots of feature u#$rades and additions9 So 0hen you u#$rade
to a ne0 7ersion of 4ython6 be sure to read the IWhatCs (e0I Web #a$e for the 7ersion at
htt#)LL0009#ython9or$L9 3he ne0 features of 4ython 29A are a7ailable here)
htt#)LL0009docs9#ython9or$L
%ou +et All This<9The NNbuiltinNN 4odule
3he tools in the $$(-iltin$$ module are a7ailable 0hene7er you use 4ython9 Eou donCt
ha7e to im#ort them9 3o see 0hatCs in the module6 ty#e dir)$$(-iltins$$*9 DonCt
for$et the JsK>althou$h the module is called $$(-iltin$$6 the alias for the module is
$$(-iltins$$9 &f that bothers you6 you can ty#e import $$(-iltin$$ to use the actual
module name9%
&f you donCt reco$nize most of the names6 thatCs o/ay9 A lot of 0hatCs in $$(-iltin$$
0or/s behind the scenes6 at least 0hen youCre Fust startin$ out9 :urious2 ;et a descri#tion
of a #articular item in the library by ty#in$ 2elp)name of item*9
Most of the $$(-iltin$$ tools that you need to /no0 about are described else0here in
this boo/9 3his section describes a fe0 additional tools you mi$ht find useful9
Seeing &hat"s inside Python ob'ects
3he dir)* function returns an al#habetized list of an obFectCs attributes6 such as functions
or methods6 constants6 and the name of the obFect9
<ereCs ho0 to use the dir)* function)
3o see the names in the current names#ace6 ty#e dir)*9
&f you ty#e dir)* immediately after startin$ 4ythonCs interacti7e mode6 you 0ill
see somethin$ li/e this)
>>> dir)*
[,$$(-iltins$$,, ,$$doc$$,, ,$$name$$,]
2A"
3o see all the attributes named items% belon$in$ to an obFect6 ty#e dir)name of
object*9 For e8am#le)
>>> dir)Lrit2metic6rror*
[,$$class$$,, ,$$delattr$$,, ,$$dict$$,, ,$$doc$$,,
,$$getattri(-te$$,,
,$$getitem$$,, ,$$2as2$$,, ,$$init$$,, ,$$ne9$$,,
,$$red-ce$$,,
,$$red-ce$ex$$,, ,$$repr$$,, ,$$setattr$$,,
,$$setstate$$,,
,$$str$$,, ,$$-nicode$$,, ,args,, ,message,]
3o see the attributes of a s#ecific attribute6 se#arate the obFect name and the
attribute name 0ith a dot6 li/e this)
dir)Lrit2metic6rror"$$doc$$*
3i# Eou can also see some attributes by ty#in$ 2elp 0ith the name in
#arentheses6 for e8am#le)
>>> 2elp)Lrit2metic6rror*
3he dir)* function sho0s you these ty#es of attributes)
For a module6 it returns the moduleCs attributes9
For a ty#e or class obFect6 it returns the obFectCs attributes and those of its base
classes9
For any other obFect6 it returns the obFectCs attributes and the attributes of its class
and base classes9
/eading and &riting files
3he open)* function sets u# a file obFect in 4ython9 &t returns a file data ty#e9
Warnin$ 3he built1in documentation for open)* for 4ython 292 throu$h 29! has an error9
&t states or im#lies% that because open)* is an alias for file)*6 you should use
file)*9 3his is incorrect9 ?se open)* rather than file)* so that your code 0ill
0or/ 0ith all 7ersions of 4ython9
3he follo0in$ sections describe most of the ar$ument o#tions and the methods of the
open)* function9
(ptions for the open56 function
<ere are the rules and o#tions for open)*)
Eou should $i7e a name to the file youCre o#enin$6 or else it 0ill be difficult to do
anythin$ 0ith it9
3he first item in the #arentheses is a strin$ containin$ the filename9 &f the file
belon$s in a #articular directory6 ty#e the #ath to the directory9 For e8am#le)
,my$dir8my$file,
2A!
3he second item in #arentheses is a strin$ re#resentin$ the mode9 &f you lea7e this
out6 the mode defaults to ,r, for Iread1only9I 3he modes are described in 3able
-+1-9
3able -+1-) Modes of the o#en% Function
M#en table as s#readsheet
4ode 4eanin"
,r,
M#en the file as read1only allo0in$ no chan$es%9 3his is the default9
,9,
M#en the file for 0ritin$9 :reate the file if it doesnCt already e8ist9
Warnin$ ?sin$ ,9, erases anythin$ currently in the file9
,a,
M#en the file for a##endin$9 :reate the file if it doesnCt already e8ist9
,(,
3reat the fileCs contents as binary9
3his code is used mostly 0ith Windo0s9 Most other MSes these days donCt
differentiate bet0een te8t and binary files9
,:,
Allo0 both readin$ and 0ritin$9 Add it to the ,r, or ,9, mode li/e this)
,r:,
,4,
?se uni7ersal ne0line su##ortH for readin$ te8t files created on Mac6 ?(&O6
or Windo0s9 Wor/s only 0ith ,r,H doesnCt 0or/ 0ith J :,9 3y#e it on its
o0n or add it to ,r, mode li/e this) ,r4,
So to o#en a file for readin$6 ty#e code li/e this)
myfile = open)filename, ,r,*
!avin" chan"es to disk
3o sa7e file chan$es to dis/6 you need to use one or both of these methods)
3he fl-s2)* method forces 4ython to ta/e data in the internal buffer and sa7e it
to the file9 ?se it 0hen you 0ant to 0rite chan$es to dis/ 0ithout closin$ the file9
3he close)* method closes the file obFect that is created 0ith the open)*
method9 Eou canCt read from or 0rite to the file after you close it9 ?se it 0hen
youCre finished 0ith the file9
"eading %ile ontents
Eou can read the contents of a file in se7eral 0ays9 Which one you use de#ends on the
format you 0ant for the fileCs contents)
For a strin$6 use the read)* method9 &t returns the 0hole file or u# to a s#ecified
number of bytes as a strin$9 3his e8am#le returns 2+ bytes)
>>> myN-ote = open),N-otes"txt,*
2AA
>>> myN-ote"read)2E*
,Gf yo- liAe la9s and sa-sag,
For a list of strin$s each strin$ is one line of the file%6 use the read7lines)*
method9 3he follo0in$ e8am#le reads the 0hole file9
>>> myN-ote"readlines)*
[,Gf yo- liAe la9s and sa-sages, yo- s2o-ld Zn,, ,ne5er 9atc2
eit2er one (eing made" Zn,, ,77 Crince Jtto 5on IismarcA
)attri("*Zn,,
,Zn,, ,1ot to mention 9e( sites" Zn,, ,77 @FZn,]
For a strin$ re#resentin$ each line6 use a for loo#>0hich is also best if you 0ant
to read a file #iecemeal rather than in a bi$ $ul#9
>>> for line in myN-ote!
""" print line"rstrip)*
"""
Gf yo- liAe la9s and sa-sages, yo- s2o-ld
ne5er 9atc2 eit2er one (eing made"
77 Crince Jtto 5on IismarcA )attri("*
os"mAdir)* creates a directory 0ith the #ath you s#ecify9 S#ecify the #ath as a
strin$6 as follo0s)
os"mAdir),y1e9Oictory,*
3o set a directoryCs #ermissions6 s#ecify a four1di$it octal #ermissions mode as the
second ar$ument9 3he default mode is usually %EEE6 0hich $i7es e7eryone full
access to the directory6 but your o#eratin$ system mi$ht o7erride this default9
<ereCs ho0 to s#ecify the default)
os"mAdir),y1e9Oictory,, mode=%EEE*
os"maAedirs)* 0or/s li/e os"mAdir)*6 but it creates directories recursi7ely9 For
e8am#le6 if youCre in directory A and you 0ant to create directory ' that holds
directory : that holds directory D6 ty#e this)
os"maAedirs),I8H8O,*
Warnin$ os"maAedirs)* doesnCt 0or/ 0ith Windo0s ?ni7ersal (amin$
:on7ention ?(:% #aths6 0hich use the ZZ2ostZpat2 format9
os"remo5e)* deletes the item on the #ath s#ecified9 S#ecify the #ath as a strin$9 &t
doesnCt remo7e directories9 &n Windo0s it raises an error if the file is in use9 3he
code loo/s li/e this)
os"remo5e),B8test,*
os"rmdir)* remo7es a sin$le directory on the #ath s#ecified6 and
os"remo5edirs)* remo7es multi#le directories9 We recommend usin$
s2-til"rmtree)* rather than these functions9 See :ha#ter -*9
os"9alA)* creates a $enerator obFect that lists the files in a directory tree9 &t
$enerates a three1item tu#le directory #ath6 directory names6 filenames% and
e8amines the directory in to#1do0n default% or bottom1u# order9 3o ma/e and use
the $enerator obFect6 0rite code li/e this)
>>> for root, dirs, files in os"9alA),N-otes,*!
""" print root, dirs, files
Eou use three ames in the loo# because the $enerator ma/es a three1item tu#le9
os"tmpfile)* creates a ne0 tem#orary file9 3he file is o#ened for 0ritin$ and is
binary "9:("%9 &tCs automatically deleted 0hen you close it9
262
3i# 3he tempfile module #ro7ides more tools for creatin$ tem#orary filesH use
that instead of os"tmpfile)* if you 0ant more than the basics9
*anderin" down the os.path
3he os"pat2 module is a submodule of os9 &t contains tools that handle filenames so that
your #ro$ram can 0or/ on multi#le #latforms9
3i# We recommend that you im#ort the 0hole os module 0hen you need to use os"pat29
&f you donCt6 Mur#hyCs .a0 dictates that youCll need somethin$ else in the os module
and for$et you didnCt im#ort it and $et an annoyin$ error9
3@:<(&:A.
S3?FF
Many of the os"pat2 functions donCt 0or/ 0ith Windo0s ?ni7ersal
(amin$ :on7ention ?(:% #aths6 0hich use the ZZ2ostZpat2
format9
Descri#tions of some im#ortant os"pat2 tools follo09 All these tools ta/e #athnames as
ar$uments9 ?se strin$s to s#ecify #athnames9
os"pat2"Toin)* is one of the most im#ortant os"pat2 tools9 &t ta/es one or more
#aths and Foins them by usin$ the current o#eratin$ systemCs #ath se#arator9 &f any
com#onent is an absolute #ath6 all #re7ious com#onents are thro0n a0ay9 <ereCs
an e8am#le)
>>> os"pat2"Toin),-sers,, ,stef,, ,`-ote,*
Mn ?(&O this #roduces)
,-sers8stef8`-ote,
Mn Windo0s it #roduces)
,-sersZZstefZZ`-ote,
3i# Mn Windo0s6 os"pat2"Toin)"c!", "foo"* creates a #ath relati7e to the
current directory on dri7e H! instead of creatin$ c!ZZfoo9 3o $et an absolute
#ath on Windo0s6 use this synta8)
os"pat2"Toin),c!ZZ,, ,foo,*
os"pat2"a(spat2)* ta/es a relati7e #athname and returns the corres#ondin$
absolute #athname6 li/e so)
>>> os"pat2"a(spat2),`-ote,*
,8-sers8stef8`-ote8,
os"pat2"normpat2)* con7erts #ath names in nonstandard formats to standard
format9 For e8am#le)
>>> os"pat2"normpat2),stef88"8`-ote,*
,stef8`-ote,
os"pat2"split)* ta/es a #athname and returns it in t0o #arts) the directory #art
and the filename9
26"
>>> os"pat2"split),8-sers8stef8`-ote8N-ote2"ne9,*
),8-sers8stef8`-ote,, ,N-ote2"ne9,*
&f you s#ecify a directory6 it returns the #ath and an em#ty strin$6 li/e this)
>>> os"pat2"split),8-sers8stef8`-ote8,*
),8-sers8stef8`-ote,, ,,*
3i# Mn Windo0s systems usin$ ?(:6 use os"pat2"split-nc)* instead9
os"pat2"split)* has t0o related functions)
o os"pat2"dirname)* returns the directory name of the #ath the first half
of the #air that os"split)* returns%9
o os"pat2"(asename)* returns the filename the second half of the
os"split)* #air%9 &f no file 0as s#ecified6 the os"pat2"(asename)*
function returns an em#ty strin$9
os"pat2"splitext)* 0hich stands for Is#lit e8tensionI% s#lits the #athname
into a #air of #athnames)
o 3he first #art is the #athname6 includin$ the #art of the filename before the
e8tension9
o 3he second #art includes a #eriod and the fileCs e8tension9
o >>> os"pat2"splitext),8-sers8stef8`-ote8N-ote2"txt,*
o ),8-sers8stef8`-ote8N-ote2,, ,"txt,*
3@:<(&:A.
S3?FF
&f there is no e8tension or no file6 the second #art is an
em#ty strin$9
o 3his e8am#le #uts os"pat2"split)* and os"pat2"splitext)* to$ether)
o >>> dirname, filename =
os"pat2"split),-sers8stef8`-ote8N-ote2"txt,*
o >>> (asename, extension = os"pat2"splitext)filename*
o >>> dirname, filename
o ),-sers8stef8`-ote,, ,N-ote2"txt,*
o >>> (asename, extension
o ),N-ote2,, ,"txt,*
os"pat2"exists)* returns 0r-e if the s#ecified #ath e8istsH other0ise6 it returns
>alse9 &t also returns >alse for bro/en symbolic lin/s9
os"pat2"isdir)* returns 0r-e if the s#ecified #ath is an e8istin$ directory9 &t
follo0s symbolic lin/s9
!eein" environment variables
3he os module includes a dictionary6 os"en5iron6 that stores your o#eratin$ systemCs
en7ironment 7ariables 0hich contain user and system #references%9 3he 7ariables and
their 7alues are stored as strin$s9 3he dictionary is set u# 0hen you start 4ython9 &f you
chan$e your en7ironment 7ariables outside 4ython6 the dictionary doesnCt reflect the
chan$es until you restart 4ython9
3o see the 7alue of an en7ironment 7ariable6 ty#e os"en5iron and then6 in sGuare
brac/ets6 ty#e the name of the 7ariable in Guotation mar/s6 li/e this)
26!
>>> os"en5iron[,HJ6,]
,8-sers8stef,
Warnin$ Althou$h itCs #ossible to chan$e en7ironment 7ariables by chan$in$ the
os"en5iron dictionary6 itCs not safe to do so9 ?se the Copen)* function of the
s-(process module to chan$e en7ironment 7ariables instead9 3he chan$es
affect only the sub#rocesses9%
Subprocessing
3he s-(process module6 ne0 in 4ython 29!6 lets you interact 0ith the o#eratin$ system
to create ne0 #rocesses6 #ass information into and out of them6 and $et their return codes9
&t re#laces a number of functions and modules a7ailable else0here in 4ython6 includin$
the follo0in$)
os"system
os"spa9n
os"popen
popen2
commands
3he s-(process module is safer than the #recedin$ methods of startin$ ne0 #rocesses
because its default is to create a ne0 #rocess directly rather than creatin$ them throu$h
the shell9 3hat means that6 0ith s-(process6 you can safely #ass shell s#ecial characters9
3here are t0o 0ays to use the s-(process module) by creatin$ a Copen instance and by
usin$ the call)* function9 3he Copen method is more com#licated6 so 0e only document
the call)* function here9
Introducin" the call56 function
3he function s-(process"call)* is a shortcut for one of the more common uses of the
s-(process module9 &t runs a command6 0aits for the command to finish6 and then
returns its return code9 3his sim#le e8am#le lists the contents of a directory9 3he % after
the directory listin$ is a return code>a status messa$e that the sub#rocess sends9 Mn
?(&O6 % indicates that the sub#rocess com#leted successfully9
>>> s-(process"call)[,ls,, ,yOir,]*
copyy"py 2ello9orld"pyc myst-ff"Qip st-ff"Qip
copyy"pyc myspider"py pageco-nt"py st-ff2"Qip
2ello9orld"py myspider"pyc pageco-nt"pyc x
%
&seful callin" parameters
<ere are some of the more useful #arameters of the call)* function)
26A
args6 a strin$ or seGuence of #ro$ram ar$uments9 3he #ro$ram is the first item in
the seGuence6 and the other items are any command1line ar$uments the #ro$ram
ta/es9
&n the abo7e e8am#le6 args is a list9 3he #ro$ram is ,ls, and the command1line
ar$ument is ,yOir,9
3@:<(&:A.
S3?FF
Mn Windo0s6 args is con7erted to a strin$H the con7erted
7alue may not 0or/ 0ith all Windo0s a##lications9 &t 0or/s
0ith a##lications that su##ort the rules of MS : runtime9%
s2ell=0r-e s#ecifies to run the command throu$h the shell9 3he default shell is
s2 ?(&O% or 0hate7er is s#ecified by :MMS4@: Windo0s%9 3o s#ecify a
different shell6 use the exec-ta(le #arameter9
en5 defines en7ironment 7ariables for the sub#rocess9 3hese only a##ly to the
sub#rocessH they donCt chan$e any en7ironment 7ariables defined in your main
shell9
stdin6 stdo-t6 and stderr define ho0 data and error messa$es are transmitted to
and from the child #rocess9 Balues for these #arameters include CGC66 an e8istin$
file descri#tor a #ositi7e inte$er%6 an e8istin$ file obFect6 or 1one9 CGC6 creates a
ne0 #i#e to the child9 1one s#ecifies not to redirect messa$es the childCs file
handles are inherited from the #arent%9 3o send error messa$es to the same #lace
as other out#ut6 set stderr=@0OJ409
c9d s#ecifies a directory to chan$e to before the child #rocess runs9
Tryin" an (! command with call56
3his e8am#le code tries to run a command usin$ call)* and #rints information about the
success or failure of the attem#t9 3he name retcode stands for the return code that the
command sends9 3o use this try bloc/ in your o0n code6 Fust fill in your command and
ar$ument in #lace of "mycmd" : "myarg")
try!
retcode = s-(process"call)"mycmd" : " myarg", s2ell=0r-e*
if retcode # %!
print >>sys"stderr, "H2ild 9as terminated (y signal", 7retcode
else!
print >>sys"stderr, "H2ild ret-rned", retcode
except J@6rror, e!
print >>sys"stderr, "6xec-tion failed!", e
266
!tayin" on Time with the datetime and time 4odules
:om#uters usually store time as the number of seconds that ha7e #assed since some
s#ecific date called the epoch%9 3he e8act date 7aries de#endin$ on the o#eratin$ system9
3he ?(&O o#eratin$ system usually uses January -6 -*+09
<umans donCt thin/ of dates in terms of lar$e numbers of seconds6 so one of the functions
of the datetime and time libraries is to con7ert this machine time into date and time
information you can use9
4ython has t0o #rimary modules 0ith date and time tools)
3he datetime module includes tools for 0or/in$ 0ith dates6 times6 and
combinations thereof9 &t su##orts se7eral different calendar systems and 0ays of
#resentin$ time zone information but 0e donCt co7er time zones in this boo/%9
3he time module includes tools for 0or/in$ 0ith times and dates in the recent
#ast to near future9 &t focuses mostly on mani#ulatin$ time based on the
com#uterCs internal re#resentations of time9 For e8am#le6 the time module
includes a sleep)* method6 0hich lets you #ause e8ecution of your #ro$ram
0hile the com#uter counts do0n for you9
)sing the datetie odule
When you im#ort the datetime module6 you ha7e a7ailable a datetime)* obFect that
stores both date and time information9 Eou can also 0or/ 0ith dates and times se#arately
by usin$ date)* obFects and time)* obFects9 3hey 0or/ much the same 0ay as the
datetime)* obFect6 so 0e donCt co7er them here9
#omponents of a datetime56 ob>ect
An obFect that stores date and time information loo/s li/e this)
datetime"datetime)/=<=, E, 2%, 22, &<*
<ereCs more information about the com#onents)
datetime"datetime is the ty#e of obFect9
3he ar$uments are inte$ers6 se#arated by commas6 in this order)
o DeGuired ar$uments) year6 month6 and day of month
o M#tional ar$uments 0hich default to %%) hours6 minutes6 seconds6
microseconds9 Another o#tional ar$ument is time zone information6 0hich
defaults to 1one9 ?sin$ time zones in 4ython is an ad7anced feature
beyond the sco#e of this boo/9%
26+
3he #recedin$ obFect lea7es out seconds, microseconds, and time 2one
information9
When you s#ecify o#tional ar$uments6 you must s#ecify all ar$uments do0n to
the le7el of #recision you need by ty#in$ a number for any ar$uments to the left
of the ones you 0ant to s#ecify9 &tCs o/ay to lea7e out ar$uments to the ri$ht9 For
e8am#le6 to s#ecify no hours and A* minutes6 you ty#e % for the hours and &= for
the minutes9 Eou can lea7e out the seconds9 3he code loo/s li/e this)
datetime"datetime)/=</, /2, 3/, %, &=*
'ormattin" date and time information
4ython stores a datetime)* obFect in e8actly the same 0ay you entered it)
>>> moon9alA = datetime"datetime)/=<=, E, 2%, 22, &<, %*
>>> moon9alA
datetime"datetime)/=<=, E, 2%, 22, &<*
&f you 0ant the date and time in another format6 you ha7e a fe0 built1in o#tions6
includin$ the str)* function6 the ctime)* method6 and the isoformat)* method 0e
donCt document the latter t0o%9
3i# 3he most fle8ible 0ay to dis#lay a datetime)* obFect6 ho0e7er6 is to use the
strftime)* method IstrftimeI stands for Istrin$1format timeI%9 3his method ta/es a
strin$ as an ar$ument and uses D codes inside the strin$ to s#ecify ho0 to format
each element of the datetime)* obFect9 3he strftime codes 0or/ li/e the strin$
inter#olation codes described in :ha#ter 66 but the meanin$s of the codes themsel7es
are different9% 3he datetime)* formattin$ codes 7ary sli$htly by o#eratin$ system9
<ereCs one 0ay to format the moon9alA obFect)
-9 Type moon9alA"strftime9
29 (n the same line, in parentheses, type the followin" strin",
3" )"DL, DI Dd, DP DG!D!D@"*
3his strin$ tells 4ython to dis#lay the time usin$ a lon$ day of the 0ee/ DL%6 lon$
month DI%6 date of month Dd%6 four1di$it yearDP%6 and time9 &t uses commas and
colons as #unctuation9
<ereCs the line of code and the result)
>>> moon9alA"strftime)"DL, DI Dd, DP DG!D!D@"*
,@-nday, F-ly 2%, /=<= /%!&<!%%,
26,
Addin" time
When you #erform arithmetic on datetime)* obFects6 you $et the result in a
timedelta)* obFect9 &ts attributes are days, seconds6 and microseconds.
3@:<(&:A.
S3?FF
<ere is the timedelta)* obFect you $et 0hen you subtract the date
of the moon 0al/ from todayCs date in this e8am#le6 ItodayI is May
2-6 20066 a##ro8imately A)2A #9m9%9 3he e8am#le sho0s that -"6!A"
days ela#sed bet0een the first moon 0al/ on July 206 -*6*6 and May
2-6 2006)
>>> no9 = datetime"datetime"today)*
>>> moon9alA = datetime"datetime)/=<=, E, 2%, 22, &<*
>>> no9 7 moon9alA
datetime"timedelta)/3.&3, <<&=<, .%E=<'*
>>> print no9 7 moon9alA
/3.&3 days, /'!2=!&<".%E=<'
3he timedelta)* obFect 0or/s 0ith the follo0in$ o#erations) addition, subtraction,
multiplication, floor division, and absolute value. 3he timedelta)* obFect is immutable6
so it 0or/s as a dictionary /ey9
D@M@M'@D When you create a timedelta)* obFect6 you donCt ha7e to use days6
seconds6 and microseconds9 3hat 0ould be tedious for lar$e time
differences5%
Follo0 these ste#s to s#ecify a timedelta)
-9 Type datetime"timedelta9
29 (n the same line, in parentheses, specify any of the followin" keyword
ar"uments, separated by commas,
3" 9eeAs, days, 2o-rs, min-tes, seconds, microseconds, milliseconds
All the ar$uments are o#tional and if left out default to %9 For e8am#le6 to s#ecify
one day6 fi7e minutes6 and three seconds6 ty#e this)
datetime"timedelta)d#ys!0, min"tes!1, seconds!2*
3his is the timedelta obFect created and 4ythonCs result9 3he result doesnCt loo/ e8actly
li/e 0hat you entered because 4ython internally con7erts your data to days6 seconds6 and
microseconds9
>>> datetime"timedelta)/,3,%,%,&*
datetime"timedelta)/, 3%3*
When #rinted as a strin$6 the #recedin$ timedelta)* obFect loo/s li/e this)
>>> str)datetime"timedelta)/, 3%3**
26*
/ day, %!%&!%3
Taking your tie
3he time)* module includes tools for 0or/in$ 0ith the com#uterCs internal
re#resentation of time9 3ools de#end on your o#eratin$ system9
3he time)* module doesnCt handle times or dates in either
3he far #ast before -*00 A9D9%
3he far future
3@:<(&:A.
S3?FF
3he future time and date of the cutoff de#ends on your
o#eratin$ systemH for ?(&O systems6 itCs 20", A9D9
3o 0or/ 0ith far #ast or far future dates6 use the datetime)* module instead9
&sin" the time56 ob>ect
A time)* obFect is stored as a seGuence of nine inte$ers) year, month, day of month, hour,
minute, second, $eekday, day of year, and $hether daylight savings time is on % / 4 or off
% % 4.
3he time)* obFect is a tu#le in 4ython 29- and earlier9 &n later 7ersions6 it is another data
ty#e called str-ct$time)*9 WeCre tellin$ you this because you mi$ht see an error
messa$e referencin$ str-ct$time)* obFects9% 3ime is also e8#ressed as a floatin$ #oint
number6 for e8am#le6 by the time"time)* method9
3o create a time)* obFect that stores the current time and date6 ty#e this youCll $et a
different result6 of course%)
>>> time"localtime)*
)2%%<, 2, /, /', =, ., 2, 32, %*
3o see the local time as a strin$6 ty#e this)
>>> time"asctime)*
,Wed >e( / /E!3&!2= 2%%<,
3o format a time obFect as a strin$6 use the strftime)* method9 See IFormattin$ date
and time information6I earlier in this cha#ter9
2+0
-et's pause for a moment
3he time moduleCs sleep)* method is useful for #ro$rams that need built1in #auses9
Yuiz sho0s6 anyone2% 3he sleep)* method #auses the e8ecution of a #ro$ram for a
s#ecified number of seconds9
3he follo0in$ e8am#le uses the sleep)* method)
import time
for x in range)3*!
print "02e time is no9", time"asctime)*
print "1o9 sleeping for 2 seconds""""
time"sleep)2*
3his is the result of runnin$ the e8am#le code)
02e time is no9 @at >e( . /E!2'!%' 2%%<
1o9 sleeping for 2 seconds"""
02e time is no9 @at >e( . /E!2'!/% 2%%<
1o9 sleeping for 2 seconds"""
02e time is no9 @at >e( . /E!2'!/2 2%%<
1o9 sleeping for 2 seconds"""
8eepin" Track with the lo""in" 4odule
3he logging module is ne0 in 4ython 29"9 ?se it to set u# lo$ files or dis#lays for the
errors and debu$$in$ information for your #ro$rams9 4ython has 7ery fle8ible lo$$in$
featuresH 0e $et you started 0ith a fe0 basic 0ays to use lo$$in$9
Getting started &ith basic logging
3he tool that #rocesses messa$es is called a logger9 3he default lo$$er is called Iroot9I
.o$ messa$es are ta$$ed 0ith #riority codes6 and each #riority code has a method that
#roduces a lo$ messa$e of that #riority9 3he code methods are
de(-g)*, info)*, 9arning)*, error)*, critical)*
3o lo$ a messa$e6 follo0 these ste#s)
-9 Type logging6 a dot, and the method correspondin" to the messa"e priority,
for example,
2" logging"9arning
3i# 'y default6 the root lo$$er doesnCt do anythin$ 0ith debu$ or info messa$es9
&f you 0ant to see your messa$e 0ithout reconfi$urin$ the lo$$er6 use one of
these methods that indicate a hi$her #riority messa$e) 9arning)*6 error)*6
2+-
or critical)*9
"9 (n the same line, in parentheses, type a messa"e as a strin".
." logging"9arning(%*#tch o"t3%)
3he follo0in$ e8am#le tells the root lo$$er to lo$ a 0arnin$ messa$e and sho0s
the result you $et if you ty#e this in interacti7e mode)
>>> logging"9arning)"Watc2 o-t!"*
WLB1G1S!root!Watc2 o-t!
3@:<(&:A.
S3?FF
3he default beha7ior of the root lo$$er is as follo0s)
4rocess 0arnin$ messa$es and hi$her6 i$nore debu$ and info
messa$es
&f you try to use the info)* method 0ith the default root
lo$$er6 you $et no res#onse)
>>> logging"info)"0ry and see me"*
>>>
4rint messa$es as strin$s
.o$ messa$es to sys"stderr
&n interacti7e mode6 sys"stderr #rints to the screen9
#hanging the configuration of a logger
3o chan$e the default confi$uration of the root lo$$er6 or to set u# the confi$uration of a
lo$$er you 0rite yourself6 use the (asicHonfig)* function9 Fi$ure -+1- illustrates some
e8am#le (asicHonfig)* code9
Fi$ure -+1-) @8am#le of lo$$in$9 basic :onfi$% code9
3he /ey0ord ar$uments for (asicHonfig)* reGuire 4ython 29! and later9
1esigning your o&n logging syste
&f different #arts of your #ro$ram ha7e different lo$$in$ reGuirements6 hereCs some
$uidance to $et you started creatin$ your o0n lo$$ers9
2+2
3i# (ame your lo$$ers accordin$ to the #art of the #ro$ram youCre lo$$in$ errors from
and then confi$ure your lo$$er in these basic 0ays)
-evels of messa"es to process
3he le7els are associated 0ith inte$ers6 as follo0s)
o :D&3&:A.) A0
o @DDMD) !0
o WAD(&(;) "0
o &(FM) 20
o D@'?;) -0
'ormat of the messa"es
Handlers that determine where the messa"es should be lo""ed.
Most handlers are of the ty#e @treamHandler or >ileHandler9 3hereCs also
@0CHandler sends error messa$es 7ia e1mail%6 H00CHandler sends
messa$es to an <334 ser7er%6 and more9
Seeing a fe& logging functions
3hese are the most useful basic functions a7ailable in the lo$$in$ module)
log)*
.o$s a messa$e9 S#ecify o#tions in this order6 se#arated by commas) #riority le7el
as an inte$er%6 messa$e a formattin$ strin$ 0ith D codes%6 and the ar$uments to
include in the strin$9 Add exc$info at the end to include e8ce#tion information in
the messa$e9 3his e8am#le lo$s a 0arnin$ messa$e by usin$ D formattin$)
>>> logging"log)3%, "2ello, is t2at DsV", "yo-"*
WLB1G1S!root!2ello, is t2at yo-V
disa(le)*
Disables messa$es at the s#ecified #riority le7el and belo09 M7errides the le7els
s#ecified in lo$$ers9 S#ecify the #riority le7el as an inte$er9
s2-tdo9n)*
Shuts do0n the lo$$in$ system9 Flushes and closes all handlers9
#hapter .G, rocessin" Text
(verview
2+"
3here are 0hole boo/s on 4ythonCs te8t1#rocessin$ ca#abilities6 so this cha#ter co7ers
only the basics>three of the most im#ortant te8t1#rocessin$ modules that come 0ith
4ython9 &n this cha#ter6 you find out about
3he re module6 0hich brin$s the #o0er of re$ular e8#ression te8t searches to
4ython
3he @tringGJ library6 0hich lets you 0or/ 0ith strin$s as if they 0ere files
3he text9rap library6 0hich includes 7arious tools for tidyin$ u# #ara$ra#hs
3i# More te8t1#rocessin$ information can be found in :ha#ter 69
A 4illion *ays to re, %ou 8now That There Are
3he re module su##orts searchin$ te8t files by usin$ regular e!pressions rege!es for
short%>strin$s that s#ecify a set of match rules9
3he follo0in$ sections describe
3he character codes that you use to build re$e8es
3he search abilities of 4ythonCs re library
3i#
*hen not to use a re"ex
4ythonCs data ty#es include a number of easy1to1use te8t1search methods9 We
recommend that you $et familiar 0ith these methods and use them rather than the re
module if they 0ill fit your needs9
De$ular e8#ressions are com#le8 and hard to use6 and itCs easy to ma/e mista/es
0hen usin$ them9
For e8am#le6 if you ha7e a file of Guotations6 each se#arated by ,DDZn,6 you could
use a re$e8 to s#lit the file into se#arate Guotes9 'ut it 0ould be better to use the
split)* method of strin$s9
*riting a basic regex
3o create a re$e86 Fust 0rite it as a strin$>that is6 enclose some te8t in Guotation mar/s9
Eou can use sin$le or double Guotation mar/s6 or e7en tri#le Guotes9 Eour strin$ can be
Fust a set of characters to loo/ for6 or it can include s#ecial instructions9
2+!
3he follo0in$ strin$6 if used 0ith one of the re methods or functions6 uses the []
characters to $i7e s#ecial instructions to search for either monty or minty but not
mointy6 a city in =aza/hstan%)
,m[oi]nty,
3i# 'oth re$e8es and 4ython strin$s often use the bac/slash Z% to indicate s#ecial
treatment for a character9 3he re module reco$nizes 4ythonCs Z codes6 li/e Zn for
ne0line9 We discuss these codes in :ha#ter 69% 'ut to a7oid conflicts6 you should
usually 0rite re$e8es as ra0 strin$s by #re#endin$ r to the strin$9 3he r tells
4ythonCs strin$ data ty#e to i$nore any bac/slashes inside the strin$ and sa7e them
for the re method or function to #rocess9
Setting up a basic regex search
3he follo0in$ code $i7es the name Qipcode to a re$e8 s#ecified as a ra0 strin$% that
loo/s for the te8t Kip! follo0ed by any number of 0hite s#ace characters Zs;% and fi7e
di$its Zd%)
>>> Qipcode = r,Kip!Zs;ZdZdZdZdZd,
3@:<(&:A.
S3?FF
3o use the Qipcode re$e8 to search for a zi# code in a strin$6 ty#e
code li/e this)
>>> addr = "@treet address! 3.2 Lny92ere Bd Zn Kip!
32.33"
>>> re"searc2)Qipcode, addr*
&f the search is successful6 it returns an obFect of the ty#e
$sre"@B6$atc26 li/e this)
#$sre"@B6$atc2 o(Tect at %x'2%2<.%>
&f there is no match6 it returns 1one9
>>> mystring = "foo"
>>> print re"searc2)Qipcode, mystring*
1one
/egular expression characters and codes
3he search rules in a re$ular e8#ression can be sim#le the sim#lest useful re$e8 is a
sin$le character% or com#le89 :om#le8 rules are im#lemented by usin$ characters that
ha7e s#ecial meanin$s in re$ular e8#ressions9 We describe these characters and codes in
the follo0in$ sections9
!pecial characters
3able -,1- sho0s the most commonly used of the characters that ha7e s#ecial meanin$s
in re$e8es9
2+A
3able -,1-) De$ular @8#ression S#ecial :haracters
M#en table as s#readsheet
#haracter Action 2xample and ?otes
" dot% Matches any sin$le character e8ce#t a
ne0line9
,p"t2on, matches pyt2on6
p!t2on6 p2t2on6 but not
pyyt2on9
[ ]
@ncloses a set of characters to match a
character class%9 A match occurs if any one
of the characters is found9 Eou canenclose
s#ecific characters or a ran$e of characters6
such as all the u##ercase letters) [L7K]
,0[iy]ger, matches 0iger or
0yger but not 0iyger%9
&f you 0ant to match a hy#hen
inside a character class6 it must
be the first or last character9
[Zs"7] matches s#ace6 #eriod6
or hy#hen9
caret% At the be$innin$ of a character class6 stands
for Inot9I Mutside of a character class6
matches the be$innin$ of a line9
,[a], matches any character
that isnCt JaK9
,L, matches ,Lmerica, but
not ,Hanada,9
a
Matches the end of a line or strin$6 de#endin$
on the 430GJO6 fla$9 See 3able -,1"9%
,ta, matches ,(rig2t, but
not ,tender,9
regex# [
regex$
Matches either of t0o re$e8es6 startin$ 0ith
the leftmost and sto##in$ 0hen it finds a
match9 Eou can use multi#le [ characters to
Foin se7eral re$e8es9
,pyt2on, [ ,asp, first loo/s
in a strin$ for ,pyt2on,9 &f
,pyt2on, is not found6 it then
loo/s for ,asp,9
{ number }
Matches the #recedin$ re$e8 only if the re$e8
occurs a certain number of times
consecuti7ely9
,y{3}, matches ,pyyyt2on,
but not ,pyt2on,
)*
@ncloses a re$e8 $rou#9 Eou can refer to
$rou#s by number9 3he first $rou# is referred
to by Z/6 the second by Z26 and so on9
r ,C2one! )Zd{3}*7)Zd{3}7
Zd{.}*, loo/s for a #hone
number6 0ith the area code in
$rou# - and the number in
$rou# 29
Z/ refers to the area code $rou#
in the abo7e re$e89
Z
@sca#e code>treats the character that
follo0s as s#ecial6 or treats a s#ecial
character as te8t9 See the follo0in$ section9
,Za, searches for the a
character in te8t instead of
treatin$ a as an Iend of line or
strin$I code9
2+6
3able -,12) De$e8 :odes 0ith S#ecial Meanin$s
M#en table as s#readsheet
#ode Action
Z number
Matches the contents of the $rou# 0ith that number
ZL
Matches only at the start of a strin$
Z(
Matches an em#ty strin$ at the be$innin$ or end of a 0ord
ZI
Matches an em#ty strin$ in the middle of a 0ord
Zd
Matches any di$it %7=%
ZO
Matches any character that is not a di$it
Zs
Matches any 0hite s#ace character
Z@
Matches any character that is not 0hite s#ace
Z9
Matches any al#hanumeric character
ZW
Matches any character that is not al#hanumeric
ZK
Matches only at the end of a strin$
3able -,1") M#tional Fla$s for De$e8es
M#en table as s#readsheet
-on" ?ame !hort
?ame
4eanin"
&;(MD@:AS@ & Matches are case1insensiti7e9
.M:A.@ . Matches de#end on the current locale9
M?.3&.&(@ M Allo0s and a to match linefeeds Zn% inside a strin$9 Without
it6 they match only the be$innin$ % and end a% of a strin$9
DM3A.. S Dot "% matches any character includin$ ne0line9
?(&:MD@ ? Matches de#end on the ?nicode locale9
B@D'MS@ O Allo0 s#aces and comments in re$e8es9 3his ma/es them
easier for humans to read9
2scape codes with special meanin"s
When you use Z in front of some characters in a re$e86 it creates a code 0ith a s#ecial
meanin$9 3able -,12 lists these s#ecial re$e8 codes9 3he re module also reco$nizes the
4ython esca#e codes>for e8am#le6 Zn for ne0line9
'indin" repeats
Mne common re$e8 tas/ is to find re#eats of a character or other match9 Eou 0rite a re$e8
to find re#eats by usin$ the characters :6 ;6 V6 or {}9
4ython lets you search for re#eated characters in a number of 0ays)
2++
; matches the #re7ious character any number of times6 including none.
py;t2on matches pt2on6 pyt2on6 pyyt2on6 and so on9
: matches the #re7ious character one or more times9
py:t2on matches pyt2on and pyyt2on6 but not pt2on9
V matches the #re7ious character zero or one times9
pyVt2on matches pt2on and pyt2on6 but not pyyt2on9
(umbers enclosed in curly braces {}% create a ran$e of re#eated characters6 li/e this)
py{&}t2on matches an e8act number of re#eats) pyyyyyt2on
py{/,3}t2on matches a ran$e of re#eats) pyt2on6 pyyt2on6 and pyyyt2on9 &t
doesnCt match pt2on or pyyyyt2on9
3i# &f you lea7e out the first number of the ran$e for e8am#le6 py{,3}t2on%6 the
first ran$e defaults to 09 3his e8am#le 0ould match pt2on6 pyt2on6 pyyt2on6
or pyyyt2on9 &f you lea7e out the last number of the ran$e for e8am#le6
py{/,}t2on%6 any number of re#eats eGual to or lar$er than the first number
0ill match9
De$e8es su##ort t0o /inds of re#eatin$ searches) =reedy and non-greedy.
1reedy searhes
A $reedy search tries to match as many of the search characters as #ossible9
3he follo0in$ re$e8 $reedily searches for one or more y characters in a ro09 &t finds all
three y characters in the strin$ ,pyyyt2on,)
>>> p = ,pyyyt2on,
>>> greedy$re = 'y4'
>>> mymatc2 = re"searc2)greedy$re, p*
>>> mymatc2"gro-p)*
,yyy,
Non-greedy searhes
A non1$reedy search sto#s after it finds one match9
3o s#ecify a non1$reedy search6 add V after either
3he re#eat character
3he end brace of a ran$e
2+,
3he follo0in$ re$e8 searches for one or more y characters in a ro06 but the V character
s#ecifies a non1$reedy search9 As soon as this search finds one y6 itCs finished)
>>> p = ,pyyyt2on,
>>> non$greedy$re = 'y45'
>>> mymatc2 = re"searc2)non$greedy$re, p*
>>> mymatc2"gro-p)*
,y,
/e"ex "roups
De$e8es can be com#licated to read and to ty#e9 Mne 0ay of ma/in$ re$e8es easier to
read is to enclose a #ortion of a re$e8 inside #arentheses to ma/e a rege! group. 3his also
ma/es the re$e8 easier to use later because you can refer to the $rou# by ty#in$ a number
or name instead of rety#in$ the 0hole re$e89
4ython deals 0ith t0o ty#es of re$e8 $rou#s)
?umbered "roups, Whene7er you $rou# #art of a re$e8 inside #arentheses6 it
automatically $ets a number>/ for the first $rou#6 2 for the second6 and so on9
?amed "roups, 3o name a $rou#6 be$in the statement inside #arentheses 0ith
this code substitutin$ your name%)
VC#groupname>
Follo0 it 0ith the re$e8 and then ty#e a closin$ #arenthesis9
3he follo0in$ e8am#le creates a $rou# named Qip that searches for the te8t Kip!
follo0ed by any number of s#aces and e8actly fi7e di$its9
)VC#Qip>Kip!Zs;ZdZdZdZdZd*
Eou can refer to a numbered or named re$e8 $rou# in se7eral 0ays)
Eou can refer to a $rou# in any atc2J(Tect method that ta/es a re$e8 as an
ar$ument9 &n the e8am#les belo06 the gro-pdict)* method returns a dictionary
containin$ the $rou# name ,Qip, and its matches9 3he gro-p)* method returns
the matches of the second $rou#)
>>> my$regex = r,)VC#Qip>Kip!Zs;ZdZdZdZdZd*Zs;)@tate!Zs;Z9Z9*,
>>> addrs = "Kip! /%%/% @tate! 1P"
>>> y = re"searc2)my$regex, addrs*
>>> y"gro-pdict),Qip,*
{,Qip,! ,Kip! /%%/%,}
>>> y"gro-p)2*
,@tate! 1P,
When 0ritin$ a re$e86 you can refer to the ,Qip, $rou# by usin$ the follo0in$
synta8) )VC=,Qip,*
Eou can use a $rou# 0hen s#ecifyin$ re#lacement te8t) Zg#Qip>9
2+*
3i# For an e8am#le of a re$e8 0ith multi#le $rou#s6 see IMa/in$ re$e8es more
readable6I later in this cha#ter9
)sing the regular expression odule
3here are t0o 0ays to use the re module)
?se functions to #erform re$e8 o#erations on a strin$9
With the compile)* function6 turn the re$e8 into an obFect called a regular
e!pression ob-ectH in some 4ython messa$es6 itCs called an ,(1>Pattern ob-ect%
and use the obFectCs methods to 0or/ 0ith the re$e89
'unctional versus methodical
3i# &f you 0ant to use a sim#le re$e8 only once6 itCs o/ay to use the functions9 'ut for
code that uses re$e8es hea7ily6 0e recommend usin$ the com#iled form of re$e8es
for t0o reasons)
3he com#iled obFect 0or/s faster9
Dememberin$ the order of ar$uments is easier if you al0ays use the com#iled
form9
2sing a regex %untion
3o use a re$e8 function6 ty#e these elements)
3he function name
4arentheses
4ass any ar$uments inside the #arentheses6 se#arated by commas9 &he rege! is
al$ays the first argument.
3his e8am#le uses the findall)* function to return a list of all the 7o0els in a strin$9
>>> mystring = ,Po-r fat2er smelt of elder(erries!,
>>> print re"findall),[aeio-],, mystring*
[,o,, ,-,, ,a,, ,e,, ,e,, ,o,, ,e,, ,e,, ,e,, ,i,, ,e,]
Some re$e8 functions return a match obFect9 See IYueryin$ the match obFect6I later in
this cha#ter9
2sing a regex method
3o use a re$e8 method6 ty#e these elements)
3he name of the re$e8 obFect that you created by usin$ the compile)* function
2,0
See the ne8t section6 I:om#ilin$ re$ular e8#ressions9I
A dot
3he method name
4arentheses
4ass any ar$uments inside the #arentheses6 se#arated by commas9
3his e8am#le creates a re$e8 obFect that finds 7o0els and uses the findall)* method to
return a list of all the 7o0els in a strin$9
>>> mystring = ,Po-r fat2er smelt of elder(erries!,
>>> 5o9els = re"compile),[aeio-],*
>>> 5o9els"findall)mystring*
[,o,, ,-,, ,a,, ,e,, ,e,, ,o,, ,e,, ,e,, ,e,, ,i,, ,e,]
#ompilin" re"ular expressions
3o turn a re$e8 strin$ into a regular e!pression ob-ect6 you use the compile)* function9
compile)* ta/es a re$e8 strin$ and o#tional fla$s see 3able -,1"% as ar$uments9 <ereCs
an e8am#le)
>>> my$regex = re"compile),py:t2on,, re"GS1JB6HL@6*
>>> my$regex
#$sre"@B6$Cattern o(Tect at %x'22%%e%>
3he obFect contains the follo0in$ data attributes)
flags) A number corres#ondin$ to the fla$ used 0hen the re obFect 0as
com#iled9 See 3able -,1"9 3his is % if no fla$s 0ere used9
>>> my$regex"flags
2
gro-pindex) A dictionary ma##in$ any $rou# names to $rou# numbers9 3he
dictionary is em#ty if no $rou# names 0ere s#ecified9
pattern) 3he re$e8 from 0hich the obFect 0as com#iled9
>>> my$regex"pattern
,py:t2on,
Tools of the re module
3he follo0in$ re$e8 functions and methods are the most useful9
%indall&'
findall)* returns a list of all the occurrences of a re$e8 in a strin$9 &t also ta/es an
o#tional fla$ ar$ument 3able -,1" lists the fla$s%9
2,-
3i# finditer)* does the same thin$ as findall)* but returns an iterator obFect rather
than a list9
math&'
3o find a sin$le occurrence of a re$e8 at the start of a strin$6 use matc2)*)
matc2)* ta/es a strin$ and an o#tional fla$ as ar$uments 3able -,1" lists the
fla$s%9
matc2)* returns either
o A atc2J(Tect instance if there is a match%
See IYueryin$ the match obFect6I later in this cha#ter9
o 1one if there is no match%
D@M@M'@D &tCs easy to confuse matc2)*6 0hich matches only at the be$innin$ of a
strin$6 and searc2)*6 0hich matches any0here in a strin$9 See the
follo0in$ section9% Mne 0ay around this #roblem is to al0ays use
searc2)*6 s#ecifyin$ in your re$e8 to match only at the be$innin$ of a
strin$9
searh&'
3o find a sin$le occurrence of a re$e8 any$here in a strin$6 use searc2)*9
searc2)* ta/es a strin$ and an o#tional fla$ as ar$uments 3able -,1" lists the
fla$s%9
searc2)* returns either
o A atc2J(Tect instance if there is a match%
See IYueryin$ the match obFect6I later in this cha#ter9
o 1one if there is no match%
split&'
3o s#lit a strin$ 0here7er a re$e8 is found6 use split)*9
split)* ta/es these ar$uments)
o A strin$
o An o#tional inte$er s#ecifyin$ the ma8imum number of times to s#lit the
strin$
split)* returns a list of the substrin$s9
3his e8am#le sho0s the 0or/in$s of the split)* function)
2,2
>>> co9/ = "G ne5er sa9 a p-rple co9, YY G ne5er 2ope to see oneR"
>>> re"split),,, co9/* + f-nction
[,G ne5er sa9 a p-rple co9, ,, , G ne5er 2ope to see oneR,]
3his e8am#le uses the split)* method 0ith the same re$e8)
>>> co92 = "I-t G can tell yo- any2o9, YY G,d rat2er see t2an (e one""
>>> sep = re"compile),,*
>>> sep"split)co92*
[,I-t G can tell yo- any2o9, ,, " G,d rat2er see t2an (e one""]
su!&'
3o find all occurrences of some te8t in a strin$ and substitute other te8t6 use s-()*)
When s-()* finds a match6 it substitutes the re#lacement strin$9
s-()* returns either
o 3he chan$ed strin$ if any chan$es 0ere made%
o 3he ori$inal strin$ if no chan$es 0ere made%
s-()* ta/es these ar$uments)
A re#lacement strin$
3he ori$inal strin$
An o#tional inte$er
3he inte$er s#ecifies the ma8imum number of re#lacements9 &f you lea7e the
inte$er out6 all matches are re#laced9
3he follo0in$ code sho0s the result of re#lacin$ #art of a strin$)
>>> mystring = "6-ropean s9allo9"
>>> myregex = re"compile)"6-ropean"*
>>> myregex"s-()"Lfrican", mystring* + met2od
,Lfrican s9allo9,
>>> re"s-(),6-ropean,, ,Lfrican,, mystring* + f-nction
,Lfrican s9allo9,
Iueryin" the match ob>ect
A successful re$e8 search returns a 4ython obFect called a atc2J(Tect6 as illustrated in
the follo0in$ e8am#le)
>>> my$regex = re"compile),py:t2on,*
>>> mystring = "pyyyt2on is a s---per programming lang-age"
>>> mymatc2 = my$regex"matc2)mystring*
>>> mymatc2
#$sre"@B6$atc2 o(Tect at %x'2c<c2'>
2,"
3he atc2J(Tect stores information about 0here in the strin$ the match starts and ends6
the substrin$s% that 0ere matched6 and so on9 3o $et access to this information6 you use
the follo0in$ methods)
gro-p)*) 3a/es the $rou# inde8 number as an ar$umentH this defaults to 06 or the
first $rou#6 0hich returns the matched strin$9
>>> mymatc2"gro-p)%*
,pyyyt2on,
gro-ps)*) Deturns a tu#le containin$ the match found for each re$e8 $rou#9
3a/es an o#tional ar$ument to return for the re$e8 $rou#s that didnCt match
anythin$H this defaults to 1one9
3i# 3he re$e8 for the follo0in$ atc2J(Tect has t0o $rou#s6 one named $rou#
for zi# code and one numbered $rou# for the strin$ ,4@L,9
>>> y"gro-ps)*
),Kip! =./%/,, ,4@L,*
gro-pdict)*) Deturns a dictionary containin$ all the named sub$rou#s of the
match9 3he /eys are the sub$rou# names9 3a/es an o#tional ar$ument to return for
non#artici#atin$ $rou#sH this defaults to 1one9
>>> y"gro-pdict)*
{,Qip,! ,Kip! =./%/,}
span)*) Deturns a tu#le containin$ the startin$ and endin$ inde8 numbers that
0ere matched9 3a/es the $rou# inde8 number as an ar$umentH this defaults to 06 or
the first $rou#9
>>> mymatc2"span)%*
)%, '*
Eou can $et Fust the startin$ #osition 0ith start)* and Fust the endin$ #osition
0ith end)*9 'oth of these also ta/e the $rou# inde8 number ar$ument9
3i#
Additional re"ex resources
3he IDe$ular @8#ression <MW3MI Web #a$e at
htt#)LL0009am/9caL#ythonLho0toLre$e8L includes a basic tutorial on re$ular
e8#ressions9
De$e8 debuggers can hel# clarify re$e8 #roblems9 =odos6 the 4ython De$ular
@8#ression Debu$$er a7ailable at htt#)LL0009/odos9sourcefor$e9netL%6 is one such
tool9
A tool for #racticin$ and testin$ re$e8es comes 0ith 4ython 29!9 &tCs #art of the ;?&
tool /it 3/inter6 and itCs usually installed at 0ools8scripts8redemo"py9
=i/i6 0hich comes 0ith the W84ython installer6 has nice colorin$ for $rou#in$ in
re$e8es9 More info about W84ython can be found at htt#)LL000908#ython9or$L9
2,!
When youCre 0ritin$ a #ro$ram that uses a re$e8 to search a strin$6 you usually 0ant to
chec/ 0hether there 0as a match9 3o do this6 you chec/ 0hether the match obFect is
1one9 For e8am#le6 you mi$ht use this code remember that if mymatc2 is short for if
mymatc2 is not 1one%)
if mymatc2!
print ,atc2 fo-nd,
else!
print ,1o matc2,
4akin" re"exes more readable
Eou can use the W6BIJ@6 fla$ 0hen you 0rite a re$e8 to ma/e it more readable9 With this
fla$6 you can use comments and 0hite s#ace in your re$e89 <ereCs ho0 it 0or/s)
3he re$e8 inter#reter i$nores any 0hite s#ace that is outside sGuare brac/ets9
Anythin$ follo0in$ the + character on a line>as lon$ as + isnCt inside sGuare
brac/ets>is considered a comment rather than #art of the re$e89
?sin$ a ra0 strin$ 0ith tri#le1Guotes is the sim#lest 0ay to 0rite a multi1line
re$e8 0ith comments9
3his e8am#le searches te8t for a #hone number9 &t finds numbers 0ith or 0ithout
#arentheses around the area code6 and 0ith or 0ithout a s#ace6 #eriod6 or hy#hen as a
se#arator9
>>> p2one$re = re"compile)r,,,
""" Z)V + optionally matc2 (egin parent2esis
""" )VC#area>Zd{3}* + matc28name gro-p for area code
""" Z*V + optionally matc2 end parent2esis
""" [Zs"7]V + optionally matc2 space, period, or 2yp2en
""" )VC#prefix>Zd{3}* + matc28name gro-p for prefix
""" [Zs"7]V + optionally matc2 space, period, or 2yp2en
""" )VC#s-ffix>Zd{.}* + matc28name gro-p for s-ffix
""" ,,,, re"W6BIJ@6*
>>> p2one$re"s-()r,Zg#area>7Zg#prefix>7Zg#s-ffix>,, ,)2/3*&&&7/2/2,*
2/37&&&7/2/2
>>> p2one$re"s-()r,Zg#area>7Zg#prefix>7Zg#s-ffix>,, ,2/3"&&&"/2/2,*
2/37&&&7/2/2
!trin"s :is"uised as 'iles
3he c@tringGJ and @tringGJ modules su##ort Istrin$ buffersI also called memory
files% that let you #retend that a strin$ is a file9 3his is useful because some 4ython library
modules6 such as the tarfile module6 0or/ 0ith files but not 0ith strin$s9 4ython holds
the IfileI in memory instead of 0ritin$ the file to dis/9
2,A
!trin"I( versus c!trin"I(
3he c@tringGJ module im#lements the same functionality as @tringGJ9 c@tringGJ is
faster6 but it has a cou#le of limitations)
c@tringGJ doesnCt su##ort subclassin$9 ?se @tringGJ if you need to 0rite a
subclass of @tringGJ9
&f you $i7e ?nicode strin$s to c@tringGJ6 you must encode them as ?3F1, see
:ha#ter 6%9
#reating a StringI0 ob'ect
3here are t0o 0ays to create a @tringGJ obFect)
ass it a strin" as an ar"ument. 3his is called initiali2ing the buffer.
Do this if you 0ant to read or other0ise #rocess an e8istin$ strin$9
-eave out the ar"ument, 0hich creates the obFect 0ith an em#ty buffer9
Do this if you 0ant to 0rite to the obFect9
3o create a @tringGJ obFect6 follo0 these ste#s)
-9 Type from c@tringGJ import @tringGJ9
29 (n the next line, type code like one of the followin" lines.
Substitute the name of your file and your strin$ or its name%9
3o create an em#ty buffer6 ty#e this)
pretend$file = @tringGJ)*
3o initialize the buffer 0ith an e8istin$ strin$6 ty#e this)
pretend$file = @tringGJ)"mystring"*
StringI0 special ethods
3he methods for 0or/in$ 0ith @tringGJ files are the same as the methods for 0or/in$
0ith re$ular files described in :ha#ter -+%9
<ere are t0o differences)
2,6
3he @tringGJ obFect includes the get5al-e)* method6 0hich returns the entire
contents of the memory file as a strin$)
>>> pretend$file"get5al-e)*
,mystring,
3he @tringGJ close)* method frees u# the memory buffer9
A re$ular file obFectCs close)* method 0rites the contents to dis/9
ara"raph :umplin"s, 'illin" and *rappin" Text
3he text9rap module6 ne0 in 4ython 29"6 is for formattin$ #ara$ra#hs of te8t9 Eou can
#erform three basic formattin$ tas/s 0ith it) unindentin$ Idedentin$I%6 0ra##in$6 and
fillin$9 Eou can also create a customized 0extWrapper obFect9
/eo$ing indentation fro strings
3o remo7e 0hite s#ace that a##ears in front of e7ery line of the strin$6 use the dedent)*
function9 &t ta/es a strin$ as an ar$ument9
3@:<(&:A.
S3?FF
Dedent is meant to be the o##osite of
indent.
Mne use for this function is to ma/e tri#le1Guoted strin$s in a 4ython #ro$ram>
docstrin$s for e8am#le>line u# 0ith the left mar$in6 e7en thou$h they are indented in
the source code9 <ereCs an e8am#le)
>>> mystring = ,,, 0yger, 0yger, (-rning (rig2t
""" Gn t2e forests of t2e nig2t,,,
>>> print mystring
0yger, 0yger, (-rning (rig2t
Gn t2e forests of t2e nig2t
>>> print text9rap"dedent)mystring*
0yger, 0yger, (-rning (rig2t
Gn t2e forests of t2e nig2t
*rapping text by splitting it up
3o turn a lon$ strin$ into a list of strin$s6 one for each line of te8t6 use the 9rap)*
function9 &t ta/es a strin$ as an ar$ument6 and it also ta/es an o#tional 9idt2 ar$ument6
s#ecifyin$ the ma8imum len$th of any line it defaults to +0 characters%9 3he strin$s
returned donCt ha7e ne0line characters9
3he follo0in$ e8am#le sho0s the use of the 9rap)* function on a lon$ strin$)
>>> metap2or = """Z
""" Worse, many a(andon t2e pearl 92ic2 exists in t2e present in fa5or
""" of a cara5an of dreams 92ic2 al9ays recedes across t2e desert" 77
ixed
2,+
""" etap2or Hall of >ame"""
>>> m = text9rap"9rap)metap2or, 9idt2=.%*
>>> m
[,Worse, many a(andon t2e pearl 92ic2,, ,exists in t2e present in fa5or
of
a,,,cara5an of dreams 92ic2 al9ays recedes,, ,across t2e desert" 77
ixed
etap2or,, ,Hall of >ame,]
3i# Eou could use a for loo# to #rint the result of the 9rap)* function)
>>> for i in m!
""" print i
"""
Worse, many a(andon t2e pearl 92ic2
exists in t2e present in fa5or of a
cara5an of dreams 92ic2 al9ays recedes
across t2e desert" 77 ixed etap2or
Hall of >ame
<ereCs a shorter idiom for accom#lishin$ the same thin$)
print ,Zn,"Toin)m*
*rapping text by adding ne&line characters
3o add ne0line characters to a lon$ strin$6 use the fill)* function9 &t ta/es a strin$ as an
ar$ument and returns a strin$9 &t also ta/es an o#tional 9idt2 ar$ument6 s#ecifyin$ the
ma8imum len$th of any line it defaults to +0 characters%9
<ereCs an e8am#le of 0ra##in$ te8t 0ith the fill)* function)
>>> longstring = ,,,
""" I-t once yo- (-ild some islands of peace into yo-r daily ro-tine,
t2ey
""" 2elp ser5e as (eac22eads against t2e f-ll7co-rt press of life"
""" 77ixed etap2or Hall of >ame,,,
>>> 9rappedstring = text9rap"fill)longstring, 9idt2=.%*
>>> 9rappedstring
, I-t once yo- (-ild some islands ofZnpeace into yo-r daily ro-tine,
t2ey 2elpZnser5e as (eac22eads against t2e f-ll7Znco-rt press of
life" 77ixed etap2orZnHall of >ame,
>>> print 9rappedstring
I-t once yo- (-ild some islands of
peace into yo-r daily ro-tine, t2ey 2elp
ser5e as (eac22eads against t2e f-ll7
co-rt press of life" 77ixed etap2or
Hall of >ame
2,,
#reating a Text*rapper ob'ect
3he 9rap)* and fill)* functions 0or/ by creatin$ a 0extWrapper instance and callin$
a sin$le method on it9
3@:<(&:A.
S3?FF
3his instance $oes a0ay after you use it6 so for a##lications that
format many te8t strin$s6 itCs more efficient to create your o0n
0extWrapper obFect9
Text*rapper attributes
When you create a 0extWrapper obFect6 you can s#ecify any of the follo0in$ ar$uments9
Eou can also use them as /ey0ord ar$uments for the 9rap)* and fill)* functions9
9idt2) 3he ma8imum len$th of a 0ra##ed line9 3he default is +0 characters9
expand$ta(s) :han$es tabs to an eGui7alent number of s#aces bet0een - and ,%9
Default 0r-e9 &f set to >alse6 tabs are treated as sin$le characters9
replace$92itespace) :han$es all 0hite s#ace characters to s#aces9 Default
0r-e9
What counts as a 0hite s#ace character is defined by the 92itespace data
attribute of the string module9 3his de#ends on your locale and o#eratin$
system9 <ereCs our result)
>>> import string
>>> string"92itespace
,ZtZnZx%(Zx%cZr ,
3he meanin$s of the codes are) Zt tab%6 Zn ne0line%6 Zx%( 7ertical tab%6 Zx%c
form feed%6 Zr carria$e return%6 , , s#ace%9
Warnin$ &f you set expand$ta(s to >alse and replace$92itespace to 0r-e6
tabs are con7erted to sin$le s#ace characters9
initial$indent) Set this to a strin$ of fi7e s#aces or so to $et an indented line at
the be$innin$ of a #ara$ra#h9 3he default is an em#ty strin$9
s-(seN-ent$indent) 3his strin$ is added at the be$innin$ of all 0ra##ed lines
after the first9 Default is an em#ty strin$9
fix$sentence$endings) 4uts t0o s#aces bet0een sentences9
Warnin$ What text9rap thin/s a sentence is mi$ht not match 0hat you thin/ a
sentence is9 For e8am#le6 fix$sentence$endings #uts t0o s#aces after
IDr9I Default is >alse9
(reaA$long$9ords) 'rea/s 0ords lon$er than the 9idt2 attribute9 &f set to
>alse6 lines 0ith lon$ 0ords may e8ceed the s#ecified 0idth9 Default is 0r-e9
2,*
2xample of a custom Text*rapper ob>ect
<ereCs an e8am#le of a custom 0extWrapper obFect9 3his obFect s#ecifies a !01character
line and indents each line after the first one ! s#aces9
>>> my9rapper = text9rap"0extWrapper)9idt2=.%, s-(seN-ent$indent=" "*
>>> indented$string = my9rapper"fill)longstring*
>>> print indented$string
I-t once yo- (-ild some islands of
peace into yo-r daily ro-tine, t2ey
2elp ser5e as (eac22eads against t2e
f-ll7co-rt press of life" 77ixed
etap2or Hall of >ame
2*0
#hapter .J, :i""in" into :isk :ata
4ython includes a 7ariety of modules and #ac/a$es for 0or/in$ 0ith data files on dis/9 &n
this cha#ter6 you find out ho0 to
?se 4ython to 0or/ 0ith files on your hard dri7e6 includin$ com#ressed Qi# files9
?se 4ython to communicate 0ith databases9
3urn 4ython obFects such as dictionaries% into formats that can be stored or sent
o7er the &nternet9
!hell +ame, #opyin" and 4ovin" 'iles
3he s2-til module lets you use 4ython to mani#ulate6 co#y6 and remo7e files on your
hard dri7e IshutilI stands for Ishell utilityI%9
3i# Eou can use the os module to 0or/ 0ith hard dri7e files6 but s2-til is usually
sim#ler because it does a lot of the #ath mani#ulation behind the scenes9
Warnin$ &f youCre 0or/in$ on a Mac or your #ro$ram 0ill run on a Mac6 use the
macostools module6 not s2-til6 to co#y files and directories9 Mn Mac MS6 the
s2-til module does not reco$nize resource for/s and other metadata9 &f you
use s2-til to co#y files on a Mac6 the filesC resources6 file ty#es6 and creator
codes may not be correct9
Some of the useful s2-til functions are described in the follo0in$ list9 All the functions
ta/e #athname ar$uments as strin$s%>source #athname first6 follo0ed by destination
#athname if any%)
copy)*) :o#ies the source file and its #ermissions to the destination9
3@:<(&:A.
S3?FF
3he copy)* function has these rules)
o 3he destination can be a file or a directory9
o &f the destination is a directory6 the co#y has the same
name as the source file9
3his e8am#le co#ies the file ,myspider"py, to ,copyy"py, 0ithin the same
directory)
>>> import s2-til
>>> s2-til"copy),myspider"py,, ,copyy"py,*
copy2)*) .i/e copy)*6 but also co#ies the last access time and last modification
time9
2*-
copymode)*) :o#ies the #ermissions of the source file to the destination file9
Does not chan$e the file contents6 o0ner6 or $rou#9
copytree)*) :o#ies the entire contents of the source directory to the destination9
3he destination cannot already e8ist9 ?ses copy2)* to co#y files9 3his e8am#le
co#ies the contents of ,yOir, to ,y1e9Oir,)
>>> s2-til"copytree),yOir,, ,y1e9Oir,*
3his function also ta/es an o#tional third ar$ument6 symlinAs6 s#ecifyin$ 0hat to
do 0ith symbolic lin/s)
o 0r-e creates symbolic lin/s in the ne0 directory tree9
o >alse the default% co#ies the contents of symlin/ed files9
3@:<(&:A.
S3?FF
Startin$ in 7ersion 29"6 4ython raises an J@6rror if any #roblems occur
0hile im#lementin$ copytree)*9 &n earlier 7ersions6 4ython #rinted a
messa$e9% 3he J@6rror loo/s somethin$ li/e this)
>>> s2-til"copytree),yOir,, ,y1e9Oir,*
0race(acA )most recent call last*!
>ile "#stdin>", line /, in V
>ile
"8@ystem83i(rary8>rame9orAs8Cyt2on"frame9orA8Wersions82"38li(8
pyt2on2"38s2-til"py", line /%2, in copytree
J@6rror! [6rrno /E] >ile exists! ,y1e9Oir,
mo5e)*) (e0 in 4ython 29"9 Mo7es the source to the destination9
3@:<(&:A.
S3?FF
3he mo5e)* function has these rules)
o 3he destination cannot already e8ist9
o &f the source and the destination are on different file
systems6 mo5e)* first does a co#y to the destination
and then deletes the source9
o 3he #aths can be files or directories9
3his e8am#le mo7es a directory)
s2-til"mo5e),y1e9Oir,, ,""8y1e9Oir,*
rmtree)*) Deletes the s#ecified directory and its contents9
@rror messa$es de#end on the o#tional ar$ument ignore$errors)
o When set to 0r-e6 ignore$errors su##resses any error messa$es9
o 3he default is >alse6 0hich causes 4ython to raise an e8ce#tion if it
disco7ers an error 0hile e8ecutin$ rmtree)*9
2*2
An ad7anced feature also lets you customize the e8ce#tion handlin$9
3his e8am#le remo7es the directory that 0e mo7ed in the #re7ious e8am#le)
s2-til"rmtree),""8y1e9Oir,*
Dippin" and &n)ippin"
3he Qipfile and gQip modules su##ort data com#ression and e8#ansion9
6ipfile
3he Qipfile module #ro7ides access to Qi# files6 0hich are created 0ith WinQi# the
most common Windo0s file com#ression utility%6 the Mac MS I:reate Archi7eI
command6 or the ?(&O Qip utility9
3@:<(&:A.
S3?FF
&n Windo0s O46 Mac MS6 and ?(&O6 Qi# files are similar to folders
because they can hold files and folders%9 &f you select a folder in
Windo0s O4 and ma/e it a com#ressed folder6 itCs com#ressed as a
Qi# file9
3he Qipfile module lets you use 4ython to create6 read6 0rite6 a##end6 and list a
com#ressed Qi# file9
Warnin$ 3he Qipfile module doesnCt 0or/ 0ith either
Qi# files 0ith a##ended comments
Qi# files that s#an multi#le dis/s
'indin" out whether a file is a Dip file
3o identify a file as a Qi# file6 im#ort Qipfile and use the is$Qipfile)* function)
&ts ar$ument is a #athname strin$9
&t returns 0r-e if the file is a 7alid Qi# file9
3his e8am#le chec/s a file called ,st-ff"Qip,)
>>> import Qipfile
>>> Qipfile"is$Qipfile),yOir8st-ff"Qip,*
0r-e
Warnin$ DonCt ma/e incorrect assum#tions if is$Qipfile)* returns >alse9 &t also
returns >alse if the file doesnCt e8ist9
2*"
#reatin" a Dip'ile ob>ect for readin" or writin"
3o read or 0rite to a com#ressed Qi# file6 you create a Kip>ile)* instance9
3o create a Kip>ile obFect6 follo0 these ste#s)
-9 Type a name for the ob>ect and = Qipfile"Kip>ile)9
29 Type the path to your Dip file 5as a strin"6.
"9 If you want to write to the Dip file, type a comma and ,9,9
Warnin$ ?sin$ ,9, erases 0hate7er is currently in the file6 so ma/e doubly sure
that you donCt need to /ee# 0hat is already in the Qi# file before you $o
a##endin$ ,9,9
!9 3here is another mode6 ,a, for a##end9 3his is an ad7anced feature 0e donCt
e8#ound on in this boo/9
3i# &f you donCt 0ant to com#ress the stored data6 s/i# to Ste# A9
A9 If you want to compress the data, specify a compression type by typin" a
comma and compression= KGC$O6>3L06O9
KGC$@0JB6O the default% stores data 0ithout com#ression9
3@:<(&:A.
S3?FF
3he Qli( module must be a7ailable if you 0ant to com#ress
the data9
69 2nd by typin" *9
&f you fill in both o#tional #arameters6 your instance mi$ht loo/ li/e this)
>>> myQ = Qipfile"Kip>ile),yOir8x"Qip,, ,9,,
compression=Qipfile"KGC$O6>3L06O*
>>> myQ
#Qipfile"Kip>ile instance at %xE'%a'>
*orkin" with a Dip'ile ob>ect
Kip>ile instances are similar to file obFects9 3hey ha7e these methods)
close)* and read)*) Wor/ li/e the file)* obFect methods of the same names
see :ha#ter -+%9
getinfo)*) 3a/es a strin$ s#ecifyin$ the name of an item in the archi7e and
returns a KipGnfo)* obFect containin$ information about the item9
>>> myQ"getinfo),pageco-nt"py,*
#Qipfile"KipGnfo instance at %xE=fa'>
Mne 0ay to see the attributes of the KipGnfo)* obFect is to use the dir)*
function9 3he follo0in$ e8am#le loo/s at the contents of a KipGnfo)* obFect
called myinfo and then loo/s at its file size)
2*!
>>> dir)myinfo*
[,HBH,, ,>ileHeader,, ,$$doc$$,, ,$$init$$,, ,$$mod-le$$,,
,comment,,
,compress$siQe,, ,compress$type,, ,create$system,,
,create$5ersion,,
,date$time,, ,external$attr,, ,extra,, ,extract$5ersion,,
,file$offset,,
,file$siQe,, ,filename,, ,flag$(its,, ,2eader$offset,,
,internal$attr,,
,orig$filename,, ,reser5ed,, ,5ol-me,]
>>> myinfo"file$siQe
.22%3
infolist)*) Deturns a list of KipGnfo)* obFects for all items in the order theyCre
stored in the archi7e9
namelist)*) Deturns a list of strin$s>the names of the items in the archi7e>li/e
so)
>>> myQ"namelist)*
[,copyy"py,, ,2ello9orld"py,, ,myspider"py,, ,pageco-nt"py,, ,x,]
printdir)*) .ists the items in the archi7e to sys"stdo-t9
>>> myQ"printdir)*
>ile 1ame odified
@iQe
copyy"py 2%%<7%37%E /<!&<!&.
.22%
2ello9orld"py 2%%&7%E7%& /&!%/!2'
22
myspider"py 2%%&7//7%< 2/!2=!%2
.22%
pageco-nt"py 2%%&7/%7/% /&!%/!/%
3<E
x 2%%<7%272. /3!.=!.<
%
testQip)*) @8amines the chec/sums :D:s% of all the items in the archi7e9
Deturns as a strin$ the name of the first bad file detectedH if all files are $ood6
returns 1one9
9rite)*) 3a/es as an ar$ument a strin$ s#ecifyin$ a #athname and 0rites the
s#ecified file to the archi7e9
3he archi7e must be o#en for 0ritin$ or a##endin$9 9rite)* ta/es t0o o#tional
ar$uments)
o arcname6 a strin$ s#ecifyin$ the name for the file in the archi7e it defaults
to the filename%
o A com#ression ty#e6 either KGC$@0JB6O or KGC$O6>3L06O
3his e8am#le 0rites the file ,2ello9orld"py, to the archi7e and stores it 0ith
the name ,foo,)
myQ"9rite),yOir82ello9orld"py,, arcname=,foo,*
2*A
9ritestr)*) Writes a data strin$ to the archi7e9 3he archi7e must be o#ened 0ith
mode ,9, or ,a,9 3a/es t0o ar$uments)
o A strin$ s#ecifyin$ the name that the 0ritten data strin$ 0ill ha7e in the
archi7e or a KipGnfo)* instance 0ith at least a filename6 a date6 and a
time%
o A strin$ containin$ the data to store
myQipf"9ritestr)info, "my$data"*
3he Kip>ile)* instance has a data attribute6 de(-g6 0hich s#ecifies the le7el of debu$
out#ut to #rint to sys"stdo-t9 3he o#tions ran$e from % no out#ut% to 3 the most
out#ut%9 3he default is %9
Dippin" up a ython library
CyKip>ile)* instances create zi# archi7es containin$ 4ython libraries9 4ython can
im#ort modules from Qi# files9 4uttin$ the modules your #ro$ram uses into Qi# files is
one 0ay of deli7erin$ a smaller #ro$ram9
3o create a zi# archi7e 0ith 4ython files6 follo0 these ste#s)
-9 #reate a CyKip>ile)* instance.
?se the instructions in I:reatin$ a Qi#File obFect for readin$ or 0ritin$6I earlier
in this cha#ter9 M#en the archi7e for 0ritin$ or a##endin$9
Eour code 0ill loo/ somethin$ li/e this)
mypQ = Qipfile"CyKip>ile),foo"Qip,, ,9,*
29 (n the next line, type the name of your instance, a dot, and 9ritepy)9
"9 (n the same line, type a file or directory pathname and *9
&f you ty#e a filename6 the suffi8 must be "py9 3his e8am#le uses a directory
#athname)
mypQ"9ritepy),yOir,*
<o0 9ritepy)* 0or/s de#ends on the #athname you ty#e)
&f you ty#e a filename6 the file is added to the archi7e9
&f you ty#e a directory name6 all the 4ython files in the directory 0ith the suffi8es
"py6 "pyo6 or "pyc are added to the archi7e9 3he file locations de#end on the
#athCs location)
2*6
o &f the #ath is a re$ular directory6 the files are added to an archi7e at the to#
le7el of the directory9
o &f the #ath is a #ac/a$e directory6 the files are added under the #ac/a$e
name and subdirectory names if any%9
D@M@M'@D CyKip>ile)* obFects use the same methods as Kip>ile)* obFects9
g6ip
3he gQip module reads and 0rites com#ressed files that are com#atible 0ith the ;(?
#ro$ram gQip9 3o your #ro$ramCs users6 the contents of these files loo/ li/e ordinary files
that is6 not zi##ed%9
3@:<(&:A.
S3?FF
3he gQip module doesnCt su##ort all the formats that the ;(? gQip
utility su##orts9 For e8am#le6 compress and pacA arenCt su##orted9
3o use 4ythonCs gQip module6 use its open)* function to create a SQip>ile)* instance9
3he instance 0or/s li/e a file obFect6 but 0ith these differences)
3he SQip>ile)* instance doesnCt su##ort the readinto)* or tr-ncate)*
methods9
3he close)* method does not close the file obFect the instance is usin$9 &t closes
the instance only9
3his feature allo0s you to do somethin$ to the file obFect after 0or/in$ 0ith it as
a SQip>ile)* instance9 For e8am#le6 if the file obFect is a @tringGJ obFect6 you
can use the @tringGJ"get5al-e)* function to retrie7e the contents after closin$
the SQip>ile)* instance9
3o create a SQip>ile)* instance from a file on dis/6 follo0 these ste#s)
-9 Type gQip"open)
29 (n the same line, type the path to your ")ipped file as a strin". 'or example,
3" gQip"open)'filen#me.g6'
3i# &f you donCt 0ant to 0rite to the file6 s/i# to Ste# !9
!9 If you want to write to the file, type a comma and ,9,9
Warnin$ ?sin$ the ,9, mode erases 0hate7er is currently in the file6 so ma/e
sure that you donCt need to /ee# 0hat is already in the file before you $o
a##endin$ ,9,9
A9 3here is another mode6 ,a, for a##end9 3his is an ad7anced feature 0e donCt
document in this boo/9
3i# &f you 0ant the file to o#en and close faster6 you can tell $zi# to create a
faster, larger file)
a9 &ype a comma.
2*+
b9 &ype compressle5el=9
c9 &ype an integer from / %fastest, largest file4 to = %slo$est, smallest
file4.
'y default6 the compressle5el 7alue is = slo0est6 smallest file%9
69 2nd the line of code by typin" *9
&f you fill in both o#tional #arameters6 your code and the result mi$ht loo/ li/e
this)
>>> import gQip
>>> myfile = gQip"open),N-ote2"ne9"gQ,, mode=,9,, compressle5el='*
>>> myfile
#gQip open file ,N-ote2"ne9"gQ,, mode ,9(, at %x.(&c% %x&d.=%>
3@:<(&:A.
S3?FF
&tCs also #ossible to create a SQip>ile)* instance from a file or file1
li/e obFect such as a @tringGJ obFect% already o#ened in 4ython9
3his 0or/s much li/e usin$ the open)* function6 but the order of the
ar$uments is different)
-9 Mode o#tional6 defaults to the mode of the 4ython file
obFect%
29 :om#ression le7el o#tional6 defaults to * or
slo0estLsmallest%
"9 3he name of the 4ython file obFect reGuired%
3he creation of a SQip>ile)* instance mi$ht loo/ somethin$
li/e this)
>>> gfile = gQip"SQip>ile)mode=,9,,
compressle5el=', fileo(T=myfile*
2*,
!ussin" (ut !I- :atabases
3he sNlite3 module $i7es you an easy 0ay to store your data by usin$ the SY.ite
library6 an im#lementation of the uni7ersal database lan$ua$e SY.9 SY. is #ronounced
IseGuelI and stands for Structured Yuery .an$ua$e9 SY.ite is #ronounced IseGuel1iteI%9
sNlite3 is a ne0 standard module in 4ython 29A9 &f you ha7e 4ython 7ersion 292 throu$h
29!6 you can do0nload the essentially similar pysNlite module from
htt#)LL0009#ysGlite9or$L9
We donCt ha7e the s#ace for detailed information on usin$ SY.>SY. is a 0hole
#ro$rammin$ lan$ua$e all by itself9 'ut in the sections that follo06 0e sho0 you the
basics of usin$ sNlite3 as 0ell as some Guic/ tric/s for usin$ SY.9 ,? For Dummies,
Ath @dition6 by Allen 3aylor has more information9% 3he Web site htt#)LL0009sGlite9or$L
also has lots of documentation9
Installing S-Lite and s3lite<
3he sNlite3 module is automatically installed 0hen you install 4ython 29A9 Mn
Windo0s6 the 4ython 29A installation also includes SY.ite itself9 Mn other #latforms6
ma/e sure that SY.ite is installed before you build 4ython 29A9 See the B6LO6 file in
your 4ython do0nload for details9
Setting up a S-Lite database
3o create or use a SY.ite database6 you o#en a connection to the database itCs li/e
o#enin$ a file% and create a cursor to access the database9
Eou can o#en a connection in t0o 0ays)
:isk1based connection, Allo0s you to 0rite chan$es to the database file on dis/9
In1memory database, 3he connection 0or/s faster6 but you canCt 0rite chan$es
to dis/9 &tCs most useful for loadin$ some data into memory and then runnin$ a
bunch of Gueries9
3o o#en a connection to a database on dis/6 follo0 these ste#s)
-9 Type import sNlite39
29 (n the next line, type the code to open the connection,
o For a dis/1based connection6 ty#e the name of the database in the
#arentheses)
o conn = sNlite3"connect),my$data(ase,*
o For an in1memory database6 ty#e the s#ecial strin$ ,!memory!, in the
#arentheses9
2**
Eou can ty#e a #athname to an e8istin$ database or ty#e a ne0 filename to
create a ne0 database9
conn = sNlite3"connect),!memory!,*
"9 (n the next line, create a cursor by typin" code like this,
." c-rsor = conn"c-rsor)*
3he cursor is li/e a boo/mar/ tellin$ you 0here you are in the database9
*orking &ith a S-Lite database
3his section describes some basic tas/s you can do 0ith the database) creatin$ a table6
addin$ data to a table6 and $ettin$ data out of a table9
#reatin" a table
A table is an area in a database 0here you store related information9 A table consists of
records sets of information%6 0hich in turn contain fields each field is one bit of
information of a #articular type%9 3o create a table6 you 0rite a 7uery instructions to the
database%6 and then use the cursorCs exec-te)* method to run the Guery9 3o create a table
to store addresses6 you mi$ht use code li/e this)
N-ery = """ 77 sNlite N-eries are stringsR comments -se
"77"
create ta(le address 77 create a ta(le named "address"
) 77 (egin creating a record
name 5arc2ar, 77 create a field called "name", 9it2 type
"5arc2ar"
street 5arc2ar,
city 5arc2ar
* 77 end of t2is record
""" + end of N-ery
3he abo7e code is formatted so that itCs easier for humans to read9 SY. doesnCt care about
indentation in Guery strin$s9 3he te8t after 77 on each line re#resents comments9 4ython
i$nores these9
D@M@M'@D 3he code 5arc2ar in the #recedin$ e8am#le is a type a declaration of the
/ind of information to be stored in that field%9 5arc2ar s#ecifies that the
field contains te8t of 7aryin$ len$th9 SY.ite reGuires you to define a ty#e
for each field9 SY.ite doesnCt care much about ty#es6 but most other SY.
databases do9%
3i# 3hin/ of SY. tables as bein$ li/e 4ython classes e8ce#t that they only contain data6
not methods9 A SY. database can ha7e multi#le tables9 A record or ro$ the section
0ithin #arentheses in the abo7e e8am#le% is li/e a class instance9 3he fields or
columns of a record each line inside the #arentheses is one field% are li/e instance
data attributes6 e8ce#t that all records ha7e e8actly the same attributes the data
"00
associated 0ith the fields can be different%9 &n contrast6 class instances can ha7e
different attributes6 althou$h most of the time they donCt9
3@:<(&:A.
S3?FF
Actually6 most modern SY. databases do allo0 for creatin$ methods6
but theyCre an ad7anced and com#licated feature9 So #retend 0e
didnCt mention them9
opulatin" a table with data
After you ha7e a table6 you can populate the table add data to it%6 li/e this)
-9 #reate a list of tuples.
@ach element in a tu#le should corres#ond to a field in one of the databaseCs
records9 For e8am#le)
addresses = [
),Cet @2ops 3td,, ,/23 ain @t,, ,1otlo(,*,
),@imilar Cet @2ops 3td,, ,32/ >irst @t,, ,Iolton,*,
]
3i# For hel# arran$in$ data into the #ro#er format6 see :ha#ter ,9
29 &se the exec-te)* method in a for loop to write the addresses into the
database.
3" for addr in addresses!
." c-rsor"exec-te)"insert into address 5al-es )V, V, V*", addr*
3he exec-te)* methodCs first ar$ument is a strin$ 0ith the SY. command
insert into6 the name of the table6 and 5al-es follo0ed by a set of Guery
#arameters in #arentheses9 Yuestion mar/s V% stand for Guery #arametersH
sNlite3 #asses in an ar$ument for each #arameter9 3he number of Guestion mar/s
must match the number of elements in the second ar$ument9 .uc/y us>in the
e8am#le abo7e6 there are three Guestion mar/s and three elements in each addr
tu#le9 3he second ar$ument is the tar$et of the for loo#9
3i# ?se double Guotation mar/s or tri#le Guotes for all 4ython strin$s that contain SY.
Gueries9 SY. reGuires strin$ 7alues to be surrounded by sin$le Guotation mar/s see
the e8am#le in the I;ettin$ a subset of the 7aluesI section%6 so youCll ma/e life easier
for yourself if you a7oid usin$ sin$le Guotation mar/s for 4ython strin$s 0hen
0or/in$ 0ith SY.9
+ettin" field values from a table
3o $et field 7alues from a table6 you e8ecute a Guery s#ecifyin$ the data you 0ant and
then use the fetc2all)* method to retrie7e the data9 Eou may ha7e noticed by no0 that
most SY.ite tas/s in7ol7e e8ecutin$ a Guery9%
"0-
1etting all the values
3his e8am#le retrie7es a list of all the 7alues stored in our tableCs name field)
>>> c-rsor"exec-te)"select name from address"*
#sNlite3"H-rsor o(Tect at %x<<'3%>
>>> print c-rsor"fetc2all)*
[)-,Cet @2ops 3td,,*, )-,@imilar Cet @2ops 3td,,*]
3he fetc2all)* method returns a list of tu#les9 'ecause each tu#le in the result has only
one element6 the tu#les end 0ith a comma9 See :ha#ter , for a re7ie0 of tu#les9
D@M@M'@D SY.ite stores te8t data as ?nicode strin$s9 See :ha#ter 6 for more info
about ?nicode9
1etting a su!set o% the values
3he real #o0er of a database lies in its ability to $i7e you the subset of information that
matches your needs9 &n this e8am#le6 0e $et the records that ha7e I'oltonI in the city
field9 &f you /no0 that the ; character stands for Iall fields6I itCs #retty easy to understand
0hat this Guery does)
>>> c-rsor"exec-te)"select ; from address 92ere city=,Iolton,"*
#sNlite3"H-rsor o(Tect at %x<<'3%>
>>> print c-rsor"fetc2all)*
[)-,@imilar Cet @2ops 3td,, -,/23 ain @t,, -,Iolton,*]
3@:<(&:A.
S3?FF
When youCre startin$ out 0ith sNlite36 you should #robably stic/
0ith storin$ and retrie7in$ sim#le strin$s6 numbers6 and dates9 'ut
0hen you $et more comfortable6 you can store 4ython obFects in the
database by usin$ the picAle moduleCs d-mps)* function9 See the
ne8t section6 I4ic/lin$ Eour Data And Delishin$ the Mutcome%9I
(ote that you 0onCt be able to share your database 0ith non14ython
#ro$rams if you store #ic/led data9
3@:<(&:A.
S3?FF
sNlite3 follo0s the 4ython Database A4& S#ecification 7290 for
more details6 see 4ython @nhancement 4ro#osal 4@42!* at
htt#)LL0009#ython9or$Lde7L#e#sL#e#102!*L%9 'ecause 4ython
database modules are all desi$ned to 0or/ consistently6 0hen you
need a more #o0erful database such as 4ost$reSY. or Firebird%6 it
should be easy to u#$rade your code) Eou need only chan$e the
connect)* function call9
"02
icklin" %our :ata 5And /elishin" the (utcome6
3he picAle and cCicAle modules let you store data bet0een runs of a #ro$ram6 the 0ay
a 0ord #rocessor allo0s you to sa7e formatted documents and edit them later e7en if you
Guit the #ro$ram9 3his is called pickling because itCs a 0ay of preserving your data9
3@:<(&:A.
S3?FF
Storin$ a 4ython obFect in a file ma/es it a persistent ob-ect. Eou
could 0rite your o0n code to store and read 4ython obFects6 but if
your data includes com#le8 obFects6 itCs easier to use #ic/lin$ because
it automatically con7erts data bac/ and forth bet0een strin$s and
4ython obFects9
4ython includes the follo0in$ su##ort for #ersistent obFects)
3he cCicAle and picAle modules con7ert 4ython obFects to and from strin$
re#resentations9
3@:<(&:A.
S3?FF
4ic/lin$ is commonly used 0hen 0or/in$ 0ith #ersistent
databases6 0hich store 4ython obFects so that they can be
accessed by /eys9
3he s2el5e module see the ne8t section% su##orts #ersistent dictionaries 0hose
7alues can be arbitrary 4ython obFects9 &t 0or/s 0ith the picAle and anyd(m
modules9
Warnin$ 4ic/led data is a security ris/ because it mi$ht include malicious code9 Ma/e
sure you un#ic/le data only from a trusted source9
3he module of choice for the maFority of #ic/lin$ tas/s is cCicAle6 0hich is sim#ler and
faster than the picAle module9 3he only reason to use picAle is if you need access to its
ad7anced features 0hich 0e donCt co7er in this boo/%9
Pickling an ob'ect
3o #ic/le a 4ython obFect6 follo0 these ste#s)
-9 Type import cCicAle9
29 (pen a file for writin". 'or example,
3" mypicAlefile = open)myfile, ,9,*
!9 Type cCicAle"d-mp and in parentheses specify the name of the ob>ect you
want to pickle and the name of the file you opened 5which is where the
pickled ob>ect will be stored6.
&" cCicAle"d-mp)myo(T, mypicAlefile*
69 #lose the file.
E" mypicAlefile"close)*
icklin" restrictions
"0"
Most 4ython data ty#es can be #ic/led6 0ith the follo0in$ e8ce#tions)
Functions6 built1in functions6 and classes defined at a le7el other than the to# le7el
of a module canCt be #ic/led9
A class 0ith a $$dict$$ attribute or a $$setstate$$)* method that isnCt
com#atible 0ith #ic/lin$ canCt be #ic/led9
3u#les6 lists6 sets6 and dictionaries that contain non1#ic/lable obFects canCt be
#ic/led9
4ic/lin$ stores only the names of functions and classes6 not any code or attributes
from them9 So 0hen you un#ic/le a module6 its functions must be a7ailable for
im#ortin$ on the system youCre un#ic/lin$ from9
When instances are #ic/led6 class attributes arenCt #ic/led alon$ 0ith them9
3he follo0in$ functions #ic/le a 4ython obFect9 3he function you use de#ends on 0hat
ty#e of data you 0ant returned9
dumps56
3he d-mps)* function #ic/les a 4ython obFect and returns a strin$9 &tCs useful for storin$
4ython obFects in databases6 such as SY.ite see the #re7ious section6 ISussin$ Mut SY.
Databases9I%
3his function ta/es a 4ython obFect as an ar$ument9
dump56
3he d-mp)* function #ic/les the obFect and 0rites the #ic/led obFect to an o#en file or a
file1li/e obFect li/e @tringGJ%9
3his function ta/es t0o ar$uments)
A 4ython obFect
A file obFect o#ened for 0ritin$
3i# 3he d-mp)* and d-mps)* functions ta/e one o#tional #arameter that s#ecifies the
#ic/lin$ 7ersion to use9 3he follo0in$ subsections sho0 the ri$ht #arameter for your
7ersion of 4ython9
ython 7.$ or later
&f youCre usin$ 4ython 29" or later6 the #ic/lin$ 7ersion #arameter for the d-mp)* and
d-mps)* functions is called protocol9
3i# Eou can s#ecify the #ic/lin$ #rotocol 7ersion by enterin$ one of the 7alues from the
"0!
sidebar6 IAll about #ic/lin$ #rotocols9I
All about picklin" protocols
3here are three #ic/lin$ #rotocols)
% is an .,C// #rotocol9 &tCs com#atible 0ith earlier 7ersions of 4ython9
% is the default 7alue9
/ is a binary #rotocol also com#atible 0ith earlier 7ersions of 4ython%9
2 0or/s more efficiently 0ith 4ythonCs ne01style classes discussed in :ha#ter
-!%9
Eou can also s#ecify this 7ersion by enterin$ HGSH6@0$CBJ0JHJ3 or any negative
inte$er9
4rotocol 7ersion 2 is ne0 in 4ython 29"9
(lder ython versions
&f youCre usin$ a 7ersion of 4ython older than 29"6 the o#tional #ic/lin$ 7ersion #arameter
is called (in9 &t s#ecifies to #ic/le the data in a binary format9 3he default 7ersion is te8t6
0hich is less efficient9%
Warnin$ DonCt use (in in your code unless your #ro$ram must be com#atible 0ith earlier
7ersions of 4ython9
)npickling an ob'ect
3he loadin$ function for a #ic/led 4ython obFect de#ends on 0hether itCs a file or a strin$9
&npicklin" files
3he load)* function ta/es a file obFect or a file1li/e obFect such as @tringGJ% as an
ar$ument9 3he file should be o#ened in read mode9 load)* reads the contents of the file
obFect and un#ic/les it9 3he result is the ori$inal obFect that you #ic/led9
3@:<(&:A.
S3?FF
3o un#ic/le a 4ython obFect from a file6 follo0 these ste#s)
-9 Import cCicAle9
29 (pen for readin" the file containin" the pickled ob>ect.
3" mypicAlefile = open)myfile, ,r(,*
!9 Type a name for the ob>ect, = cCicAle"load6 and in
"0A
parentheses the name of the file you opened.
&" myo(T = cCicAle"load)mypicAlefile*
&npicklin" strin"s
3he loads)* function ta/es the name of a strin$ as an ar$ument9 &t reads the contents of
the strin$ and un#ic/les it9 3he result is the ori$inal obFect that you #ic/led9
&sin" shelve with :B41style databases
3his section is for #eo#le 0ho need to store 4ython data on dis/ usin$ 4ython 29! or
earlier6 or 0ho need to use the 'er/eley D' database library9 'er/eley D' and its
relati7es are called D0'-style databases9% &f you ha7e 4ython 29A6 read ISussin$ Mut
SY. Databases6I earlier in this cha#ter6 instead9 &tCs easier to use9 Eou can also do0nload
pysNlite for 4ython 292 throu$h 29!9%
Storing pickles on a shelf
3he s2el5e module is a I0ra##erI around se7eral D'M1style database modules
a7ailable in 4ython9 3he shel7e module 0or/s li/e this)
-9 Eou create a s2el5e obFect and tell it to o#en a database file9
29 3he s2el5e obFect loo/s at your database file on dis/ and decides 0hich 4ython
database module to use9
"9 3he 4ython database module accesses your database file9
3he s2el5e module and its tools ha7e the follo0in$ features)
3he open)* function automatically chooses an a7ailable database module 0hen
you o#en a file9
Shel7e obFects beha7e li/e dictionaries see :ha#ter *%6 0ith a fe0 minor
differences see I4ro#erties of a shel7e obFectI later in this cha#ter%9
Shel7e obFects allo0 you to store #ic/led 4ython obFects as 7alues9 See I4ic/lin$
Eour Data And Delishin$ the Mutcome%I earlier in this cha#ter9%
A shel7e obFect is called a persistent dictionary because it can store and reconstitute
4ython obFects from a dis/ file9
#reating a shel$e ob'ect
3o create a s2el5e)* obFect6 follo0 these ste#s)
-9 Type import s2el5e9
"06
29 (n the next line, type a name for the shelf, =6 and s2el5e"
open),mydata(ase,9
Substitute the name of your D'M1style database file9
"9 To open the file as read1only, to open an existin" file as read1write, or to
create a new, empty database, type a comma and a fla",
3he fla$s are)
,r,
read only
,9,
read and 0rite
,c,
create database if it doesnCt e8ist default%
,n,
create ne0 em#ty database
Warnin$ &f the file e8ists6 ty#in$ ,n, erases its contents9
mys2elf = s2el5e"open),mydata(ase,, 'w'
!9 If you have ython 7.$ or later and you want to use one of the advanced
picklin" protocols, type a comma6 protocol= and one of the followin",
For more about the #rotocols6 see I4ic/lin$ Eour Data And Delishin$ the
Mutcome%I earlier in this cha#ter9
o / binary #rotocol
o 2 or <&;<@S3P4DM3M:M. ad7anced #rotocolH 0or/s more efficiently
0ith 4ythonCs ne01style classes
mys2elf = s2el5e"open),mydata(ase,, ,9,, protocol=2
A9 To allow chan"es to mutable ob>ects in the database, type a comma and
9rite(acA=0r-e9
<" mys2elf = s2el5e"open),mydata(ase,, ,9,, protocol=2,
write7#c!(r"e
Warnin$ 3his feature ma/es the database slo0 to close9
3i# &f you donCt turn on the 9rite(acA feature6 you can still ma/e chan$es to
mutable obFects by usin$ a three1ste# #rocess described in the sidebar6
I:han$in$ mutable obFects in a shel7e database9I
,9 Type the end parenthesis.
=" mys2elf = s2el5e"open),mydata(ase,, ,9,, protocol=2,
9rite(acA=0r-e*
#han"in" mutable ob>ects in a shelve database
When you create a s2el5e obFect6 by default chan$es to mutable obFects are not stored in
the database file6 because allo0in$ this ma/es the database slo09 3o allo0 such chan$es6
you can set 9rite(acA=0r-e 0hen o#enin$ a s2el5e obFect see I:reatin$ a shel7e
obFectI%9 'ut if you lea7e it turned off6 you can use this three1ste# #rocess for ma/in$
"0+
chan$es to mutable obFects9 3hese ste#s assume youC7e already o#ened a s2el5e database
obFect9
-9 Access the mutable 7alue by usin$ its /ey6 and $i7e it a tem#orary name9 &n this
e8am#le6 the mutable 7alue is a list and its /ey is ,mylist,)
2" temp = mys2elf[,mylist,]
"9 Ma/e chan$es to the co#y9 &n this e8am#le6 0e a##end a strin$ to the list9
." temp"append),foo,*
A9 Assi$n the chan$ed co#y bac/ to the same database /ey9
<" mys2elf[,mylist,] = temp
Properties of a shel$e ob'ect
3he obFects created by the s2el5e module are li/e a cross bet0een a dictionary and a file
obFect9 As 0ith a dictionary6 you can retrie7e6 store6 search for6 and delete /eys and
7alues9 As 0ith a file obFect6 you can o#en and close a s2el5e obFect and direct it to 0rite
chan$es to a file on dis/9 s2el5e obFects ha7e these uniGue features)
=eys must be strin$s9
Balues can be strin$s or #ic/lable 4ython obFects9
:han$es to mutable obFects are not 0ritten to dis/ unless you set
9rite(acA=0r-e 0hen you create the shel7e obFect9
See the sidebar6 I:han$in$ mutable obFects in a shel7e database6I for some
0or/around 0ays of chan$in$ mutable obFects9
3he sync)* method 0rites chan$es to dis/9 Writin$ ha##ens automatically 0hen
you use the close)* method9%
3@:<(&:A.
S3?FF
When you 0rite chan$es to dis/ by usin$ the sync)* or
close)* method6 an e8tension mi$ht be added to the
filename and more than one file mi$ht be created9
Whether these ha##en de#ends on the underlyin$ database s2el5e is usin$9
"0,
#hapter 7K, Accessin" the Internet
4ython has an array of tools for 0or/in$ 0ith information o7er the &nternet9 3his cha#ter
introduces tools for handlin$ Web #a$e addresses6 do0nloadin$ and #arsin$ data from
Web #a$es6 buildin$ OM. data structures6 creatin$ and sendin$ e1mail messa$es6 and
0ritin$ and installin$ :;& scri#ts for #rocessin$ Web forms%9
3@:<(&:A.
S3?FF
3his cha#ter introduces conce#ts for 0or/in$ 0ith &nternet modules6
but 0e assume that you ha7e basic /no0led$e of Web6 OM.6 and e1
mail #rotocols9
:ownloadin" *eb :ata
3he follo0in$ sections co7er o#enin$ ?D.s6 readin$ their contents6 and submittin$ form
data to a Web ser7er9
4ython has t0o modules for #erformin$ these tas/s) -rlli( and -rlli(29 @ach has
s#ecific stren$ths9 We focus on the sim#ler -rlli( module in this section6 but 0e
recommend the -rlli(2 module for o#enin$ and fetchin$ data from Web #a$es because
it has better error handlin$9
0pening a )/L
3he function -rlopen)* creates obFects that beha7e a lot li/e files6 but it has t0o
im#ortant differences from the open)* function6 0hich creates files)
-rlopen)* ta/es a Web #a$e address ?niform Desource .ocator6 or ?D.% as an
ar$ument rather than as a filename%9
-rlopen)* o#ens the ?D. only for readin$6 not for 0ritin$9
3i# 'oth the -rlli( and the -rlli(2 modules su##ort the -rlopen)* function9 3he
function 0or/s the same 0ay in both6 but the -rlopen)* function in -rlli(2 has
better error re#ortin$ on #roblems that occur 0hen o#enin$ Web #a$es9
&sin" urllib7.urlopen56 to open a &/-
3o o#en a ?D.6 follo0 these ste#s)
-9 Type import -rlli(29
29 (n the next line, type a name for the ob>ect and = -rlli("-rlopen)9
"9 Type the &/- 5as a strin"6 and close the parentheses by typin" *9
Eour code should loo/ somethin$ li/e this)
"0*
page =
-rlli(2"-rlopen),2ttp!88999"pyt2on"org8doc8c-rrent8modindex"2tml,*
&f all $oes 0ell6 the Web #a$e o#ens9 &f you e8amine the obFect you Fust created6 youCll
see somethin$ li/e this)
>>> page
#addinfo-rl at /3E'.'E'% 92ose fp = #socAet"$fileo(Tect o(Tect at
%x'3Ec/(.>>
/eadin" text from a &/-
3o read te8t from an o#ened ?D.6 use a for loo#9 3his e8am#le #rints the te8t to
#sys"stdo-t> one line at a time)
for line in page!
sys"stdo-t"9rite)line*
&nderstandin" *eb pa"e errors
&f thereCs a #roblem o#enin$ the #a$e6 4ython raises an e8ce#tion s#ecifyin$ the /ind of
error9 Sometimes the messa$e is lon$6 so 0e 0onCt re#roduce the 0hole thin$ here9 3he
critical #art of the messa$e is the last line9 <ereCs #art of the messa$e you $et if the site
e8ists but the #a$e doesnCt e8ist)
>>> page=-rlli(2"-rlopen),2ttp!88999"pyt2on"org8idontexist"2tml,*
["""]
-rlli(2"H00C6rror! H00C 6rror .%.! 1ot >o-nd
Detailed error re#ortin$ is useful 0hen you 0ant to 0rite code that handles different Web
errors in different 0ays9 For e8am#le6 you mi$ht 0rite code that chec/s a Web #a$eCs
lin/s and lo$s 0hether the lin/ is still 7alid6 0hether it redirects to another #a$e6 or
0hether the lin/ed #a$e no lon$er e8ists9 3he spider"py #ro$ram in :ha#ter ! uses this
functionality9%
%inding inforation about a )/L
4ython has t0o useful tools for $ettin$ the scoo# on a ?D.9
info56
3o $et meta1information ser7er ty#e6 last1modified date6 content ty#e6 and so on% about a
?D. youC7e o#ened6 use the info)* method6 li/e this)
>>> print page"info)*
Oate! @-n, 2/ ay 2%%< %E!&2!/& S0
@er5er! Lpac2e82"%"&. )Oe(ian S1483in-x* OLW82 @W18/"/".
mod$pyt2on83"/"3
Cyt2on82"3"& mod$ssl82"%"&. Jpen@@38%"="Ee
"-0
3ast7odified! on, /E Lpr 2%%< /2!%.!%& S0
60ag! "2.%//%7'ac&73'//(E.%"
Lccept7Banges! (ytes
Hontent73engt2! 3&&2&
Honnection! close
Hontent70ype! text82tml
"eturl56
3o $et the actual ?D. that 0as o#ened 0hen you made your reGuest6 use the get-rl)*
method6 li/e this)
>>> redirect =
-rlli(2"-rlopen),2ttp!88999"li5eTo-rnal"com8-sers8firecat,*
>>> redirect"get-rl)*
,2ttp!88firecat"li5eTo-rnal"com8,
3i# ?se get-rl)* to find out 0hether the ?D. you o#ened redirected you some0here
else9
Processing special characters in a )/L
3o #rocess s#ecial characters such as a s#ace or tilde% in a ?D.6 use the -rlli(
moduleCs N-ote$pl-s)* function9
Warnin$ Althou$h the functions N-ote)* and -nN-ote)* are similar to N-ote$pl-s)*6
0e recommend usin$ N-ote$pl-s)* to a7oid #roblems 0ith ?D.s that contain
: and s#ace characters9
3he N-ote$pl-s)* function has a reGuired ar$ument and an o#tional ar$ument9
/e=uired ar"ument
3he N-ote$pl-s)* function reGuires a ?D. strin$ or #art of a ?D.% as an ar$ument and
does the follo0in$)
De#laces s#ecial characters anythin$ but letters6 di$its6 underscores6 dots6 and
hy#hens% by usin$ the "Dxx" esca#e format
De#laces s#aces 0ith : reGuired for Guotin$ <3M. form contents%
@sca#es : characters in the ori$inal strin$
3his e8am#le #rocesses a ?D. 0ith slash characters and a tilde \%)
>>> import -rlli(
>>> -rlli("N-ote$pl-s),2ttp!88cat7and7dragon"com8\stef,*
,2ttpD3LD2>D2>cat7and7dragon"comD2>DE6stef,
3i# 3o re7erse this #rocess6 use -nN-ote$pl-s)*9 3his function doesnCt use any o#tional
#arameters9%
"--
(ptional ar"ument
3he N-ote$pl-s)* function ta/es an o#tional safe ar$ument6 0hich s#ecifies characters
to lea7e alone9
3he follo0in$ e8am#le #rocesses the same ?D. as the #recedin$ e8am#le6 but the slash
character has been desi$nated as safe9
>>> -rlli("N-ote$pl-s),2ttp!88cat7and7dragon"com8\stef,, safe=,8,*
,2ttpD3L88cat7and7dragon"com8DE6stef,
Subitting for data
When communicatin$ 7ia <3346 the -rlopen)* function usually ma/es a S60 reGuest to
the <334 ser7er9 A S60 reGuest limits your ?D. address and data% to -602! bytes9 3o
send more data>for e8am#le6 to submit the results of a form>you need to ma/e a CJ@0
reGuest9
Sendin$ data 7ia a CJ@0 reGuest reGuires the follo0in$ ste#s9 (ote that these ste#s reGuire
you to im#ort both -rlli( for the -rlencode)* function% and -rlli(2 for the
-rlopen)* function%)
-9 Type the followin",
2" import -rlli(, -rlli(2
3"
!9 &se the -rlli("-rlencode)* function to encode the data.
3he function ta/es one of the follo0in$ ar$uments)
o A ma##in$ obFect such as a dictionary of form fields%
o A seGuence of t0o1element tu#les
3he function con7erts its ar$ument to the format application8x79997form7
-rlencoded9 <ereCs an e8am#le)
>>> mylist = [),/,, ,one,*, ),2,, ,t9o,*, ),3,, ,t2ree,*]
>>> mydata = -rlli("-rlencode)mylist*
>>> mydata
,/=one^2=t9o^3=t2ree,
A9 #all -rlli(2"-rlopen)* and pass the data from -rlencode)* as the second
ar"ument, like this,
<" x = -rlli(2"-rlopen),2ttp!88999"company"com,, mydata*
"-2
Tamin" the *ild &/-
3he -rlparse module s#lits ?D.s into com#onents6 combines com#onents into a ?D.6
and con7erts a relati7e ?D. to an absolute ?D.9
3o s#lit a ?D. strin$ into com#onents6 #ass it to the -rlparse)* function9 3here are t0o
o#tional #arameters for -rlparse)*)
defa-lt$sc2eme) ?sed if the ?D. doesnCt include an addressin$ scheme6 for
instance6 ,2ttp,9 3he default is an em#ty strin$ 0hich means itCs off%9
allo9$fragments) Allo0s ?D.s 0ith elements follo0in$ a + character
desi$natin$ a location some0here inside a #a$e%9 Defaults to /6 0hich means to
allo0 them9
3he -rlparse)* function returns a si81item tu#le of strin$s containin$
-9 Addressin$ scheme 2ttp or ftp%
29 (et0or/ location htt#)LL0009#ython9or$%
"9 4ath 8doc82"."28li(8mod-le7-rlparse"2tml%
!9 4arameters Rtype="a"%
A9 Yuery Vfilter=/<.%%%
69 Fra$ment identifier +top%
3he tu#le items donCt include delimiters colons and slashes%6 e8ce#t for a leadin$ slash in
the #ath com#onent9 &f any of the items isnCt #art of the ?D. bein$ #arsed6 the tu#le
contains an em#ty strin$ for that item9
3he follo0in$ e8am#le contains an addressin$ scheme6 net0or/ location6 and #ath6 but
doesnCt contain #arameters6 a Guery6 or a fra$ment identifier)
>>> -rlparse"-rlparse)"2ttp!88pyt2on"org8doc82"."28li(8mod-le7
-rlparse"2tml"*
),2ttp,, ,pyt2on"org,, ,8doc82"."28li(8mod-le7-rlparse"2tml,, ,,, ,,,
,,*
3he -rlparse module has a cou#le of other ca#abilities)
3o turn a tu#le created by -rlparse)* bac/ into an ?D. strin$6 #ass the tu#le to
-rl-nparse)*9
3o stic/ to$ether a base ?D. and a relati7e ?D. to create a com#lete ?D.6 use
-rlToin)*9 &t ta/es the base ?D. and relati7e ?D. as strin$s9 &t also ta/es the
o#tional allo9$fragments ar$ument9 3his e8am#le Foins a sin$le Web #a$e to its
location)
>>> -rlparse"-rlToin),2ttp!88pyt2on"org8doc82"."28mac8,,
,mac"2tml,*
"-"
,2ttp!88pyt2on"org8doc82"."28mac8mac"2tml,
+ettin" Hip with Hypertext
3o read and out#ut <3M.1formatted te8t files and to find lin/s in <3M. documents6 use
the 2tmlli( module9 &t su##orts all of O<3M. -906 all of <3M. u# to 2906 and much of
<3M. "90 and "929
0f parsers! foratters! and &riters
Deadin$ and out#uttin$ an <3M. file is a three1ste# #rocess9 <ereCs 0hatCs $oin$ on
inside 4ython 0hile itCs ha##enin$)
-9 A #arser recei7es information from an <3M.1formatted file9 3his file mi$ht be an
<3M. document you ha7e on dis/6 or it mi$ht be a Web #a$e youC7e o#ened 0ith
-rlli(2"-rlopen)* see IDo0nloadin$ Web Data6I earlier in this cha#ter%9
When the #arser encounters an o#enin$ ta$6 it stores 0hate7er is inside that ta$ in
a buffer until it encounters the matchin$ closin$ ta$9
29 3he #arser #asses the information to a formatter6 0hich reformats it into a human1
readable form by readin$ the <3M. ta$s and a##lyin$ instructions to them9
"9 3he formatter #asses the reformatted information to a 0riter6 0hich out#uts the
information9
Setting up a read+and+output process
3he #arser6 formatter6 and 0riter are all instance obFects6 so you need to create the
instances before they can act9 3he #arser 0or/s on the contents of the file or Web #a$e6
so you ha7e to o#en and read a file or Web #a$e before the #arser6 formatter6 and 0riter
can act9
3o set u# a read1and1out#ut #rocess6 follo0 these ste#s)
-9 Import the 2tmlli(6 formatter6 and @tringGJ modules, like so,
2" import 2tmlli(, formatter
3" from c@tringGJ import @tringGJ
!9 (pen an HT4-1formatted file,
&" myfile = open)"memo"2tml", ,r(,*
69 /ead the file,
E" 2tml = myfile"read)*
,9 #reate a writer instance.
3he sim#lest 0riter is O-m(Writer)*6 0hich out#uts #lain te8t9
"-!
d-md-m = formatter"O-m(Writer)@tringGJ)**
*9 #reate a formatter instance and pass it the writer instance.
L(stract>ormatter)* is the most commonly used formatter class9
fermat = formatter"L(stract>ormatter)d-md-m*
-09 #reate an H03Carser instance and pass it the formatter instance.
//"parsley = 2tmlli("H03Carser)fermat*
-29 'eed the file contents to the parser.
/3"parsley"feed)2tml*
-!9 #lose the parser and the file.
/&"parsley"close)*
/<"myfile"close)*
0utputting the links of a *eb page
After the #arser has read the document6 the documentCs lin/s are a7ailable in the data
attribute anc2orlist9
>>> parsley"anc2orlist
[,2ttp!88999"portmeirion75illage"com8,]
Getting help for essy .T(L
&f you ha7e to deal 0ith badly formatted <3M. and thereCs a lot of it out there%6 donCt
des#airH a third1#arty tool can hel#9 &tCs called 'eautiful Sou# after the #oem in .lice3s
.dventures in "onderland%6 and full details are a7ailable at
htt#)LL0009crummy9comLsoft0areL'eautifulSou#9
The +reat Q4-
4ython comes 0ith modules that su##ort @8tensible Mar/u# .an$ua$e OM.%6 a
mechanism for creatin$ structured documents in #lain te8t9
3@:<(&:A.
S3?FF
Althou$h 0e 0rote plain te!t abo7e6 OM. is actually often encoded
in ?3F1,9 See :ha#ter 6 for details about character encodin$9
3@:<(&:A.
S3?FF
'oth <3M. and OM. data include tags>te8t surrounded by
brac/ets #>%9 'ut they use ta$s differently9
&n <3M.6 ta$s are specific formatting instructions. For
e8am#le6 #p> starts a ne0 #ara$ra#h and #(> mar/s boldface
ty#e9
"-A
&n OM.6 ta$s label kinds of data.
An OM. ta$ can also ha7e subta$s for /inds of data that are
part of a lar$er $rou#>#address> data can include #name>
and #street>H #item> data can include #price>6 and so on9
3hese ta$s clarify the data structure6 so itCs easy for a
com#uter to se#arate the data into #arts9
3i# &f OM. ta$s and subta$s remind you of 4ythonCs classes and their data attributes6 you
mi$ht be on to somethin$S9
The ,leentTree =(L ipleentation
3he best 0ay to use OM. in 4ython is 0ith @lement3ree9
@lement3ree is included as xml"etree in 4ython 29A9
@lement3ree is a7ailable for do0nload from htt#)LL0009effbot9or$9zoneLelement1
inde89htm if youCre usin$ an earlier 7ersion of 4ython9
@lement3ree is based on an 6lement data ty#e6 0hich stores hierarchical data structures
such as OM.% in memory9
3@:<(&:A.
S3?FF
(rderin" up some Q4-
<ereCs an a##lication for 0hich OM. is useful9 Su##ose you need to
send an order9 &f you 0ere 0ritin$ it on a #iece of #a#er6 it 0ould
loo/ somethin$ li/e this)
Jrder!
6ric Half7a7Iee
/23 ain @t
Lnyto9n, HL, =&.32
Gtems!
>oo / a/2 a/2
Iar 2 a/& a3%
@-(total! a.2
@2ipping! a&
0otal! a.E
&n OM.6 it 0ould loo/ somethin$ li/e this)
#Vxml 5ersion="/"%" encoding="G@J7''&=7/"V>
#order>
#recipient>
#name>6ric Half7a7Iee#8name>
#street>/23 ain @t#8street>
#city>Lnyto9n#8city> #state>HL#8state>
#Qipcode>=&.32#8Qipcode>
#8recipient>
#items>
#item>
"-6
#prodcode>>oo#8prodcode>
#Nty>/#8Nty> #price>/2#8price>
#total>/2#8total>
#8item>
#item>
#prodcode>Iar#8prodcode>
#Nty>2#8Nty> #price>/price>
#total>3%#8total>
#8item>
#8items>
#s-(total>.2#8s-(total>
#s2ipping>s2ipping>
#total>.E#8total>
#8order>
Importin" 2lementTree
<o0 you im#ort @lement3ree de#ends on your 7ersion of 4ython9
&n 4ython 29A6 ty#e this)
from xml"etree import 6lement0ree as 60
&n earlier 7ersions6 after you do0nload @lement3ree6 ty#e the follo0in$)
import elementtree"6lement0ree as 60
3i# Eou donCt ha7e to use the as 60 #art6 but it 0ill hel# if you are follo0in$
alon$ 0ith our e8am#les9%
#reatin" an element
An @lement3ree structure consists of 6lement instances9
An 6lement instance must ha7e a tag>a strin$ identifyin$ the element ty#e9
An 6lement instance can ha7e these o#tions) attributes, a te!t string, and
subelements child elements%9
3o create an element6 ty#e 60"6lement and #ass the ta$ strin$ as an ar$ument6 li/e this)
tree = 60"6lement)"tree"*
2lement attributes
@lement attributes are dictionary /ey)7alue #airs9 3he /eys must be uniGue9%
,ssigning attri!utes
3o assi$n attributes to an e8istin$ element6 use the attri()* method6 li/e this)
(ranc2 = 60"6lement)"(ranc2"*
"-+
(ranc2"attri(["one"] = "/"
(ranc2"attri(["t9o"] = "2"
3anipulating attri!utes
3o mani#ulate attributes6 use the follo0in$ methods6 0hich 0or/ a lot li/e dictionary
methods see :ha#ter *%)
3o assi$n attributes 0hen creatin$ an element6 use /ey0ord ar$uments6 li/e this)
(ranc2 = 60"6lement)"(ranc2", one="/", t9o="2"*
3o return attribute 7alues6 use the get)* method 0ith the /ey)
(ranc2"get)"one"*
3o $et a list of attribute /eys6 use the Aeys)* method6 li/e so)
(ranc2"Aeys)*
3o $et a list of attributes /ey)7alue tu#les%6 use the items)* method)
(ranc2"items)*