%!PS-Adobe-2.0 %%Creator: dvips(k) 5.96.1 Copyright 2007 Radical Eye Software %%Title: paper-letter/tut.dvi %%CreationDate: Fri Dec 19 16:19:43 2008 %%Pages: 128 %%PageOrder: Ascend %%BoundingBox: 0 0 612 792 %%DocumentFonts: Helvetica Helvetica-Oblique Times-Roman Times-Bold %%+ Times-Italic Courier Helvetica-Bold Courier-Bold %%DocumentPaperSizes: Letter %%EndComments %DVIPSWebPage: (www.radicaleye.com) %DVIPSCommandLine: dvips -N0 -t letter -o paper-letter/tut.ps %+ paper-letter/tut.dvi %DVIPSParameters: dpi=600 %DVIPSSource: TeX output 2008.12.19:1619 %%BeginProcSet: tex.pro 0 0 %! /TeXDict 300 dict def TeXDict begin/N{def}def/B{bind def}N/S{exch}N/X{S N}B/A{dup}B/TR{translate}N/isls false N/vsize 11 72 mul N/hsize 8.5 72 mul N/landplus90{false}def/@rigin{isls{[0 landplus90{1 -1}{-1 1}ifelse 0 0 0]concat}if 72 Resolution div 72 VResolution div neg scale isls{ landplus90{VResolution 72 div vsize mul 0 exch}{Resolution -72 div hsize mul 0}ifelse TR}if Resolution VResolution vsize -72 div 1 add mul TR[ matrix currentmatrix{A A round sub abs 0.00001 lt{round}if}forall round exch round exch]setmatrix}N/@landscape{/isls true N}B/@manualfeed{ statusdict/manualfeed true put}B/@copies{/#copies X}B/FMat[1 0 0 -1 0 0] N/FBB[0 0 0 0]N/nn 0 N/IEn 0 N/ctr 0 N/df-tail{/nn 8 dict N nn begin /FontType 3 N/FontMatrix fntrx N/FontBBox FBB N string/base X array /BitMaps X/BuildChar{CharBuilder}N/Encoding IEn N end A{/foo setfont}2 array copy cvx N load 0 nn put/ctr 0 N[}B/sf 0 N/df{/sf 1 N/fntrx FMat N df-tail}B/dfs{div/sf X/fntrx[sf 0 0 sf neg 0 0]N df-tail}B/E{pop nn A definefont setfont}B/Cw{Cd A length 5 sub get}B/Ch{Cd A length 4 sub get }B/Cx{128 Cd A length 3 sub get sub}B/Cy{Cd A length 2 sub get 127 sub} B/Cdx{Cd A length 1 sub get}B/Ci{Cd A type/stringtype ne{ctr get/ctr ctr 1 add N}if}B/CharBuilder{save 3 1 roll S A/base get 2 index get S /BitMaps get S get/Cd X pop/ctr 0 N Cdx 0 Cx Cy Ch sub Cx Cw add Cy setcachedevice Cw Ch true[1 0 0 -1 -.1 Cx sub Cy .1 sub]{Ci}imagemask restore}B/D{/cc X A type/stringtype ne{]}if nn/base get cc ctr put nn /BitMaps get S ctr S sf 1 ne{A A length 1 sub A 2 index S get sf div put }if put/ctr ctr 1 add N}B/I{cc 1 add D}B/bop{userdict/bop-hook known{ bop-hook}if/SI save N @rigin 0 0 moveto/V matrix currentmatrix A 1 get A mul exch 0 get A mul add .99 lt{/QV}{/RV}ifelse load def pop pop}N/eop{ SI restore userdict/eop-hook known{eop-hook}if showpage}N/@start{ userdict/start-hook known{start-hook}if pop/VResolution X/Resolution X 1000 div/DVImag X/IEn 256 array N 2 string 0 1 255{IEn S A 360 add 36 4 index cvrs cvn put}for pop 65781.76 div/vsize X 65781.76 div/hsize X}N /p{show}N/RMat[1 0 0 -1 0 0]N/BDot 260 string N/Rx 0 N/Ry 0 N/V{}B/RV/v{ /Ry X/Rx X V}B statusdict begin/product where{pop false[(Display)(NeXT) (LaserWriter 16/600)]{A length product length le{A length product exch 0 exch getinterval eq{pop true exit}if}{pop}ifelse}forall}{false}ifelse end{{gsave TR -.1 .1 TR 1 1 scale Rx Ry false RMat{BDot}imagemask grestore}}{{gsave TR -.1 .1 TR Rx Ry scale 1 1 false RMat{BDot} imagemask grestore}}ifelse B/QV{gsave newpath transform round exch round exch itransform moveto Rx 0 rlineto 0 Ry neg rlineto Rx neg 0 rlineto fill grestore}B/a{moveto}B/delta 0 N/tail{A/delta X 0 rmoveto}B/M{S p delta add tail}B/b{S p tail}B/c{-4 M}B/d{-3 M}B/e{-2 M}B/f{-1 M}B/g{0 M} B/h{1 M}B/i{2 M}B/j{3 M}B/k{4 M}B/w{0 rmoveto}B/l{p -4 w}B/m{p -3 w}B/n{ p -2 w}B/o{p -1 w}B/q{p 1 w}B/r{p 2 w}B/s{p 3 w}B/t{p 4 w}B/x{0 S rmoveto}B/y{3 2 roll p a}B/bos{/SS save N}B/eos{SS restore}B end %%EndProcSet %%BeginProcSet: 8r.enc 0 0 % File 8r.enc TeX Base 1 Encoding Revision 2.0 2002-10-30 % % @@psencodingfile@{ % author = "S. Rahtz, P. MacKay, Alan Jeffrey, B. Horn, K. Berry, % W. Schmidt, P. Lehman", % version = "2.0", % date = "27nov06", % filename = "8r.enc", % email = "tex-fonts@@tug.org", % docstring = "This is the encoding vector for Type1 and TrueType % fonts to be used with TeX. This file is part of the % PSNFSS bundle, version 9" % @} % % The idea is to have all the characters normally included in Type 1 fonts % available for typesetting. This is effectively the characters in Adobe % Standard encoding, ISO Latin 1, Windows ANSI including the euro symbol, % MacRoman, and some extra characters from Lucida. % % Character code assignments were made as follows: % % (1) the Windows ANSI characters are almost all in their Windows ANSI % positions, because some Windows users cannot easily reencode the % fonts, and it makes no difference on other systems. The only Windows % ANSI characters not available are those that make no sense for % typesetting -- rubout (127 decimal), nobreakspace (160), softhyphen % (173). quotesingle and grave are moved just because it's such an % irritation not having them in TeX positions. % % (2) Remaining characters are assigned arbitrarily to the lower part % of the range, avoiding 0, 10 and 13 in case we meet dumb software. % % (3) Y&Y Lucida Bright includes some extra text characters; in the % hopes that other PostScript fonts, perhaps created for public % consumption, will include them, they are included starting at 0x12. % These are /dotlessj /ff /ffi /ffl. % % (4) hyphen appears twice for compatibility with both ASCII and Windows. % % (5) /Euro was assigned to 128, as in Windows ANSI % % (6) Missing characters from MacRoman encoding incorporated as follows: % % PostScript MacRoman TeXBase1 % -------------- -------------- -------------- % /notequal 173 0x16 % /infinity 176 0x17 % /lessequal 178 0x18 % /greaterequal 179 0x19 % /partialdiff 182 0x1A % /summation 183 0x1B % /product 184 0x1C % /pi 185 0x1D % /integral 186 0x81 % /Omega 189 0x8D % /radical 195 0x8E % /approxequal 197 0x8F % /Delta 198 0x9D % /lozenge 215 0x9E % /TeXBase1Encoding [ % 0x00 /.notdef /dotaccent /fi /fl /fraction /hungarumlaut /Lslash /lslash /ogonek /ring /.notdef /breve /minus /.notdef /Zcaron /zcaron % 0x10 /caron /dotlessi /dotlessj /ff /ffi /ffl /notequal /infinity /lessequal /greaterequal /partialdiff /summation /product /pi /grave /quotesingle % 0x20 /space /exclam /quotedbl /numbersign /dollar /percent /ampersand /quoteright /parenleft /parenright /asterisk /plus /comma /hyphen /period /slash % 0x30 /zero /one /two /three /four /five /six /seven /eight /nine /colon /semicolon /less /equal /greater /question % 0x40 /at /A /B /C /D /E /F /G /H /I /J /K /L /M /N /O % 0x50 /P /Q /R /S /T /U /V /W /X /Y /Z /bracketleft /backslash /bracketright /asciicircum /underscore % 0x60 /quoteleft /a /b /c /d /e /f /g /h /i /j /k /l /m /n /o % 0x70 /p /q /r /s /t /u /v /w /x /y /z /braceleft /bar /braceright /asciitilde /.notdef % 0x80 /Euro /integral /quotesinglbase /florin /quotedblbase /ellipsis /dagger /daggerdbl /circumflex /perthousand /Scaron /guilsinglleft /OE /Omega /radical /approxequal % 0x90 /.notdef /.notdef /.notdef /quotedblleft /quotedblright /bullet /endash /emdash /tilde /trademark /scaron /guilsinglright /oe /Delta /lozenge /Ydieresis % 0xA0 /.notdef /exclamdown /cent /sterling /currency /yen /brokenbar /section /dieresis /copyright /ordfeminine /guillemotleft /logicalnot /hyphen /registered /macron % 0xB0 /degree /plusminus /twosuperior /threesuperior /acute /mu /paragraph /periodcentered /cedilla /onesuperior /ordmasculine /guillemotright /onequarter /onehalf /threequarters /questiondown % 0xC0 /Agrave /Aacute /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla /Egrave /Eacute /Ecircumflex /Edieresis /Igrave /Iacute /Icircumflex /Idieresis % 0xD0 /Eth /Ntilde /Ograve /Oacute /Ocircumflex /Otilde /Odieresis /multiply /Oslash /Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn /germandbls % 0xE0 /agrave /aacute /acircumflex /atilde /adieresis /aring /ae /ccedilla /egrave /eacute /ecircumflex /edieresis /igrave /iacute /icircumflex /idieresis % 0xF0 /eth /ntilde /ograve /oacute /ocircumflex /otilde /odieresis /divide /oslash /ugrave /uacute /ucircumflex /udieresis /yacute /thorn /ydieresis ] def %%EndProcSet %%BeginProcSet: texps.pro 0 0 %! TeXDict begin/rf{findfont dup length 1 add dict begin{1 index/FID ne 2 index/UniqueID ne and{def}{pop pop}ifelse}forall[1 index 0 6 -1 roll exec 0 exch 5 -1 roll VResolution Resolution div mul neg 0 0]FontType 0 ne{/Metrics exch def dict begin Encoding{exch dup type/integertype ne{ pop pop 1 sub dup 0 le{pop}{[}ifelse}{FontMatrix 0 get div Metrics 0 get div def}ifelse}forall Metrics/Metrics currentdict end def}{{1 index type /nametype eq{exit}if exch pop}loop}ifelse[2 index currentdict end definefont 3 -1 roll makefont/setfont cvx]cvx def}def/ObliqueSlant{dup sin S cos div neg}B/SlantFont{4 index mul add}def/ExtendFont{3 -1 roll mul exch}def/ReEncodeFont{CharStrings rcheck{/Encoding false def dup[ exch{dup CharStrings exch known not{pop/.notdef/Encoding true def}if} forall Encoding{]exch pop}{cleartomark}ifelse}if/Encoding exch def}def end %%EndProcSet TeXDict begin 40258431 52099146 1000 600 600 (paper-letter/tut.dvi) @start /Fa 139[40 10[40 4[40 40 3[40 95[{TeXBase1Encoding ReEncodeFont} 5 66.4176 /Courier rf /Fb 143[60 3[60 60 1[60 3[60 1[60 99[{TeXBase1Encoding ReEncodeFont}6 99.6264 /Courier rf /Fc 162[40 1[40 91[{TeXBase1Encoding ReEncodeFont}2 119.552 /Times-Roman rf /Fd 68[50 66[50 1[50 50 50 50 50 1[50 50 50 50 3[50 3[50 50 50 1[50 34[50 15[50 46[{ TeXBase1Encoding ReEncodeFont}18 83.022 /Courier-Bold rf /Fe 134[29 4[18 1[26 2[33 33 1[18 1[18 3[18 29 1[29 33 33 97[{TeXBase1Encoding ReEncodeFont}12 66.4176 /Times-Italic rf /Ff 138[72 72 72 72 1[72 72 72 1[72 72 1[72 1[72 72 72 72 72 72 72 55[72 72 40[{TeXBase1Encoding ReEncodeFont}19 119.552 /Courier rf /Fg 214[25 25 40[{TeXBase1Encoding ReEncodeFont}2 74.7198 /Times-Roman rf /Fh 206[25 49[{TeXBase1Encoding ReEncodeFont}1 49.8132 /Times-Roman rf /Fi 206[29 49[{TeXBase1Encoding ReEncodeFont}1 58.1154 /Times-Roman rf /Fj 107[33 33 25[50 50 72 50 55 28 50 33 1[55 55 55 83 22 50 22 22 55 55 28 55 55 50 55 55 8[66 1[66 72 61 66 72 78 66 78 72 83 55 66 1[28 72 1[61 66 72 72 1[66 7[55 55 55 55 55 55 55 55 55 55 1[28 33 2[39 39[50 2[{TeXBase1Encoding ReEncodeFont}60 99.6264 /Helvetica rf /Fk 3[45 5[45 17[45 101[45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 33[{TeXBase1Encoding ReEncodeFont}97 74.7198 /Courier rf /Fl 134[60 60 86 60 66 33 60 40 66 66 66 66 100 27 60 1[27 66 66 33 66 66 60 66 66 6[73 2[113 80 86 73 80 86 93 80 93 86 100 66 80 1[33 86 93 73 80 86 86 80 80 7[66 66 66 66 66 66 66 66 66 66 1[33 40 33 41[60 2[{ TeXBase1Encoding ReEncodeFont}61 119.552 /Helvetica rf /Fm 134[33 33 48 33 33 18 26 22 1[33 33 33 52 18 33 18 18 33 33 22 29 33 29 33 29 22 6[48 48 2[48 41 37 44 1[37 48 48 59 3[22 48 48 1[41 1[44 1[48 5[18 12[17 22 17 2[22 22 22 36[37 2[{TeXBase1Encoding ReEncodeFont}49 66.4176 /Times-Roman rf /Fn 167[80 113 80 86 73 80 86 2[93 86 1[73 2[33 86 93 73 80 86 86 86 86 65[{TeXBase1Encoding ReEncodeFont}19 119.552 /Helvetica-Bold rf /Fo 105[46 28[42 42 60 42 46 23 42 28 46 46 46 46 69 18 42 1[18 46 46 23 46 46 42 46 46 7[55 55 78 1[60 51 55 60 65 55 65 60 69 46 2[23 60 65 51 55 60 60 55 55 6[23 46 46 46 46 46 46 46 46 46 46 1[23 28 23 41[42 2[{TeXBase1Encoding ReEncodeFont}62 83.022 /Helvetica rf /Fp 68[50 39[50 21[50 1[50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 1[50 50 50 50 50 50 50 50 50 1[50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 1[50 50 50 50 50 50 1[50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 1[50 50 50 50 50 33[{ TeXBase1Encoding ReEncodeFont}89 83.022 /Courier rf /Fq 133[32 37 37 55 37 42 23 32 32 42 42 42 42 60 23 37 23 23 42 42 23 37 42 37 42 42 1[42 5[46 3[60 46 42 51 1[51 60 55 69 46 1[37 28 1[60 51 51 1[55 51 51 6[28 1[42 1[42 4[42 1[23 21 28 21 40[42 42 2[{TeXBase1Encoding ReEncodeFont}55 83.022 /Times-Italic rf /Fr 105[42 28[42 42 60 42 46 28 32 37 46 46 42 46 69 23 46 1[23 46 42 28 37 46 37 46 42 1[42 4[55 60 1[83 60 60 55 46 60 1[51 65 60 78 55 2[32 65 65 51 55 60 60 55 60 1[42 4[28 42 42 42 42 42 42 42 42 42 42 1[21 28 45[{TeXBase1Encoding ReEncodeFont}62 83.022 /Times-Bold rf /Fs 68[42 17[63 17[83 1[29 37 37 24[37 42 42 60 42 42 23 32 28 42 42 42 42 65 23 42 23 23 42 42 28 37 42 37 42 37 28 42 4[51 60 60 78 60 60 51 46 55 60 46 60 60 74 51 60 32 28 60 60 46 51 60 55 55 60 1[37 47 2[23 23 42 42 42 42 42 42 42 42 42 42 23 21 28 21 47 42 28 28 28 65 69 2[34 28 29[46 46 2[{ TeXBase1Encoding ReEncodeFont}89 83.022 /Times-Roman rf /Ft 103[25 29[37 37 37 54 37 42 21 37 25 42 42 42 42 62 17 37 17 17 42 42 21 42 42 37 42 42 1[42 2[21 2[50 50 3[46 50 2[50 58 54 62 2[37 1[54 58 1[50 1[54 1[50 76 5[21 42 42 42 1[42 1[42 42 42 42 21 21 25 42[37 2[{ TeXBase1Encoding ReEncodeFont}57 74.7198 /Helvetica rf /Fu 134[50 1[72 1[55 33 1[44 2[50 55 4[28 55 1[33 44 55 2[50 13[55 2[61 9[61 70[{TeXBase1Encoding ReEncodeFont}16 99.6264 /Times-Bold rf /Fv 139[28 1[33 4[78 28 2[28 50 2[44 1[44 50 44 27[61 72 9[28 50 50 5[50 50 50 3[25 44[{ TeXBase1Encoding ReEncodeFont}19 99.6264 /Times-Roman rf /Fw 137[72 80 40 72 48 2[80 80 120 1[72 1[32 3[80 80 2[80 14[104 5[80 1[72 2[112 88 1[104 21[40 1[40 44[{ TeXBase1Encoding ReEncodeFont}21 143.462 /Helvetica rf /Fx 140[72 6[32 6[80 3[80 14[104 28[80 1[80 80 3[40 46[{ TeXBase1Encoding ReEncodeFont}9 143.462 /Helvetica-Oblique rf /Fy 105[115 28[103 103 149 103 115 57 103 69 1[115 115 115 172 46 2[46 115 115 57 115 115 103 115 115 7[138 138 195 1[149 126 138 2[138 161 149 172 115 2[57 149 161 126 138 149 149 138 138 1[115 4[57 58[{ TeXBase1Encoding ReEncodeFont}45 206.559 /Helvetica rf end %%EndProlog %%BeginSetup %%Feature: *Resolution 600dpi TeXDict begin %%BeginPaperSize: Letter /setpagedevice where { pop << /PageSize [612 792] >> setpagedevice } { /letter where { pop letter } if } ifelse %%EndPaperSize end %%EndSetup %%Page: 1 1 TeXDict begin 1 0 bop 0 83 3901 9 v 2533 446 a Fy(Python)57 b(T)-25 b(utor)s(ial)3015 676 y Fx(Release)38 b(2.5.3)2684 1852 y Fw(Guido)g(v)l(an)i(Rossum)2365 2035 y(F)-6 b(red)39 b(L.)g(Dr)o(ak)m(e)n(,)f(Jr)-7 b(.,)39 b(editor)3044 4091 y Fv(19th)24 b(December)l(,)h(2008)2676 5201 y Fu(Python)g (Softwar)n(e)h(F)n(oundation)3019 5317 y Fv(Email:)k Ft(docs@p)n(ython.org)p eop end %%Page: 2 2 TeXDict begin 2 1 bop 0 83 a Fs(Cop)o(yright)19 b(\251)i(2001-2008)16 b(Python)j(Softw)o(are)h(F)o(oundation.)i(All)f(rights)f(reserv)o(ed.)0 230 y(Cop)o(yright)f(\251)i(2000)d(BeOpen.com.)24 b(All)c(rights)g (reserv)o(ed.)0 377 y(Cop)o(yright)f(\251)i(1995-2000)16 b(Corporation)i(for)i(National)f(Research)h(Initiati)n(v)o(es.)25 b(All)20 b(rights)g(reserv)o(ed.)0 524 y(Cop)o(yright)f(\251)i (1991-1995)16 b(Stichting)k(Mathematisch)f(Centrum.)24 b(All)d(rights)f(reserv)o(ed.)0 671 y(See)h(the)f(end)f(of)h(this)h (document)d(for)i(complete)f(license)h(and)g(permissions)f (information.)p eop end %%Page: 1 3 TeXDict begin 1 2 bop 1796 1618 a Fr(Abstract)0 1848 y Fs(Python)24 b(is)j(an)e(easy)g(to)h(learn,)g(po)n(werful)e (programming)d(language.)39 b(It)26 b(has)g(ef)n(\002cient)f(high-le)n (v)o(el)e(data)i(structures)g(and)g(a)h(simple)0 1948 y(b)n(ut)19 b(ef)n(fecti)n(v)o(e)f(approach)f(to)j(object-oriented)c (programming.)21 b(Python')-5 b(s)19 b(ele)o(gant)f(syntax)h(and)f (dynamic)g(typing,)g(together)g(with)i(its)0 2047 y(interpreted)i (nature,)h(mak)o(e)g(it)i(an)e(ideal)h(language)e(for)h(scripting)g (and)g(rapid)f(application)h(de)n(v)o(elopment)d(in)k(man)o(y)f(areas)g (on)h(most)0 2147 y(platforms.)0 2294 y(The)29 b(Python)f(interpreter)f (and)i(the)g(e)o(xtensi)n(v)o(e)e(standard)h(library)g(are)h(freely)f (a)n(v)n(ailable)h(in)g(source)f(or)h(binary)f(form)g(for)g(all)i(ma-)0 2393 y(jor)e(platforms)f(from)h(the)g(Python)g(W)-7 b(eb)29 b(site,)i Ft(http://www)l(.p)n(ython.org/)-6 b Fs(,)30 b(and)e(may)g(be)h(freely)e(distrib)n(uted.)49 b(The)28 b(same)h(site)g(also)0 2493 y(contains)d(distrib)n(utions)g(of)h(and)f (pointers)g(to)i(man)o(y)d(free)i(third)f(party)g(Python)g(modules,)i (programs)d(and)h(tools,)j(and)d(additional)0 2593 y(documentation.)0 2739 y(The)31 b(Python)f(interpreter)g(is)j(easily)e(e)o(xtended)f (with)i(ne)n(w)f(functions)f(and)h(data)g(types)h(implemented)d(in)j(C) g(or)f(C)3474 2732 y(++)3601 2739 y(\(or)f(other)0 2839 y(languages)19 b(callable)h(from)f(C\).)h(Python)f(is)j(also)e (suitable)g(as)h(an)f(e)o(xtension)f(language)f(for)i(customizable)f (applications.)0 2986 y(This)h(tutorial)f(introduces)f(the)i(reader)e (informally)g(to)i(the)g(basic)g(concepts)e(and)i(features)f(of)g(the)h (Python)e(language)g(and)h(system.)25 b(It)0 3086 y(helps)e(to)g(ha)n (v)o(e)f(a)h(Python)e(interpreter)h(handy)f(for)h(hands-on)f(e)o (xperience,)g(b)n(ut)h(all)i(e)o(xamples)d(are)i(self-contained,)e(so)i (the)g(tutorial)0 3185 y(can)d(be)g(read)g(of)n(f-line)e(as)j(well.)0 3332 y(F)o(or)k(a)h(description)d(of)i(standard)g(objects)g(and)f (modules,)i(see)g(the)f Fq(Python)f(Libr)o(ary)h(Refer)m(ence)g Fs(document.)38 b(The)25 b Fq(Python)f(Refer)n(-)0 3432 y(ence)h(Manual)f Fs(gi)n(v)o(es)h(a)h(more)f(formal)f(de\002nition)h (of)g(the)g(language.)39 b(T)-7 b(o)26 b(write)f(e)o(xtensions)g(in)g (C)i(or)e(C)3102 3425 y(++)3196 3432 y(,)i(read)e Fq(Extending)e(and)0 3531 y(Embedding)k(the)i(Python)e(Interpr)m(eter)i Fs(and)f Fq(Python/C)h(API)f(Refer)m(ence)p Fs(.)51 b(There)28 b(are)h(also)g(se)n(v)o(eral)f(books)g(co)o(v)o(ering)f(Python)g(in)0 3631 y(depth.)0 3778 y(This)j(tutorial)f(does)h(not)f(attempt)h(to)g (be)g(comprehensi)n(v)o(e)c(and)k(co)o(v)o(er)e(e)n(v)o(ery)h(single)g (feature,)i(or)f(e)n(v)o(en)f(e)n(v)o(ery)f(commonly)g(used)0 3877 y(feature.)58 b(Instead,)33 b(it)f(introduces)e(man)o(y)g(of)h (Python')-5 b(s)31 b(most)g(note)n(w)o(orthy)e(features,)k(and)e(will)h (gi)n(v)o(e)f(you)f(a)i(good)e(idea)h(of)h(the)0 3977 y(language')-5 b(s)17 b(\003a)n(v)n(or)h(and)g(style.)24 b(After)18 b(reading)f(it,)i(you)e(will)i(be)f(able)g(to)h(read)e(and)h (write)h(Python)e(modules)g(and)g(programs,)g(and)g(you)0 4077 y(will)k(be)f(ready)f(to)i(learn)e(more)h(about)f(the)h(v)n (arious)f(Python)g(library)g(modules)g(described)g(in)i(the)f Fq(Python)f(Libr)o(ary)h(Refer)m(ence)p Fs(.)p eop end %%Page: 2 4 TeXDict begin 2 3 bop eop end %%Page: 1 5 TeXDict begin 1 4 bop 2764 747 a Fy(CONTENTS)0 1594 y Fr(1)83 b(Whetting)19 b(Y)-9 b(our)20 b(A)n(ppetite)2882 b(1)0 1777 y(2)83 b(Using)20 b(the)h(Python)e(Inter)o(pr)o(eter)2704 b(3)125 1876 y Fs(2.1)110 b(In)m(v)n(oking)18 b(the)i(Interpreter)73 b(.)41 b(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.) g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f (.)g(.)g(.)h(.)f(.)g(.)g(.)143 b(3)125 1976 y(2.2)110 b(The)20 b(Interpreter)e(and)i(Its)h(En)m(vironment)100 b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.) g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)143 b(4)0 2159 y Fr(3)83 b(An)20 b(Inf)n(ormal)g(Intr)o(oduction)g(to)g (Python)2453 b(7)125 2258 y Fs(3.1)110 b(Using)20 b(Python)g(as)g(a)h (Calculator)81 b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g (.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)g(.)143 b(7)125 2358 y(3.2)110 b(First)21 b(Steps)g(T)-7 b(o)n(w)o(ards)20 b(Programming)39 b(.)i(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h (.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) g(.)102 b(17)0 2540 y Fr(4)83 b(Mor)o(e)19 b(Contr)o(ol)g(Flo)o(w)i(T) -8 b(ools)2802 b(19)125 2640 y Fs(4.1)110 b Fp(if)21 b Fs(Statements)66 b(.)42 b(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h (.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(19)125 2740 y(4.2)110 b Fp(for)21 b Fs(Statements)79 b(.)41 b(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.) g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h (.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(19)125 2839 y(4.3)110 b(The)20 b Fp(range\(\))g Fs(Function)47 b(.)41 b(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.) g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f (.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(20)125 2939 y(4.4)110 b Fp(break)20 b Fs(and)g Fp(continue)f Fs(Statements,)h(and)g Fp(else)g Fs(Clauses)h(on)e(Loops)46 b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)g (.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(21)125 3039 y(4.5)110 b Fp(pass)20 b Fs(Statements)92 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f (.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(21)125 3138 y(4.6)110 b(De\002ning)20 b(Functions)98 b(.)42 b(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h (.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(22)125 3238 y(4.7)110 b(More)20 b(on)g(De\002ning)f(Functions)43 b(.)f(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f (.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.) h(.)f(.)g(.)g(.)102 b(23)0 3421 y Fr(5)83 b(Data)19 b(Structur)o(es) 3128 b(29)125 3520 y Fs(5.1)110 b(More)20 b(on)g(Lists)86 b(.)42 b(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g (.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(29)125 3620 y(5.2)110 b(The)20 b Fp(del)g Fs(statement)100 b(.)42 b(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g (.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(33)125 3719 y(5.3)110 b(T)l(uples)20 b(and)g(Sequences)66 b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(34)125 3819 y(5.4)110 b(Sets)103 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.) g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h (.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(35)125 3919 y(5.5)110 b(Dictionaries)83 b(.)41 b(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g (.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.) g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(35)125 4018 y(5.6)110 b(Looping)19 b(T)-6 b(echniques)57 b(.)42 b(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(36)125 4118 y(5.7)110 b(More)20 b(on)g(Conditions)68 b(.)42 b(.)f(.)g(.)g(.)h(.)f (.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)g(.)102 b(38)125 4218 y(5.8)110 b(Comparing)19 b(Sequences)g(and)h (Other)f(T)-7 b(ypes)49 b(.)41 b(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g (.)h(.)f(.)g(.)g(.)102 b(38)0 4400 y Fr(6)83 b(Modules)3388 b(41)125 4500 y Fs(6.1)110 b(More)20 b(on)g(Modules)80 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f (.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(42)125 4599 y(6.2)110 b(Standard)19 b(Modules)69 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f (.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)g(.)102 b(44)125 4699 y(6.3)110 b(The)20 b Fp(dir\(\))g Fs(Function)85 b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f (.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.) h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(45)125 4799 y(6.4)110 b(P)o(ackages)57 b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.) g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h (.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.) g(.)h(.)f(.)g(.)g(.)102 b(46)0 4981 y Fr(7)83 b(Input)21 b(and)f(Output)3060 b(51)125 5081 y Fs(7.1)110 b(F)o(ancier)20 b(Output)f(F)o(ormatting)98 b(.)42 b(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g (.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(51)125 5181 y(7.2)110 b(Reading)20 b(and)f(Writing)i(Files)47 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g (.)g(.)h(.)f(.)g(.)g(.)102 b(54)0 5363 y Fr(8)83 b(Err)o(ors)20 b(and)g(Exceptions)2891 b(57)p 0 5549 3901 4 v 3882 5649 a Fo(i)p eop end %%Page: 2 6 TeXDict begin 2 5 bop 125 83 a Fs(8.1)110 b(Syntax)20 b(Errors)91 b(.)42 b(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.) g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h (.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(57)125 183 y(8.2)110 b(Exceptions)57 b(.)41 b(.)g(.)h(.)f(.)g(.)h(.)f (.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g (.)g(.)h(.)f(.)g(.)g(.)102 b(57)125 282 y(8.3)110 b(Handling)19 b(Exceptions)101 b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h (.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(58)125 382 y(8.4)110 b(Raising)21 b(Exceptions)93 b(.)42 b(.)f(.)g(.)g(.)h(.)f (.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)g(.)102 b(60)125 482 y(8.5)110 b(User)n(-de\002ned)19 b(Exceptions)45 b(.)c(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(61)125 581 y(8.6)110 b(De\002ning)20 b(Clean-up)f(Actions)92 b(.)42 b(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g (.)h(.)f(.)g(.)g(.)102 b(62)125 681 y(8.7)110 b(Prede\002ned)19 b(Clean-up)g(Actions)91 b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f (.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(63)0 863 y Fr(9)83 b(Classes)3434 b(65)125 963 y Fs(9.1)110 b(A)21 b(W)-7 b(ord)20 b(About)g(T)-6 b(erminology)44 b(.)e(.)f(.)g(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g (.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(65)125 1063 y(9.2)110 b(Python)19 b(Scopes)h(and)g(Name)g(Spaces)72 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)g(.)102 b(65)125 1162 y(9.3)110 b(A)21 b(First)g(Look)e(at)i (Classes)85 b(.)41 b(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f (.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(67)125 1262 y(9.4)110 b(Random)20 b(Remarks)78 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g (.)102 b(69)125 1362 y(9.5)110 b(Inheritance)52 b(.)41 b(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h (.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(70)125 1461 y(9.6)110 b(Pri)n(v)n(ate)20 b(V)-9 b(ariables)53 b(.)41 b(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.) g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h (.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(72)125 1561 y(9.7)110 b(Odds)20 b(and)g(Ends)48 b(.)42 b(.)f(.)g(.)h(.)f(.)g (.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g (.)h(.)f(.)g(.)g(.)102 b(72)125 1660 y(9.8)110 b(Exceptions)19 b(Are)h(Classes)i(T)-7 b(oo)55 b(.)42 b(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h (.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(73)125 1760 y(9.9)110 b(Iterators)25 b(.)42 b(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f (.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g (.)g(.)h(.)f(.)g(.)g(.)102 b(74)125 1860 y(9.10)68 b(Generators)62 b(.)41 b(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f (.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(75)125 1959 y(9.11)68 b(Generator)19 b(Expressions)46 b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.) g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h (.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(76)0 2142 y Fr(10)41 b(Brief)20 b(T)-8 b(our)21 b(of)f(the)g(Standard)g(Library)2443 b(77)125 2242 y Fs(10.1)68 b(Operating)19 b(System)h(Interf)o(ace)74 b(.)42 b(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g (.)h(.)f(.)g(.)g(.)102 b(77)125 2341 y(10.2)68 b(File)21 b(W)m(ildcards)63 b(.)42 b(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h (.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(77)125 2441 y(10.3)68 b(Command)19 b(Line)h(Ar)o(guments)78 b(.)42 b(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g (.)h(.)f(.)g(.)g(.)102 b(78)125 2540 y(10.4)68 b(Error)19 b(Output)h(Redirection)f(and)g(Program)g(T)-6 b(ermination)26 b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(78)125 2640 y(10.5)68 b(String)20 b(P)o(attern)g(Matching)65 b(.)41 b(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.) g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f (.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(78)125 2740 y(10.6)68 b(Mathematics)60 b(.)41 b(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g (.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.) g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(79)125 2839 y(10.7)68 b(Internet)19 b(Access)38 b(.)k(.)f(.)g(.)h(.)f (.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g (.)g(.)h(.)f(.)g(.)g(.)102 b(79)125 2939 y(10.8)68 b(Dates)21 b(and)f(T)m(imes)59 b(.)41 b(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g (.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(79)125 3039 y(10.9)68 b(Data)21 b(Compression)59 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f (.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(80)125 3138 y(10.10)26 b(Performance)18 b(Measurement)27 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(80)125 3238 y(10.11)26 b(Quality)20 b(Control)27 b(.)42 b(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(81)125 3337 y(10.12)26 b(Batteries)21 b(Included)68 b(.)41 b(.)h(.)f(.)g(.)g (.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.) g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h (.)f(.)g(.)g(.)102 b(81)0 3520 y Fr(11)41 b(Brief)20 b(T)-8 b(our)21 b(of)f(the)g(Standard)g(Library)h(\226)f(P)o(art)f(II) 2120 b(83)125 3620 y Fs(11.1)68 b(Output)20 b(F)o(ormatting)55 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f (.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(83)125 3719 y(11.2)68 b(T)-6 b(emplating)49 b(.)41 b(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.) g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f (.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.) h(.)f(.)g(.)g(.)102 b(84)125 3819 y(11.3)68 b(W)-7 b(orking)20 b(with)g(Binary)g(Data)g(Record)g(Layouts)54 b(.)42 b(.)f(.)g(.)h(.)f (.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(85)125 3919 y(11.4)68 b(Multi-threading)23 b(.)42 b(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g (.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.) g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(86)125 4018 y(11.5)68 b(Logging)19 b(.)42 b(.)f(.)g(.)g(.)h(.)f(.)g (.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.) g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h (.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(87)125 4118 y(11.6)68 b(W)-7 b(eak)21 b(References)37 b(.)k(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g (.)102 b(87)125 4218 y(11.7)68 b(T)-7 b(ools)21 b(for)e(W)-7 b(orking)19 b(with)i(Lists)28 b(.)42 b(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h (.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.) g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(88)125 4317 y(11.8)68 b(Decimal)20 b(Floating)g(Point)g(Arithmetic)80 b(.)42 b(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g (.)102 b(89)0 4500 y Fr(12)41 b(What)20 b(No)o(w?)3270 b(91)0 4682 y(A)65 b(Interacti)o(v)o(e)19 b(Input)i(Editing)g(and)f (History)g(Substitution)1906 b(93)125 4782 y Fs(A.1)92 b(Line)20 b(Editing)71 b(.)41 b(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g (.)102 b(93)125 4882 y(A.2)92 b(History)20 b(Substitution)66 b(.)42 b(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.) f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(93)125 4981 y(A.3)92 b(K)n(e)o(y)20 b(Bindings)95 b(.)42 b(.)f(.)g(.)h(.)f(.)g(.)g (.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.) g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h (.)f(.)g(.)g(.)102 b(93)125 5081 y(A.4)92 b(Commentary)54 b(.)41 b(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.) h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(95)0 5264 y Fr(B)70 b(Floating)19 b(P)n(oint)h(Arithmetic:)k(Issues)e (and)f(Limitations)1929 b(97)125 5363 y Fs(B.1)97 b(Representation)19 b(Error)43 b(.)f(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.) g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h (.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(99)p 0 5549 3901 4 v 0 5649 a Fo(ii)p eop end %%Page: 3 7 TeXDict begin 3 6 bop 0 83 a Fr(C)65 b(History)19 b(and)h(License)2941 b(101)125 183 y Fs(C.1)97 b(History)20 b(of)g(the)g(softw)o(are)34 b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.) g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h (.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)60 b(101)125 282 y(C.2)97 b(T)-6 b(erms)20 b(and)g(conditions)f(for)g(accessing)h(or)g(otherwise) g(using)f(Python)47 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.) g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)60 b(102)125 382 y(C.3)97 b(Licenses)21 b(and)e(Ackno)n(wledgements)e(for)j (Incorporated)d(Softw)o(are)54 b(.)41 b(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g (.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)60 b(105)0 565 y Fr(D)65 b(Glossary)3335 b(115)0 747 y(Index)3572 b(119)p 0 5549 3901 4 v 3845 5649 a Fo(iii)p eop end %%Page: 4 8 TeXDict begin 4 7 bop 0 5549 3901 4 v 0 5649 a Fo(iv)p eop end %%Page: 1 9 TeXDict begin 1 8 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3641 427 y Fn(ONE)p 0 515 V 1747 978 a Fy(Whetting)58 b(Y)-29 b(our)57 b(Appetite)0 1468 y Fs(If)23 b(you)f(do)h(much)g(w)o(ork)f(on) h(computers,)f(e)n(v)o(entually)g(you)g(\002nd)h(that)g(there')-5 b(s)23 b(some)g(task)h(you')l(d)d(lik)o(e)j(to)f(automate.)33 b(F)o(or)23 b(e)o(xample,)0 1567 y(you)f(may)h(wish)g(to)h(perform)d(a) i(search-and-replace)d(o)o(v)o(er)i(a)h(lar)o(ge)f(number)g(of)g(te)o (xt)h(\002les,)i(or)e(rename)f(and)g(rearrange)f(a)j(b)n(unch)e(of)0 1667 y(photo)15 b(\002les)i(in)f(a)h(complicated)d(w)o(ay)-5 b(.)24 b(Perhaps)15 b(you')l(d)f(lik)o(e)j(to)f(write)g(a)h(small)f (custom)g(database,)g(or)g(a)h(specialized)e(GUI)h(application,)0 1766 y(or)k(a)h(simple)f(game.)0 1913 y(If)f(you')l(re)e(a)i (professional)e(softw)o(are)i(de)n(v)o(eloper)m(,)d(you)i(may)h(ha)n(v) o(e)f(to)h(w)o(ork)f(with)h(se)n(v)o(eral)g(C/C)2759 1906 y(++)2853 1913 y(/Ja)n(v)n(a)h(libraries)e(b)n(ut)h(\002nd)g(the)g (usual)0 2013 y(write/compile/test/re-compile)g(c)o(ycle)i(is)i(too)f (slo)n(w)-5 b(.)30 b(Perhaps)22 b(you')l(re)e(writing)h(a)h(test)h (suite)g(for)e(such)h(a)g(library)f(and)g(\002nd)h(writing)0 2113 y(the)k(testing)g(code)f(a)i(tedious)e(task.)42 b(Or)27 b(maybe)d(you')l(v)o(e)g(written)i(a)g(program)e(that)i(could)f (use)h(an)g(e)o(xtension)f(language,)g(and)g(you)0 2212 y(don')o(t)19 b(w)o(ant)h(to)g(design)g(and)f(implement)g(a)i(whole)f (ne)n(w)g(language)e(for)i(your)e(application.)0 2359 y(Python)h(is)i(just)g(the)f(language)f(for)g(you.)0 2506 y(Y)-9 b(ou)18 b(could)f(write)h(a)j(U)t Fm(N)t(I)t(X)g Fs(shell)e(script)f(or)g(W)m(indo)n(ws)g(batch)f(\002les)j(for)d(some)h (of)g(these)h(tasks,)g(b)n(ut)f(shell)g(scripts)h(are)f(best)h(at)g(mo) o(ving)0 2606 y(around)25 b(\002les)i(and)f(changing)f(te)o(xt)i(data,) g(not)g(well-suited)f(for)g(GUI)h(applications)e(or)h(games.)44 b(Y)-9 b(ou)26 b(could)g(write)h(a)g(C/C)3639 2599 y(++)3733 2606 y(/Ja)n(v)n(a)0 2705 y(program,)17 b(b)n(ut)i(it)i(can)e(tak)o(e)g (a)h(lot)f(of)h(de)n(v)o(elopment)c(time)k(to)f(get)g(e)n(v)o(en)g(a)h (\002rst-draft)e(program.)23 b(Python)18 b(is)i(simpler)f(to)h(use,)f (a)n(v)n(ailable)0 2805 y(on)h(W)m(indo)n(ws,)f(MacOS)h(X,)h(and)h(U)t Fm(N)t(I)t(X)h Fs(operating)18 b(systems,)j(and)e(will)i(help)f(you)f (get)h(the)h(job)e(done)h(more)f(quickly)-5 b(.)0 2952 y(Python)17 b(is)i(simple)f(to)g(use,)g(b)n(ut)h(it)f(is)h(a)g(real)f (programming)c(language,)j(of)n(fering)f(much)h(more)g(structure)g(and) h(support)e(for)i(lar)o(ge)f(pro-)0 3051 y(grams)j(than)g(shell)h (scripts)g(or)f(batch)g(\002les)h(can)g(of)n(fer)-5 b(.)25 b(On)20 b(the)h(other)f(hand,)f(Python)g(also)i(of)n(fers)f(much)f (more)h(error)f(checking)g(than)0 3151 y(C,)j(and,)f(being)f(a)h Fq(very-high-le)o(vel)f(langua)o(g)o(e)p Fs(,)f(it)j(has)f(high-le)n(v) o(el)e(data)i(types)g(b)n(uilt)h(in,)f(such)g(as)h(\003e)o(xible)f (arrays)f(and)h(dictionaries.)0 3251 y(Because)h(of)f(its)h(more)e (general)h(data)g(types)g(Python)f(is)i(applicable)f(to)g(a)h(much)e (lar)o(ger)g(problem)g(domain)g(than)g(A)-8 b(wk)22 b(or)f(e)n(v)o(en)f (Perl,)0 3350 y(yet)g(man)o(y)f(things)h(are)g(at)h(least)g(as)g(easy)f (in)g(Python)f(as)i(in)g(those)f(languages.)0 3497 y(Python)i(allo)n (ws)i(you)e(to)h(split)h(your)e(program)f(into)i(modules)f(that)i(can)f (be)g(reused)f(in)i(other)e(Python)g(programs.)32 b(It)24 b(comes)f(with)g(a)0 3597 y(lar)o(ge)18 b(collection)h(of)g(standard)f (modules)g(that)h(you)g(can)g(use)g(as)h(the)f(basis)h(of)f(your)f (programs)g(\227)i(or)f(as)h(e)o(xamples)e(to)h(start)h(learning)0 3696 y(to)f(program)e(in)i(Python.)24 b(Some)18 b(of)h(these)g(modules) f(pro)o(vide)f(things)i(lik)o(e)g(\002le)h(I/O,)f(system)g(calls,)h (sock)o(ets,)f(and)g(e)n(v)o(en)f(interf)o(aces)g(to)0 3796 y(graphical)h(user)h(interf)o(ace)f(toolkits)h(lik)o(e)g(Tk.)0 3943 y(Python)27 b(is)h(an)g(interpreted)e(language,)i(which)f(can)h (sa)n(v)o(e)g(you)e(considerable)g(time)i(during)e(program)g(de)n(v)o (elopment)f(because)i(no)0 4042 y(compilation)16 b(and)h(linking)f(is)j (necessary)-5 b(.)22 b(The)c(interpreter)d(can)j(be)f(used)h(interacti) n(v)o(ely)-5 b(,)15 b(which)i(mak)o(es)h(it)g(easy)f(to)h(e)o (xperiment)d(with)0 4142 y(features)21 b(of)h(the)f(language,)g(to)h (write)f(thro)n(w-a)o(w)o(ay)f(programs,)g(or)i(to)g(test)g(functions)f (during)f(bottom-up)f(program)h(de)n(v)o(elopment.)0 4242 y(It)g(is)i(also)e(a)h(handy)d(desk)i(calculator)-5 b(.)0 4389 y(Python)16 b(enables)i(programs)d(to)j(be)f(written)h (compactly)e(and)h(readably)-5 b(.)22 b(Programs)16 b(written)i(in)f (Python)g(are)g(typically)g(much)f(shorter)0 4488 y(than)k(equi)n(v)n (alent)e(C,)j(C)678 4481 y(++)772 4488 y(,)g(or)f(Ja)n(v)n(a)g (programs,)e(for)i(se)n(v)o(eral)f(reasons:)137 4686 y(\225)42 b(the)20 b(high-le)n(v)o(el)e(data)i(types)g(allo)n(w)g(you)f (to)i(e)o(xpress)e(comple)o(x)g(operations)f(in)j(a)f(single)g (statement;)137 4844 y(\225)42 b(statement)20 b(grouping)d(is)k(done)e (by)h(indentation)e(instead)i(of)g(be)o(ginning)e(and)i(ending)e(brack) o(ets;)137 5003 y(\225)42 b(no)19 b(v)n(ariable)g(or)h(ar)o(gument)e (declarations)h(are)h(necessary)-5 b(.)0 5201 y(Python)27 b(is)j Fq(e)n(xtensible)p Fs(:)41 b(if)29 b(you)f(kno)n(w)f(ho)n(w)h (to)h(program)d(in)j(C)g(it)g(is)h(easy)e(to)h(add)f(a)h(ne)n(w)f(b)n (uilt-in)g(function)f(or)h(module)f(to)i(the)0 5300 y(interpreter)m(,) 23 b(either)g(to)h(perform)e(critical)i(operations)e(at)i(maximum)f (speed,)h(or)f(to)h(link)g(Python)f(programs)f(to)i(libraries)f(that)h (may)0 5400 y(only)h(be)h(a)n(v)n(ailable)g(in)h(binary)d(form)i (\(such)f(as)i(a)g(v)o(endor)n(-speci\002c)c(graphics)i(library\).)42 b(Once)26 b(you)f(are)h(really)g(hook)o(ed,)f(you)h(can)p 0 5549 3901 4 v 3854 5649 a Fo(1)p eop end %%Page: 2 10 TeXDict begin 2 9 bop 0 83 a Fs(link)24 b(the)g(Python)g(interpreter)e (into)i(an)h(application)d(written)j(in)f(C)h(and)f(use)h(it)g(as)g(an) f(e)o(xtension)f(or)h(command)e(language)h(for)g(that)0 183 y(application.)0 330 y(By)h(the)f(w)o(ay)-5 b(,)24 b(the)f(language)f(is)i(named)e(after)h(the)h(BBC)h(sho)n(w)e (\223Monty)f(Python')-5 b(s)23 b(Flying)f(Circus\224)i(and)f(has)g (nothing)f(to)h(do)g(with)0 429 y(nasty)d(reptiles.)25 b(Making)19 b(references)g(to)h(Monty)f(Python)g(skits)i(in)g (documentation)c(is)k(not)f(only)f(allo)n(wed,)h(it)g(is)i(encouraged!) 0 576 y(No)n(w)f(that)g(you)e(are)i(all)h(e)o(xcited)d(about)h(Python,) g(you')o(ll)g(w)o(ant)g(to)h(e)o(xamine)f(it)h(in)g(some)g(more)f (detail.)27 b(Since)20 b(the)h(best)g(w)o(ay)g(to)g(learn)0 676 y(a)g(language)d(is)j(to)g(use)f(it,)h(the)f(tutorial)f(in)m(vites) h(you)g(to)g(play)g(with)g(the)g(Python)f(interpreter)g(as)i(you)e (read.)0 823 y(In)28 b(the)g(ne)o(xt)g(chapter)m(,)h(the)f(mechanics)f (of)h(using)g(the)g(interpreter)f(are)h(e)o(xplained.)47 b(This)29 b(is)g(rather)f(mundane)e(information,)i(b)n(ut)0 922 y(essential)21 b(for)e(trying)g(out)h(the)g(e)o(xamples)f(sho)n(wn) h(later)-5 b(.)0 1069 y(The)25 b(rest)g(of)g(the)g(tutorial)g (introduces)e(v)n(arious)h(features)g(of)h(the)g(Python)f(language)g (and)g(system)h(through)e(e)o(xamples,)i(be)o(ginning)0 1169 y(with)j(simple)g(e)o(xpressions,)h(statements)f(and)f(data)h (types,)i(through)c(functions)h(and)g(modules,)i(and)e(\002nally)h (touching)e(upon)h(ad-)0 1268 y(v)n(anced)19 b(concepts)g(lik)o(e)h(e)o (xceptions)f(and)h(user)n(-de\002ned)e(classes.)p 0 5549 3901 4 v 0 5649 a Fo(2)2550 b(Chapter)23 b(1.)52 b(Whetting)25 b(Y)-12 b(our)23 b(Appetite)p eop end %%Page: 3 11 TeXDict begin 3 10 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3623 427 y Fn(TW)n(O)p 0 515 V 1253 978 a Fy(Using)58 b(the)f(Python)h (Inter)6 b(preter)0 1506 y Fl(2.1)121 b(In)n(v)m(oking)34 b(the)g(Inter)t(preter)0 1739 y Fs(The)25 b(Python)g(interpreter)f(is)i (usually)f(installed)h(as)g(`)p Ft(/usr/local/bin/p)n(ython)p Fs(')20 b(on)25 b(those)h(machines)f(where)g(it)h(is)h(a)n(v)n (ailable;)h(putting)0 1839 y(`)p Ft(/usr/local/bin)p Fs(')16 b(in)21 b(your)g(U)t Fm(N)t(I)t(X)i Fs(shell')-5 b(s)21 b(search)f(path)f(mak)o(es)h(it)h(possible)f(to)h(start)f(it)h (by)f(typing)f(the)h(command)236 2077 y Fk(python)0 2363 y Fs(to)28 b(the)g(shell.)49 b(Since)28 b(the)g(choice)f(of)h(the)g (directory)e(where)h(the)h(interpreter)f(li)n(v)o(es)h(is)h(an)e (installation)h(option,)g(other)g(places)g(are)0 2463 y(possible;)21 b(check)f(with)i(your)e(local)h(Python)e(guru)h(or)h (system)g(administrator)-5 b(.)26 b(\(E.g.,)20 b(`)p Ft(/usr/local/p)n(ython)p Fs(')d(is)22 b(a)g(popular)d(alternati)n(v)o (e)0 2563 y(location.\))0 2709 y(On)k(W)m(indo)n(ws)f(machines,)h(the)g (Python)e(installation)i(is)h(usually)e(placed)g(in)h(`)p Ft(C:\\Python25)p Fs(',)d(though)i(you)f(can)i(change)f(this)h(when)0 2809 y(you')l(re)15 b(running)h(the)h(installer)-5 b(.)25 b(T)-7 b(o)17 b(add)g(this)h(directory)d(to)j(your)e(path,)h(you)g(can) g(type)g(the)g(follo)n(wing)f(command)f(into)i(the)h(command)0 2909 y(prompt)h(in)h(a)h(DOS)f(box:)236 3147 y Fk(set)45 b(path=\045path\045;C:\\python25)0 3433 y Fs(T)-7 b(yping)19 b(an)g(end-of-\002le)f(character)h(\()p Fp(Control-D)f Fs(on)j(U)t Fm(N)t(I)t(X)r Fs(,)g Fp(Control-Z)e Fs(on)g(W)m(indo)n (ws\))g(at)h(the)g(primary)e(prompt)g(causes)j(the)0 3533 y(interpreter)k(to)i(e)o(xit)f(with)h(a)g(zero)g(e)o(xit)f (status.)45 b(If)27 b(that)g(doesn')o(t)e(w)o(ork,)i(you)f(can)g(e)o (xit)h(the)g(interpreter)e(by)h(typing)f(the)i(follo)n(wing)0 3632 y(commands:)d(`)p Fp(import)48 b(sys;)h(sys.exit\(\))p Fs('.)0 3779 y(The)26 b(interpreter')-5 b(s)24 b(line-editing)h (features)g(usually)h(aren')o(t)e(v)o(ery)h(sophisticated.)42 b(On)28 b(U)t Fm(N)t(I)t(X)r Fs(,)g(whoe)n(v)o(er)c(installed)i(the)g (interpreter)0 3879 y(may)h(ha)n(v)o(e)h(enabled)e(support)g(for)i(the) f(GNU)i(readline)d(library)-5 b(,)28 b(which)g(adds)f(more)g(elaborate) g(interacti)n(v)o(e)f(editing)h(and)g(history)0 3978 y(features.)50 b(Perhaps)28 b(the)g(quick)o(est)g(check)g(to)h(see)g (whether)f(command)e(line)j(editing)f(is)h(supported)e(is)j(typing)d (Control-P)h(to)h(the)0 4078 y(\002rst)c(Python)e(prompt)g(you)g(get.) 37 b(If)25 b(it)g(beeps,)f(you)g(ha)n(v)o(e)f(command)g(line)h (editing;)i(see)e(Appendix)f(A)i(for)e(an)h(introduction)e(to)j(the)0 4178 y(k)o(e)o(ys.)f(If)c(nothing)d(appears)i(to)g(happen,)f(or)h(if)h Fp(^P)g Fs(is)g(echoed,)e(command)f(line)j(editing)f(isn')o(t)g(a)n(v)n (ailable;)g(you')o(ll)f(only)h(be)g(able)h(to)f(use)0 4277 y(backspace)g(to)h(remo)o(v)o(e)e(characters)i(from)f(the)h (current)f(line.)0 4424 y(The)k(interpreter)e(operates)h(some)n(what)g (lik)o(e)h(the)i(U)t Fm(N)t(I)t(X)g Fs(shell:)31 b(when)23 b(called)f(with)h(standard)f(input)g(connected)f(to)i(a)g(tty)g(de)n (vice,)g(it)0 4524 y(reads)e(and)g(e)o(x)o(ecutes)f(commands)g (interacti)n(v)o(ely;)g(when)h(called)h(with)f(a)h(\002le)g(name)f(ar)o (gument)e(or)i(with)h(a)g(\002le)g(as)g(standard)e(input,)h(it)0 4623 y(reads)f(and)g(e)o(x)o(ecutes)f(a)h Fq(script)h Fs(from)e(that)i(\002le.)0 4770 y(A)h(second)e(w)o(ay)h(of)f(starting)h (the)g(interpreter)e(is)j(`)p Fr(python)49 b(-c)g Fq(command)h Fp([arg])f(...)p Fs(',)21 b(which)f(e)o(x)o(ecutes)g(the)h (statement\(s\))f(in)0 4870 y Fq(command)r Fs(,)e(analogous)f(to)i(the) g(shell')-5 b(s)19 b Fr(-c)g Fs(option.)24 b(Since)19 b(Python)f(statements)h(often)f(contain)g(spaces)h(or)f(other)h (characters)f(that)h(are)0 4970 y(special)h(to)h(the)f(shell,)g(it)h (is)g(best)g(to)f(quote)f Fq(command)i Fs(in)f(its)i(entirety)d(with)h (double)f(quotes.)0 5116 y(Some)25 b(Python)f(modules)g(are)i(also)f (useful)g(as)h(scripts.)41 b(These)25 b(can)g(be)g(in)m(v)n(ok)o(ed)f (using)h(`)p Fr(python)48 b(-m)i Fq(module)e Fp([arg])h(...)p Fs(',)0 5216 y(which)20 b(e)o(x)o(ecutes)f(the)h(source)f(\002le)i(for) f Fq(module)f Fs(as)i(if)f(you)g(had)f(spelled)h(out)g(its)h(full)f (name)g(on)g(the)g(command)e(line.)0 5363 y(Note)25 b(that)g(there)g (is)h(a)f(dif)n(ference)e(between)i(`)p Fp(python)48 b(file)p Fs(')24 b(and)h(`)p Fp(python)48 b()49 b Fs('\);)30 b(for)25 b(continuation)g (lines)i(it)g(prompts)0 2401 y(with)k(the)f Fq(secondary)f(pr)l(ompt)p Fs(,)k(by)d(def)o(ault)f(three)h(dots)h(\(`)p Fp(...)48 b Fs('\).)56 b(The)30 b(interpreter)e(prints)i(a)h(welcome)f(message)g (stating)h(its)0 2501 y(v)o(ersion)19 b(number)f(and)i(a)h(cop)o (yright)d(notice)h(before)g(printing)g(the)h(\002rst)h(prompt:)236 2739 y Fk(python)236 2830 y(Python)44 b(1.5.2b2)g(\(#1,)g(Feb)g(28)h (1999,)f(00:02:06\))88 b([GCC)44 b(2.8.1])g(on)g(sunos5)236 2921 y(Copyright)g(1991-1995)f(Stichting)g(Mathematisch)g(Centrum,)g (Amsterdam)236 3013 y(>>>)0 3299 y Fs(Continuation)18 b(lines)j(are)f(needed)f(when)h(entering)e(a)j(multi-line)e(construct.) 24 b(As)d(an)f(e)o(xample,)f(tak)o(e)h(a)h(look)e(at)i(this)f Fp(if)h Fs(statement:)236 3537 y Fk(>>>)45 b(the_world_is_flat)d(=)i(1) 236 3629 y(>>>)h(if)f(the_world_is_flat:)236 3720 y(...)224 b(print)44 b("Be)g(careful)g(not)g(to)h(fall)f(off!")236 3811 y(...)236 3902 y(Be)h(careful)e(not)i(to)f(fall)g(off!)0 4348 y Fl(2.2)121 b(The)34 b(Inter)t(preter)h(and)g(Its)e(En)n (vironment)0 4598 y Fj(2.2.1)100 b(Error)29 b(Handling)0 4801 y Fs(When)c(an)h(error)f(occurs,)h(the)f(interpreter)f(prints)i (an)f(error)g(message)g(and)g(a)h(stack)g(trace.)41 b(In)26 b(interacti)n(v)o(e)e(mode,)i(it)g(then)f(returns)0 4900 y(to)f(the)g(primary)f(prompt;)i(when)e(input)h(came)g(from)f(a)h (\002le,)i(it)f(e)o(xits)f(with)g(a)h(nonzero)d(e)o(xit)i(status)h (after)f(printing)e(the)j(stack)f(trace.)0 5000 y(\(Exceptions)19 b(handled)g(by)i(an)f Fp(except)g Fs(clause)h(in)g(a)g Fp(try)g Fs(statement)g(are)g(not)f(errors)g(in)h(this)g(conte)o(xt.\)) k(Some)c(errors)f(are)g(uncon-)0 5100 y(ditionally)d(f)o(atal)i(and)e (cause)h(an)g(e)o(xit)g(with)h(a)f(nonzero)f(e)o(xit;)h(this)h(applies) f(to)g(internal)g(inconsistencies)f(and)h(some)g(cases)h(of)f(running)0 5199 y(out)k(of)g(memory)-5 b(.)30 b(All)23 b(error)e(messages)i(are)f (written)g(to)h(the)f(standard)f(error)h(stream;)h(normal)e(output)g (from)h(e)o(x)o(ecuted)e(commands)0 5299 y(is)h(written)f(to)g (standard)f(output.)p 0 5549 3901 4 v 0 5649 a Fo(4)2351 b(Chapter)23 b(2.)52 b(Using)23 b(the)h(Python)f(Inter)r(preter)p eop end %%Page: 5 13 TeXDict begin 5 12 bop 0 83 a Fs(T)-7 b(yping)22 b(the)h(interrupt)f (character)f(\(usually)h(Control-C)h(or)g(DEL\))f(to)i(the)f(primary)e (or)i(secondary)e(prompt)h(cancels)h(the)g(input)f(and)0 183 y(returns)17 b(to)i(the)f(primary)e(prompt.)997 153 y Fi(1)1053 183 y Fs(T)-7 b(yping)17 b(an)h(interrupt)f(while)h(a)h (command)d(is)j(e)o(x)o(ecuting)d(raises)j(the)f Fp(KeyboardInterrupt)0 282 y Fs(e)o(xception,)g(which)i(may)f(be)i(handled)d(by)i(a)h Fp(try)f Fs(statement.)0 567 y Fj(2.2.2)100 b(Ex)m(ecutab)n(le)30 b(Python)f(Scr)q(ipts)0 770 y Fs(On)20 b(BSD'ish)j(U)t Fm(N)t(I)t(X)g Fs(systems,)e(Python)e(scripts)h(can)g(be)g(made)g (directly)f(e)o(x)o(ecutable,)f(lik)o(e)j(shell)f(scripts,)h(by)e (putting)g(the)i(line)236 1008 y Fk(#!)45 b(/usr/bin/env)e(python)0 1294 y Fs(\(assuming)24 b(that)h(the)g(interpreter)e(is)j(on)e(the)h (user')-5 b(s)26 b(P)-8 b(A)f(TH\))25 b(at)g(the)g(be)o(ginning)d(of)j (the)g(script)g(and)f(gi)n(ving)g(the)h(\002le)h(an)e(e)o(x)o(ecutable) 0 1394 y(mode.)54 b(The)30 b(`)p Fp(#!)p Fs(')56 b(must)30 b(be)g(the)h(\002rst)g(tw)o(o)f(characters)g(of)g(the)g(\002le.)56 b(On)30 b(some)h(platforms,)g(this)g(\002rst)g(line)f(must)h(end)f (with)g(a)2 1494 y(U)t Fm(N)t(I)t(X)r Fs(-style)21 b(line)g(ending)e (\(`)p Fp(\\n)p Fs('\),)h(not)g(a)i(Mac)e(OS)i(\(`)p Fp(\\r)p Fs('\))d(or)i(W)m(indo)n(ws)f(\(`)p Fp(\\r\\n)p Fs('\))f(line)i(ending.)k(Note)c(that)g(the)g(hash,)f(or)h(pound,)0 1593 y(character)m(,)d(`)p Fp(#)p Fs(',)i(is)h(used)f(to)g(start)h(a)g (comment)d(in)j(Python.)0 1740 y(The)f(script)g(can)g(be)g(gi)n(v)o(en) f(an)h(e)o(x)o(ecutable)e(mode,)h(or)h(permission,)f(using)h(the)g Fr(chmod)h Fs(command:)236 1978 y Fk($)45 b(chmod)f(+x)g(myscript.py)0 2403 y Fj(2.2.3)100 b(Source)29 b(Code)g(Encoding)0 2606 y Fs(It)c(is)h(possible)f(to)g(use)g(encodings)f(dif)n(ferent)f(than)j Fm(A)t(S)t(C)t(I)t(I)j Fs(in)c(Python)f(source)g(\002les.)41 b(The)24 b(best)i(w)o(ay)f(to)g(do)f(it)i(is)g(to)f(put)g(one)f(more)0 2706 y(special)c(comment)f(line)h(right)g(after)g(the)g Fp(#!)25 b Fs(line)c(to)f(de\002ne)g(the)g(source)f(\002le)i(encoding:) 236 2852 y Fk(#)45 b(-)371 2865 y(*)416 2852 y(-)f(coding:)g Fq(encoding)j Fp(-)1268 2867 y(*)1318 2852 y(-)0 3147 y Fs(W)m(ith)24 b(that)f(declaration,)g(all)h(characters)f(in)g(the)h (source)f(\002le)h(will)g(be)g(treated)e(as)j(ha)n(ving)d(the)i (encoding)d Fq(encoding)p Fs(,)h(and)h(it)h(will)h(be)0 3247 y(possible)20 b(to)h(directly)f(write)h(Unicode)e(string)i (literals)g(in)g(the)g(selected)f(encoding.)k(The)d(list)h(of)e (possible)g(encodings)f(can)i(be)f(found)0 3346 y(in)g(the)h Fq(Python)e(Libr)o(ary)h(Refer)m(ence)p Fs(,)f(in)h(the)h(section)f(on) f Fp(codecs)p Fs(.)0 3493 y(F)o(or)24 b(e)o(xample,)g(to)g(write)h (Unicode)e(literals)i(including)e(the)h(Euro)f(currenc)o(y)g(symbol,)h (the)g(ISO-8859-15)d(encoding)i(can)h(be)g(used,)0 3593 y(with)29 b(the)h(Euro)e(symbol)g(ha)n(ving)g(the)i(ordinal)e(v)n(alue) g(164.)51 b(This)30 b(script)f(will)h(print)f(the)g(v)n(alue)g(8364)f (\(the)g(Unicode)h(codepoint)0 3692 y(corresponding)17 b(to)j(the)g(Euro)f(symbol\))g(and)h(then)g(e)o(xit:)236 3839 y Fk(#)45 b(-)371 3852 y(*)416 3839 y(-)f(coding:)g(iso-8859-15)f (-)1447 3852 y(*)1492 3839 y(-)236 4022 y(currency)h(=)g(u")p 823 3995 34 4 v 823 4007 32 4 v 8 w(C")236 4113 y(print)g (ord\(currency\))0 4399 y Fs(If)34 b(your)f(editor)h(supports)f(sa)n (ving)h(\002les)h(as)g Fp(UTF-8)f Fs(with)h(a)f(UTF-8)g Fq(byte)g(or)m(der)h(mark)f Fs(\(aka)g(BOM\),)g(you)g(can)g(use)g(that) h(in-)0 4499 y(stead)j(of)g(an)g(encoding)d(declaration.)77 b(IDLE)37 b(supports)g(this)i(capability)d(if)j Fp (Options/General/Default)45 b(Source)0 4599 y(Encoding/UTF-8)27 b Fs(is)i(set.)51 b(Notice)29 b(that)f(this)h(signature)f(is)i(not)e (understood)e(in)j(older)f(Python)f(releases)i(\(2.2)f(and)g (earlier\),)0 4698 y(and)20 b(also)g(not)g(understood)e(by)h(the)i (operating)d(system)i(for)g(script)g(\002les)h(with)g Fp(#!)k Fs(lines)c(\(only)e(used)h(on)h(U)t Fm(N)t(I)t(X)j Fs(systems\).)0 4845 y(By)f(using)f(UTF-8)f(\(either)h(through)e(the)i (signature)g(or)g(an)g(encoding)e(declaration\),)h(characters)g(of)h (most)g(languages)f(in)i(the)f(w)o(orld)0 4945 y(can)c(be)g(used)f (simultaneously)g(in)h(string)f(literals)i(and)e(comments.)23 b(Using)18 b(non-)r Fm(A)t(S)t(C)t(I)t(I)i Fs(characters)d(in)h (identi\002ers)f(is)i(not)f(supported.)0 5044 y(T)-7 b(o)23 b(display)f(all)h(these)g(characters)f(properly)-5 b(,)21 b(your)g(editor)h(must)h(recognize)e(that)i(the)f(\002le)i(is)f (UTF-8,)g(and)f(it)i(must)e(use)h(a)g(font)f(that)0 5144 y(supports)d(all)i(the)f(characters)f(in)i(the)f(\002le.)p 0 5215 1560 4 v 90 5270 a Fh(1)120 5294 y Fm(A)c(problem)i(with)g(the)g (GNU)e(Readline)k(package)f(may)e(pre)n(v)o(ent)i(this.)p 0 5549 3901 4 v 0 5649 a Fo(2.2.)52 b(The)23 b(Inter)r(preter)h(and)f (Its)g(En)n(vironment)2354 b(5)p eop end %%Page: 6 14 TeXDict begin 6 13 bop 0 83 a Fj(2.2.4)100 b(The)29 b(Inter)o(activ)n (e)e(Star)t(tup)i(File)0 286 y Fs(When)23 b(you)f(use)h(Python)f (interacti)n(v)o(ely)-5 b(,)21 b(it)j(is)g(frequently)d(handy)g(to)i (ha)n(v)o(e)g(some)g(standard)e(commands)h(e)o(x)o(ecuted)f(e)n(v)o (ery)g(time)j(the)0 386 y(interpreter)18 b(is)j(started.)k(Y)-9 b(ou)19 b(can)h(do)f(this)i(by)e(setting)h(an)g(en)m(vironment)d(v)n (ariable)i(named)g(PYTHONST)-8 b(AR)j(TUP)20 b(to)g(the)g(name)f(of)h (a)0 485 y(\002le)h(containing)d(your)h(start-up)g(commands.)24 b(This)c(is)h(similar)g(to)f(the)g(`)p Ft(.pro\002le)p Fs(')e(feature)i(of)g(the)i(U)t Fm(N)t(I)t(X)h Fs(shells.)0 632 y(This)g(\002le)g(is)g(only)e(read)h(in)h(interacti)n(v)o(e)e (sessions,)i(not)f(when)g(Python)f(reads)h(commands)f(from)g(a)i (script,)g(and)e(not)h(when)g(`)p Ft(/de)n(v/tty)p Fs(')0 732 y(is)k(gi)n(v)o(en)e(as)i(the)g(e)o(xplicit)e(source)h(of)g (commands)f(\(which)g(otherwise)h(beha)n(v)o(es)f(lik)o(e)i(an)f (interacti)n(v)o(e)f(session\).)40 b(It)26 b(is)g(e)o(x)o(ecuted)d(in)0 831 y(the)g(same)h(namespace)e(where)h(interacti)n(v)o(e)f(commands)f (are)j(e)o(x)o(ecuted,)e(so)h(that)h(objects)f(that)g(it)i(de\002nes)e (or)g(imports)f(can)i(be)f(used)0 931 y(without)18 b(quali\002cation)g (in)h(the)g(interacti)n(v)o(e)e(session.)25 b(Y)-9 b(ou)19 b(can)f(also)i(change)d(the)i(prompts)f Fp(sys.ps1)g Fs(and)g Fp(sys.ps2)h Fs(in)g(this)g(\002le.)0 1078 y(If)28 b(you)g(w)o(ant)h(to)f(read)g(an)h(additional)e(start-up)h(\002le)h (from)e(the)i(current)e(directory)-5 b(,)28 b(you)g(can)g(program)f (this)i(in)f(the)h(global)e(start-)0 1177 y(up)20 b(\002le)h(using)f (code)f(lik)o(e)i(`)p Fp(if)49 b(os.path.isfile\('.pythonrc.py'\):)94 b(execfile\('.pythonrc.py'\))p Fs('.)21 b(If)0 1277 y(you)e(w)o(ant)i (to)f(use)g(the)h(startup)e(\002le)i(in)g(a)f(script,)g(you)f(must)i (do)e(this)i(e)o(xplicitly)e(in)i(the)f(script:)236 1515 y Fk(import)44 b(os)236 1607 y(filename)g(=)g (os.environ.get\('PYTHONSTARTUP'\))236 1698 y(if)h(filename)e(and)i (os.path.isfile\(filename\):)416 1789 y(execfile\(filename\))p 0 5549 3901 4 v 0 5649 a Fo(6)2351 b(Chapter)23 b(2.)52 b(Using)23 b(the)h(Python)f(Inter)r(preter)p eop end %%Page: 7 15 TeXDict begin 7 14 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3495 427 y Fn(THREE)p 0 515 V 721 978 a Fy(An)57 b(Inf)-6 b(or)5 b(mal)57 b(Introduction)h(to)g(Python)0 1465 y Fs(In)30 b(the)g(follo)n(wing)f(e)o(xamples,)j(input)d(and)h(output)f (are)i(distinguished)d(by)i(the)h(presence)e(or)h(absence)g(of)g (prompts)f(\(`)p Fp(\273>)49 b Fs(')30 b(and)0 1565 y(`)p Fp(...)49 b Fs('\):)34 b(to)25 b(repeat)f(the)h(e)o(xample,)f(you)f (must)i(type)f(e)n(v)o(erything)e(after)j(the)f(prompt,)g(when)g(the)h (prompt)e(appears;)j(lines)f(that)g(do)0 1664 y(not)18 b(be)o(gin)g(with)h(a)g(prompt)f(are)g(output)g(from)g(the)h (interpreter)-5 b(.)23 b(Note)c(that)f(a)i(secondary)d(prompt)g(on)h(a) i(line)e(by)h(itself)g(in)g(an)g(e)o(xample)0 1764 y(means)h(you)f (must)h(type)g(a)h(blank)e(line;)h(this)h(is)g(used)f(to)g(end)g(a)h (multi-line)e(command.)0 1911 y(Man)o(y)i(of)i(the)f(e)o(xamples)f(in)i (this)g(manual,)f(e)n(v)o(en)f(those)h(entered)g(at)h(the)f(interacti)n (v)o(e)f(prompt,)h(include)f(comments.)31 b(Comments)21 b(in)0 2010 y(Python)c(start)i(with)f(the)g(hash)g(character)m(,)e(`)p Fp(#)p Fs(',)i(and)g(e)o(xtend)f(to)h(the)g(end)f(of)h(the)g(physical)f (line.)25 b(A)18 b(comment)f(may)g(appear)g(at)i(the)f(start)0 2110 y(of)j(a)g(line)g(or)f(follo)n(wing)g(whitespace)g(or)g(code,)g(b) n(ut)h(not)g(within)f(a)i(string)e(literal.)27 b(A)22 b(hash)e(character)g(within)g(a)i(string)e(literal)h(is)h(just)0 2210 y(a)f(hash)f(character)-5 b(.)0 2357 y(Some)20 b(e)o(xamples:)236 2595 y Fk(#)45 b(this)f(is)h(the)f(first)g(comment)236 2686 y(SPAM)g(=)h(1)762 b(#)45 b(and)f(this)g(is)h(the)f(second)g (comment)1357 2777 y(#)h(...)f(and)g(now)h(a)f(third!)236 2869 y(STRING)g(=)h("#)f(This)g(is)h(not)f(a)h(comment.")0 3317 y Fl(3.1)121 b(Using)34 b(Python)g(as)f(a)h(Calculator)0 3550 y Fs(Let')-5 b(s)24 b(try)f(some)g(simple)g(Python)f(commands.)32 b(Start)23 b(the)h(interpreter)d(and)i(w)o(ait)g(for)g(the)g(primary)f (prompt,)g(`)p Fp(\273>)49 b Fs('.)34 b(\(It)23 b(shouldn')o(t)0 3650 y(tak)o(e)d(long.\))0 3935 y Fj(3.1.1)100 b(Numbers)0 4138 y Fs(The)26 b(interpreter)e(acts)j(as)g(a)g(simple)f(calculator:) 36 b(you)25 b(can)h(type)g(an)g(e)o(xpression)e(at)j(it)g(and)f(it)g (will)h(write)g(the)f(v)n(alue.)42 b(Expression)0 4237 y(syntax)22 b(is)i(straightforw)o(ard:)k(the)22 b(operators)g Fp(+)p Fs(,)h Fp(-)p Fs(,)1530 4252 y Fp(*)1603 4237 y Fs(and)f Fp(/)h Fs(w)o(ork)f(just)h(lik)o(e)g(in)g(most)g(other)f (languages)f(\(for)h(e)o(xample,)g(P)o(ascal)h(or)0 4337 y(C\);)e(parentheses)e(can)h(be)g(used)g(for)g(grouping.)i(F)o(or)e(e)o (xample:)p 0 5549 3901 4 v 3854 5649 a Fo(7)p eop end %%Page: 8 16 TeXDict begin 8 15 bop 236 174 a Fk(>>>)45 b(2+2)236 266 y(4)236 357 y(>>>)g(#)f(This)g(is)h(a)g(comment)236 448 y(...)g(2+2)236 540 y(4)236 631 y(>>>)g(2+2)89 b(#)44 b(and)h(a)f(comment)g(on)h(the)f(same)g(line)g(as)h(code)236 722 y(4)236 814 y(>>>)g(\(50-5)641 827 y(*)686 814 y(6\)/4)236 905 y(5)236 996 y(>>>)g(#)f(Integer)g(division)f(returns)h(the)g (floor:)236 1088 y(...)h(7/3)236 1179 y(2)236 1270 y(>>>)g(7/-3)236 1362 y(-3)0 1648 y Fs(The)17 b(equal)f(sign)h(\(`)p Fp(=)p Fs('\))e(is)j(used)f(to)g(assign)g(a)g(v)n(alue)f(to)h(a)h(v)n (ariable.)k(Afterw)o(ards,)17 b(no)f(result)h(is)h(displayed)d(before)h (the)h(ne)o(xt)f(interacti)n(v)o(e)0 1747 y(prompt:)236 1986 y Fk(>>>)45 b(width)f(=)g(20)236 2077 y(>>>)h(height)e(=)i(5)864 2090 y(*)909 2077 y(9)236 2168 y(>>>)g(width)685 2181 y(*)774 2168 y(height)236 2260 y(900)0 2546 y Fs(A)21 b(v)n(alue)e(can)h(be)g(assigned)g(to)g(se)n(v)o(eral)g(v)n(ariables)f (simultaneously:)236 2784 y Fk(>>>)45 b(x)f(=)h(y)g(=)f(z)h(=)g(0)89 b(#)45 b(Zero)f(x,)g(y)h(and)f(z)236 2875 y(>>>)h(x)236 2967 y(0)236 3058 y(>>>)g(y)236 3149 y(0)236 3241 y(>>>)g(z)236 3332 y(0)0 3618 y Fs(There)26 b(is)j(full)e(support)f(for)g(\003oating) h(point;)j(operators)25 b(with)j(mix)o(ed)e(type)h(operands)e(con)m(v)o (ert)g(the)j(inte)o(ger)e(operand)f(to)i(\003oating)0 3718 y(point:)236 3956 y Fk(>>>)45 b(3)505 3969 y(*)595 3956 y(3.75)f(/)h(1.5)236 4047 y(7.5)236 4139 y(>>>)g(7.0)f(/)h(2)236 4230 y(3.5)0 4516 y Fs(Comple)o(x)24 b(numbers)f(are)i(also)g (supported;)h(imaginary)d(numbers)g(are)i(written)g(with)g(a)g(suf)n (\002x)g(of)g(`)p Fp(j)p Fs(')f(or)h(`)p Fp(J)p Fs('.)f(Comple)o(x)g (numbers)0 4616 y(with)d(a)h(nonzero)d(real)i(component)e(are)i (written)g(as)h(`)p Fp(\()p Fq(r)m(eal)p Fp(+)p Fq(ima)o(g)p Fp(j\))p Fs(',)e(or)h(can)g(be)g(created)f(with)i(the)f(`)p Fp(complex\()p Fq(r)m(eal)p Fp(,)47 b Fq(ima)o(g)p Fp(\))p Fs(')0 4715 y(function.)p 0 5549 3901 4 v 0 5649 a Fo(8)2137 b(Chapter)23 b(3.)52 b(An)24 b(Inf)n(or)r(mal)f(Introduction)h(to)f (Python)p eop end %%Page: 9 17 TeXDict begin 9 16 bop 236 174 a Fk(>>>)45 b(1j)550 187 y(*)640 174 y(1J)236 266 y(\(-1+0j\))236 357 y(>>>)g(1j)550 370 y(*)640 357 y(complex\(0,1\))236 448 y(\(-1+0j\))236 540 y(>>>)g(3+1j)596 553 y(*)641 540 y(3)236 631 y(\(3+3j\))236 722 y(>>>)g(\(3+1j\))686 735 y(*)731 722 y(3)236 814 y(\(9+3j\))236 905 y(>>>)g(\(1+2j\)/\(1+1j\))236 996 y(\(1.5+0.5j\))0 1282 y Fs(Comple)o(x)19 b(numbers)g(are)h(al)o(w)o (ays)h(represented)e(as)i(tw)o(o)f(\003oating)g(point)g(numbers,)e(the) j(real)f(and)g(imaginary)e(part.)25 b(T)-7 b(o)21 b(e)o(xtract)e(these) 0 1382 y(parts)h(from)f(a)i(comple)o(x)d(number)h Fq(z)p Fs(,)i(use)f Fq(z)p Fp(.real)g Fs(and)g Fq(z)p Fp(.imag)p Fs(.)236 1529 y Fk(>>>)45 b(a=1.5+0.5j)236 1620 y(>>>)g(a.real)236 1712 y(1.5)236 1803 y(>>>)g(a.imag)236 1894 y(0.5)0 2180 y Fs(The)29 b(con)m(v)o(ersion)d(functions)i(to)h(\003oating)f(point)g (and)h(inte)o(ger)f(\()p Fp(float\(\))p Fs(,)i Fp(int\(\))f Fs(and)f Fp(long\(\))p Fs(\))g(don')o(t)g(w)o(ork)g(for)g(comple)o(x)0 2280 y(numbers)i(\227)i(there)f(is)i(no)e(one)g(correct)g(w)o(ay)h(to)g (con)m(v)o(ert)d(a)j(comple)o(x)e(number)g(to)i(a)g(real)f(number)-5 b(.)58 b(Use)33 b Fp(abs\()p Fq(z)p Fp(\))e Fs(to)h(get)g(its)0 2380 y(magnitude)18 b(\(as)j(a)f(\003oat\))g(or)g Fp(z.real)g Fs(to)g(get)h(its)g(real)f(part.)236 2618 y Fk(>>>)45 b(a=3.0+4.0j)236 2709 y(>>>)g(float\(a\))236 2801 y(Traceback)f(\(most) g(recent)f(call)i(last\):)326 2892 y(File)f("",)f(line)h(1,)h (in)f(?)236 2983 y(TypeError:)f(can't)h(convert)g(complex)g(to)g (float;)g(use)g(abs\(z\))236 3074 y(>>>)h(a.real)236 3166 y(3.0)236 3257 y(>>>)g(a.imag)236 3348 y(4.0)236 3440 y(>>>)g(abs\(a\))88 b(#)45 b(sqrt\(a.real)1359 3453 y(**)1449 3440 y(2)d(+)j(a.imag)1896 3453 y(**)1986 3440 y(2\))236 3531 y(5.0)236 3622 y(>>>)0 3909 y Fs(In)27 b(interacti)n(v)o(e)g(mode,)h(the)g(last)h(printed)d(e)o(xpression)g (is)j(assigned)e(to)h(the)g(v)n(ariable)e Fp(_)p Fs(.)48 b(This)28 b(means)g(that)f(when)h(you)e(are)i(using)0 4008 y(Python)19 b(as)i(a)g(desk)f(calculator)m(,)e(it)j(is)g(some)n (what)e(easier)i(to)f(continue)f(calculations,)g(for)g(e)o(xample:)236 4155 y Fk(>>>)45 b(tax)f(=)h(12.5)f(/)g(100)236 4246 y(>>>)h(price)f(=)g(100.50)236 4338 y(>>>)h(price)685 4351 y(*)774 4338 y(tax)236 4429 y(12.5625)236 4520 y(>>>)g(price)f(+)g (_)236 4612 y(113.0625)236 4703 y(>>>)h(round\(_,)e(2\))236 4794 y(113.06)236 4886 y(>>>)0 5172 y Fs(This)23 b(v)n(ariable)f (should)g(be)h(treated)g(as)h(read-only)c(by)j(the)g(user)-5 b(.)34 b(Don')o(t)22 b(e)o(xplicitly)g(assign)h(a)g(v)n(alue)g(to)g(it) h(\227)f(you)f(w)o(ould)h(create)g(an)0 5272 y(independent)18 b(local)i(v)n(ariable)f(with)h(the)g(same)h(name)e(masking)g(the)i(b)n (uilt-in)e(v)n(ariable)g(with)i(its)g(magic)f(beha)n(vior)-5 b(.)p 0 5549 3901 4 v 0 5649 a Fo(3.1.)52 b(Using)23 b(Python)h(as)f(a)g(Calculator)2594 b(9)p eop end %%Page: 10 18 TeXDict begin 10 17 bop 0 83 a Fj(3.1.2)100 b(Str)q(ings)0 286 y Fs(Besides)22 b(numbers,)e(Python)g(can)h(also)g(manipulate)f (strings,)h(which)f(can)h(be)h(e)o(xpressed)d(in)j(se)n(v)o(eral)e(w)o (ays.)28 b(The)o(y)20 b(can)h(be)h(enclosed)0 386 y(in)e(single)g (quotes)g(or)g(double)f(quotes:)236 532 y Fk(>>>)45 b('spam)f(eggs')236 624 y('spam)g(eggs')236 715 y(>>>)h('doesn\\'t')236 806 y("doesn't")236 898 y(>>>)g("doesn't")236 989 y("doesn't")236 1080 y(>>>)g('"Yes,")e(he)i(said.')236 1172 y('"Yes,")f(he)g(said.')236 1263 y(>>>)h("\\"Yes,\\")e(he)h(said.")236 1354 y('"Yes,")g(he)g (said.')236 1446 y(>>>)h('"Isn\\'t,")e(she)h(said.')236 1537 y('"Isn\\'t,")f(she)i(said.')0 1823 y Fs(String)26 b(literals)h(can)f(span)h(multiple)e(lines)i(in)g(se)n(v)o(eral)f(w)o (ays.)44 b(Continuation)25 b(lines)h(can)h(be)f(used,)i(with)e(a)h (backslash)f(as)h(the)g(last)0 1923 y(character)19 b(on)h(the)g(line)g (indicating)f(that)h(the)h(ne)o(xt)e(line)h(is)h(a)g(logical)f (continuation)e(of)i(the)g(line:)236 2070 y Fk(hello)44 b(=)h("This)f(is)g(a)h(rather)f(long)g(string)g(containing\\n\\)236 2161 y(several)g(lines)g(of)g(text)h(just)f(as)g(you)h(would)e(do)i(in) f(C.\\n\\)416 2252 y(Note)g(that)g(whitespace)f(at)i(the)f(beginning)f (of)i(the)f(line)g(is\\)281 2344 y(significant.")236 2526 y(print)g(hello)0 2812 y Fs(Note)25 b(that)h(ne)n(wlines)f(still)h (need)f(to)h(be)f(embedded)e(in)i(the)h(string)f(using)g Fp(\\n)p Fs(;)j(the)d(ne)n(wline)g(follo)n(wing)f(the)h(trailing)g (backslash)g(is)0 2912 y(discarded.)f(This)c(e)o(xample)f(w)o(ould)g (print)h(the)g(follo)n(wing:)236 3150 y Fk(This)44 b(is)h(a)g(rather)e (long)i(string)e(containing)236 3242 y(several)h(lines)g(of)g(text)h (just)f(as)g(you)h(would)e(do)i(in)f(C.)416 3333 y(Note)g(that)g (whitespace)f(at)i(the)f(beginning)f(of)i(the)f(line)g(is)h (significant.)0 3619 y Fs(If)18 b(we)g(mak)o(e)f(the)h(string)f (literal)h(a)h(\223ra)o(w\224)e(string,)g(ho)n(we)n(v)o(er)m(,)f(the)i Fp(\\n)g Fs(sequences)f(are)h(not)f(con)m(v)o(erted)e(to)j(ne)n (wlines,)g(b)n(ut)f(the)h(backslash)0 3719 y(at)f(the)g(end)f(of)g(the) g(line,)i(and)e(the)g(ne)n(wline)g(character)f(in)i(the)g(source,)f (are)g(both)g(included)f(in)i(the)g(string)f(as)h(data.)24 b(Thus,)16 b(the)h(e)o(xample:)236 3957 y Fk(hello)44 b(=)h(r"This)f(is)g(a)h(rather)f(long)g(string)g(containing\\n\\)236 4048 y(several)g(lines)g(of)g(text)h(much)f(as)g(you)h(would)e(do)i(in) f(C.")236 4231 y(print)g(hello)0 4517 y Fs(w)o(ould)19 b(print:)236 4755 y Fk(This)44 b(is)h(a)g(rather)e(long)i(string)e (containing\\n\\)236 4847 y(several)h(lines)g(of)g(text)h(much)f(as)g (you)h(would)e(do)i(in)f(C.)0 5133 y Fs(Or)m(,)19 b(strings)h(can)f(be) h(surrounded)d(in)i(a)h(pair)g(of)f(matching)f(triple-quotes:)23 b Fp(""")d Fs(or)f Fp(''')p Fs(.)25 b(End)19 b(of)g(lines)h(do)g(not)f (need)g(to)h(be)f(escaped)0 5232 y(when)h(using)f(triple-quotes,)f(b)n (ut)j(the)o(y)e(will)i(be)f(included)f(in)h(the)g(string.)p 0 5549 3901 4 v 0 5649 a Fo(10)2091 b(Chapter)23 b(3.)52 b(An)24 b(Inf)n(or)r(mal)f(Introduction)h(to)f(Python)p eop end %%Page: 11 19 TeXDict begin 11 18 bop 236 83 a Fk(print)44 b(""")236 174 y(Usage:)g(thingy)g([OPTIONS])460 266 y(-h)1076 b(Display)44 b(this)g(usage)g(message)460 357 y(-H)h(hostname)671 b(Hostname)43 b(to)i(connect)f(to)236 448 y(""")0 734 y Fs(produces)18 b(the)j(follo)n(wing)d(output:)236 881 y Fk(Usage:)44 b(thingy)g([OPTIONS])460 973 y(-h)1076 b(Display)44 b(this)g(usage)g(message)460 1064 y(-H)h(hostname)671 b(Hostname)43 b(to)i(connect)f(to)0 1350 y Fs(The)17 b(interpreter)f(prints)g(the)i(result)f(of)g(string)g(operations)f(in)h (the)g(same)h(w)o(ay)f(as)h(the)o(y)e(are)i(typed)e(for)h(input:)23 b(inside)17 b(quotes,)g(and)f(with)0 1450 y(quotes)j(and)h(other)f (funn)o(y)g(characters)g(escaped)g(by)h(backslashes,)f(to)i(sho)n(w)f (the)g(precise)g(v)n(alue.)k(The)c(string)f(is)j(enclosed)d(in)h (double)0 1549 y(quotes)27 b(if)g(the)h(string)f(contains)f(a)i(single) f(quote)g(and)g(no)g(double)f(quotes,)i(else)g(it')-5 b(s)28 b(enclosed)f(in)g(single)g(quotes.)46 b(\(The)27 b Fp(print)0 1649 y Fs(statement,)20 b(described)f(later)m(,)g(can)h (be)g(used)g(to)h(write)f(strings)g(without)g(quotes)f(or)h(escapes.\)) 0 1796 y(Strings)g(can)g(be)g(concatenated)e(\(glued)h(together\))g (with)h(the)g Fp(+)h Fs(operator)m(,)d(and)h(repeated)g(with)2786 1811 y Fp(*)2836 1796 y Fs(:)236 2034 y Fk(>>>)45 b(word)f(=)g('Help')g (+)h('A')236 2125 y(>>>)g(word)236 2217 y('HelpA')236 2308 y(>>>)g('<')f(+)h(word)865 2321 y(*)910 2308 y(5)e(+)i('>')236 2399 y('')0 2686 y Fs(T)-7 b(w)o(o)21 b(string)g(literals)g(ne)o(xt)f(to)h(each)g(other)f(are)h (automatically)e(concatenated;)g(the)i(\002rst)h(line)f(abo)o(v)o(e)e (could)h(also)h(ha)n(v)o(e)g(been)f(written)0 2785 y(`)p Fp(word)49 b(=)g('Help')g('A')p Fs(';)20 b(this)h(only)e(w)o(orks)h (with)g(tw)o(o)h(literals,)f(not)g(with)g(arbitrary)f(string)h(e)o (xpressions:)236 3023 y Fk(>>>)45 b('str')f('ing')851 b(#)89 b(<-)g(This)44 b(is)h(ok)236 3115 y('string')236 3206 y(>>>)g('str'.strip\(\))d(+)j('ing')134 b(#)89 b(<-)g(This)44 b(is)h(ok)236 3297 y('string')236 3389 y(>>>)g('str'.strip\(\))d('ing') 224 b(#)89 b(<-)g(This)44 b(is)h(invalid)326 3480 y(File)f("",)f (line)h(1,)h(in)f(?)416 3571 y('str'.strip\(\))e('ing')1223 3663 y(^)236 3754 y(SyntaxError:)h(invalid)h(syntax)0 4040 y Fs(Strings)29 b(can)f(be)h(subscripted)f(\(inde)o(x)o(ed\);)j (lik)o(e)e(in)g(C,)g(the)g(\002rst)h(character)e(of)g(a)i(string)e(has) i(subscript)e(\(inde)o(x\))f(0.)51 b(There)28 b(is)i(no)0 4140 y(separate)24 b(character)f(type;)j(a)e(character)f(is)i(simply)f (a)h(string)f(of)g(size)h(one.)36 b(Lik)o(e)24 b(in)g(Icon,)h (substrings)e(can)h(be)g(speci\002ed)g(with)h(the)0 4239 y Fq(slice)c(notation)p Fs(:)j(tw)o(o)c(indices)g(separated)f(by)h(a)h (colon.)236 4478 y Fk(>>>)45 b(word[4])236 4569 y('A')236 4660 y(>>>)g(word[0:2])236 4752 y('He')236 4843 y(>>>)g(word[2:4])236 4934 y('lp')0 5220 y Fs(Slice)22 b(indices)f(ha)n(v)o(e)g(useful)g(def) o(aults;)h(an)f(omitted)g(\002rst)h(inde)o(x)e(def)o(aults)h(to)h (zero,)f(an)g(omitted)g(second)g(inde)o(x)f(def)o(aults)h(to)g(the)h (size)0 5320 y(of)e(the)g(string)g(being)f(sliced.)p 0 5549 3901 4 v 0 5649 a Fo(3.1.)52 b(Using)23 b(Python)h(as)f(a)g (Calculator)2548 b(11)p eop end %%Page: 12 20 TeXDict begin 12 19 bop 236 174 a Fk(>>>)45 b(word[:2])178 b(#)44 b(The)h(first)f(two)g(characters)236 266 y('He')236 357 y(>>>)h(word[2:])178 b(#)44 b(Everything)f(except)h(the)g(first)g (two)h(characters)236 448 y('lpA')0 734 y Fs(Unlik)o(e)19 b(a)i(C)f(string,)g(Python)e(strings)i(cannot)f(be)g(changed.)k (Assigning)c(to)h(an)g(inde)o(x)o(ed)e(position)h(in)h(the)f(string)h (results)g(in)g(an)g(error:)236 973 y Fk(>>>)45 b(word[0])e(=)i('x')236 1064 y(Traceback)f(\(most)g(recent)f(call)i(last\):)326 1155 y(File)f("",)f(line)h(1,)h(in)f(?)236 1247 y(TypeError:)f(object)h(doesn't)g(support)g(item)g(assignment)236 1338 y(>>>)h(word[:1])e(=)i('Splat')236 1429 y(Traceback)f(\(most)g (recent)f(call)i(last\):)326 1521 y(File)f("",)f(line)h(1,)h(in) f(?)236 1612 y(TypeError:)f(object)h(doesn't)g(support)g(slice)f (assignment)0 1898 y Fs(Ho)n(we)n(v)o(er)m(,)18 b(creating)h(a)i(ne)n (w)f(string)g(with)g(the)g(combined)e(content)i(is)h(easy)f(and)g(ef)n (\002cient:)236 2136 y Fk(>>>)45 b('x')f(+)h(word[1:])236 2228 y('xelpA')236 2319 y(>>>)g('Splat')e(+)i(word[4])236 2410 y('SplatA')0 2696 y Fs(Here')-5 b(s)20 b(a)h(useful)f(in)m(v)n (ariant)e(of)i(slice)h(operations:)j Fp(s[:i])49 b(+)g(s[i:])25 b Fs(equals)20 b Fp(s)p Fs(.)236 2843 y Fk(>>>)45 b(word[:2])e(+)i (word[2:])236 2935 y('HelpA')236 3026 y(>>>)g(word[:3])e(+)i(word[3:]) 236 3117 y('HelpA')0 3404 y Fs(De)o(generate)16 b(slice)i(indices)f (are)g(handled)f(gracefully:)22 b(an)17 b(inde)o(x)f(that)i(is)g(too)f (lar)o(ge)f(is)j(replaced)d(by)h(the)g(string)g(size,)h(an)g(upper)d (bound)0 3503 y(smaller)20 b(than)g(the)g(lo)n(wer)g(bound)e(returns)h (an)i(empty)e(string.)236 3650 y Fk(>>>)45 b(word[1:100])236 3741 y('elpA')236 3833 y(>>>)g(word[10:])236 3924 y('')236 4015 y(>>>)g(word[2:1])236 4107 y('')0 4393 y Fs(Indices)20 b(may)f(be)h(ne)o(gati)n(v)o(e)e(numbers,)h(to)h(start)h(counting)d (from)h(the)i(right.)j(F)o(or)c(e)o(xample:)236 4540 y Fk(>>>)45 b(word[-1])222 b(#)45 b(The)f(last)h(character)236 4631 y('A')236 4722 y(>>>)g(word[-2])222 b(#)45 b(The)f(last-but-one)f (character)236 4814 y('p')236 4905 y(>>>)i(word[-2:])177 b(#)45 b(The)f(last)h(two)f(characters)236 4996 y('pA')236 5088 y(>>>)h(word[:-2])177 b(#)45 b(Everything)e(except)h(the)g(last)g (two)h(characters)236 5179 y('Hel')p 0 5549 3901 4 v 0 5649 a Fo(12)2091 b(Chapter)23 b(3.)52 b(An)24 b(Inf)n(or)r(mal)f (Introduction)h(to)f(Python)p eop end %%Page: 13 21 TeXDict begin 13 20 bop 0 83 a Fs(But)21 b(note)e(that)i(-0)f(is)h (really)f(the)g(same)g(as)h(0,)f(so)h(it)g(does)e(not)h(count)g(from)f (the)h(right!)236 230 y Fk(>>>)45 b(word[-0])222 b(#)45 b(\(since)f(-0)g(equals)g(0\))236 321 y('H')0 607 y Fs(Out-of-range)17 b(ne)o(gati)n(v)o(e)h(slice)j(indices)f(are)g(truncated,)f(b)n(ut)h (don')o(t)e(try)i(this)h(for)f(single-element)e(\(non-slice\))h (indices:)236 754 y Fk(>>>)45 b(word[-100:])236 846 y('HelpA')236 937 y(>>>)g(word[-10])177 b(#)45 b(error)236 1028 y(Traceback)f(\(most) g(recent)f(call)i(last\):)326 1120 y(File)f("",)f(line)h(1,)h (in)f(?)236 1211 y(IndexError:)f(string)h(index)g(out)g(of)h(range)0 1497 y Fs(The)19 b(best)g(w)o(ay)h(to)f(remember)e(ho)n(w)i(slices)h(w) o(ork)f(is)h(to)f(think)g(of)g(the)g(indices)g(as)h(pointing)d Fq(between)i Fs(characters,)f(with)i(the)f(left)g(edge)0 1597 y(of)j(the)g(\002rst)h(character)e(numbered)e(0.)31 b(Then)21 b(the)h(right)g(edge)f(of)h(the)g(last)h(character)e(of)h(a)h (string)e(of)h Fq(n)g Fs(characters)g(has)g(inde)o(x)f Fq(n)p Fs(,)h(for)0 1696 y(e)o(xample:)281 1935 y Fk (+---+---+---+---+---+)281 2026 y(|)45 b(H)f(|)h(e)g(|)f(l)h(|)g(p)f(|) h(A)g(|)281 2117 y(+---+---+---+---+---+)281 2208 y(0)134 b(1)h(2)f(3)g(4)h(5)236 2300 y(-5)90 b(-4)f(-3)g(-2)h(-1)0 2586 y Fs(The)23 b(\002rst)h(ro)n(w)f(of)h(numbers)d(gi)n(v)o(es)i(the) h(position)e(of)i(the)f(indices)g(0...5)g(in)g(the)h(string;)h(the)e (second)g(ro)n(w)g(gi)n(v)o(es)g(the)g(corresponding)0 2686 y(ne)o(gati)n(v)o(e)18 b(indices.)25 b(The)20 b(slice)h(from)e Fq(i)i Fs(to)f Fq(j)h Fs(consists)f(of)g(all)h(characters)e(between)h (the)g(edges)g(labeled)f Fq(i)i Fs(and)f Fq(j)p Fs(,)g(respecti)n(v)o (ely)-5 b(.)0 2832 y(F)o(or)20 b(non-ne)o(gati)n(v)o(e)d(indices,)j (the)h(length)e(of)i(a)g(slice)g(is)h(the)e(dif)n(ference)f(of)h(the)h (indices,)f(if)h(both)f(are)g(within)g(bounds.)25 b(F)o(or)20 b(e)o(xample,)0 2932 y(the)g(length)g(of)f Fp(word[1:3])h Fs(is)h(2.)0 3079 y(The)f(b)n(uilt-in)f(function)g Fp(len\(\))h Fs(returns)f(the)h(length)g(of)f(a)i(string:)236 3317 y Fk(>>>)45 b(s)f(=)h('supercalifragilisticexpialidocious')236 3409 y(>>>)g(len\(s\))236 3500 y(34)0 3787 y Fr(See)21 b(Also:)0 3934 y Fq(Sequence)d(T)-6 b(ypes)0 4033 y Fg(\()p Ft(../lib/typesseq.html)l Fg(\))208 4133 y Fs(Strings,)21 b(and)h(the)f(Unicode)g(strings)h(described)e(in)i(the)g(ne)o(xt)f (section,)h(are)g(e)o(xamples)f(of)g Fq(sequence)g(types)p Fs(,)h(and)f(support)g(the)208 4233 y(common)d(operations)h(supported)f (by)h(such)h(types.)0 4380 y Fq(String)g(Methods)0 4479 y Fg(\()p Ft(../lib/str)q(ing-methods)o(.html)-6 b Fg(\))208 4579 y Fs(Both)20 b(strings)g(and)g(Unicode)f(strings)h(support)f(a)h (lar)o(ge)f(number)g(of)h(methods)f(for)g(basic)i(transformations)d (and)h(searching.)0 4726 y Fq(String)h(F)-9 b(ormatting)19 b(Oper)o(ations)0 4825 y Fg(\()p Ft(../lib/typesseq-str)q(ings)o(.html) -5 b Fg(\))208 4925 y Fs(The)22 b(formatting)f(operations)h(in)m(v)n (ok)o(ed)f(when)i(strings)g(and)f(Unicode)g(strings)h(are)g(the)h(left) f(operand)e(of)i(the)g Fp(\045)g Fs(operator)f(are)208 5025 y(described)c(in)j(more)e(detail)h(here.)p 0 5549 3901 4 v 0 5649 a Fo(3.1.)52 b(Using)23 b(Python)h(as)f(a)g(Calculator) 2548 b(13)p eop end %%Page: 14 22 TeXDict begin 14 21 bop 0 83 a Fj(3.1.3)100 b(Unicode)29 b(Str)q(ings)0 286 y Fs(Starting)23 b(with)g(Python)g(2.0)f(a)i(ne)n(w) g(data)f(type)g(for)g(storing)f(te)o(xt)h(data)h(is)g(a)n(v)n(ailable)f (to)h(the)f(programmer:)29 b(the)23 b(Unicode)f(object.)34 b(It)0 386 y(can)23 b(be)h(used)g(to)f(store)h(and)f(manipulate)f (Unicode)h(data)h(\(see)g Ft(http://www)l(.unicode)o(.o)o(rg/)-6 b Fs(\))24 b(and)f(inte)o(grates)f(well)j(with)f(the)f(e)o(xisting)0 485 y(string)d(objects,)g(pro)o(viding)d(auto-con)m(v)o(ersions)f (where)k(necessary)-5 b(.)0 632 y(Unicode)25 b(has)i(the)f(adv)n (antage)e(of)i(pro)o(viding)e(one)i(ordinal)f(for)h(e)n(v)o(ery)f (character)g(in)h(e)n(v)o(ery)f(script)h(used)g(in)h(modern)d(and)i (ancient)0 732 y(te)o(xts.)42 b(Pre)n(viously)-5 b(,)25 b(there)h(were)f(only)g(256)g(possible)h(ordinals)f(for)g(script)h (characters.)41 b(T)-6 b(e)o(xts)25 b(were)h(typically)f(bound)f(to)i (a)g(code)0 831 y(page)i(which)g(mapped)e(the)j(ordinals)e(to)i(script) f(characters.)49 b(This)29 b(lead)f(to)h(v)o(ery)e(much)g(confusion)g (especially)h(with)g(respect)g(to)0 931 y(internationalization)i (\(usually)i(written)g(as)h(`)p Fp(i18n)p Fs(')f(\227)h(`)p Fp(i)p Fs(')f(+)h(18)g(characters)e(+)i(`)p Fp(n)p Fs('\))f(of)h(softw) o(are.)61 b(Unicode)32 b(solv)o(es)g(these)0 1031 y(problems)19 b(by)h(de\002ning)e(one)i(code)g(page)f(for)h(all)g(scripts.)0 1177 y(Creating)g(Unicode)f(strings)h(in)g(Python)f(is)i(just)g(as)g (simple)f(as)h(creating)e(normal)g(strings:)236 1416 y Fk(>>>)45 b(u'Hello)e(World)h(!')236 1507 y(u'Hello)g(World)g(!')0 1793 y Fs(The)24 b(small)h(`)p Fp(u)p Fs(')f(in)g(front)g(of)g(the)g (quote)f(indicates)h(that)h(a)f(Unicode)g(string)g(is)h(supposed)e(to)h (be)h(created.)36 b(If)24 b(you)g(w)o(ant)g(to)h(include)0 1893 y(special)20 b(characters)f(in)i(the)f(string,)g(you)f(can)h(do)f (so)i(by)f(using)f(the)h(Python)f Fq(Unicode-Escape)f Fs(encoding.)23 b(The)c(follo)n(wing)g(e)o(xample)0 1992 y(sho)n(ws)h(ho)n(w:)236 2231 y Fk(>>>)45 b(u'Hello\\u0020World)c(!') 236 2322 y(u'Hello)j(World)g(!')0 2608 y Fs(The)30 b(escape)g(sequence) f Fp(\\u0020)g Fs(indicates)h(to)g(insert)g(the)h(Unicode)e(character)f (with)j(the)f(ordinal)f(v)n(alue)g(0x0020)f(\(the)i(space)0 2708 y(character\))19 b(at)h(the)g(gi)n(v)o(en)f(position.)0 2855 y(Other)29 b(characters)e(are)i(interpreted)e(by)i(using)f(their)h (respecti)n(v)o(e)f(ordinal)f(v)n(alues)i(directly)f(as)i(Unicode)d (ordinals.)50 b(If)29 b(you)f(ha)n(v)o(e)0 2954 y(literal)21 b(strings)f(in)h(the)f(standard)g(Latin-1)f(encoding)g(that)h(is)i (used)e(in)h(man)o(y)e(W)-7 b(estern)21 b(countries,)e(you)h(will)h (\002nd)f(it)i(con)m(v)o(enient)17 b(that)0 3054 y(the)j(lo)n(wer)g (256)f(characters)g(of)h(Unicode)f(are)i(the)f(same)g(as)h(the)f(256)f (characters)h(of)g(Latin-1.)0 3201 y(F)o(or)j(e)o(xperts,)g(there)g(is) i(also)e(a)h(ra)o(w)f(mode)g(just)h(lik)o(e)g(the)f(one)g(for)g(normal) f(strings.)35 b(Y)-9 b(ou)23 b(ha)n(v)o(e)g(to)g(pre\002x)g(the)g (opening)f(quote)g(with)0 3300 y('ur')e(to)h(ha)n(v)o(e)g(Python)f(use) h(the)h Fq(Raw-Unicode-Escape)17 b Fs(encoding.)26 b(It)21 b(will)h(only)f(apply)f(the)h(abo)o(v)o(e)f Fp(\\uXXXX)g Fs(con)m(v)o(ersion)f(if)i(there)0 3400 y(is)g(an)f(une)n(v)o(en)e (number)h(of)h(backslashes)f(in)i(front)e(of)h(the)g(small)h('u'.)236 3638 y Fk(>>>)45 b(ur'Hello\\u0020World)c(!')236 3729 y(u'Hello)j(World)g(!')236 3821 y(>>>)h(ur'Hello\\\\u0020World)c(!')236 3912 y(u'Hello\\\\\\\\u0020World)h(!')0 4198 y Fs(The)20 b(ra)o(w)g(mode)f(is)i(most)f(useful)g(when)g(you)f(ha)n(v)o(e)h(to)g (enter)g(lots)g(of)g(backslashes,)g(as)h(can)f(be)g(necessary)f(in)i (re)o(gular)d(e)o(xpressions.)0 4345 y(Apart)24 b(from)g(these)h (standard)e(encodings,)h(Python)f(pro)o(vides)g(a)i(whole)g(set)g(of)f (other)g(w)o(ays)h(of)g(creating)e(Unicode)h(strings)g(on)h(the)0 4445 y(basis)c(of)f(a)g(kno)n(wn)f(encoding.)0 4592 y(The)k(b)n (uilt-in)f(function)f Fp(unicode\(\))h Fs(pro)o(vides)g(access)h(to)g (all)h(re)o(gistered)e(Unicode)g(codecs)g(\(COders)h(and)f(DECoders\).) 33 b(Some)0 4691 y(of)18 b(the)h(more)f(well)h(kno)n(wn)e(encodings)g (which)h(these)g(codecs)g(can)h(con)m(v)o(ert)d(are)j Fq(Latin-1)p Fs(,)f Fq(ASCII)p Fs(,)f Fq(UTF-8)p Fs(,)i(and)f Fq(UTF-16)p Fs(.)23 b(The)18 b(latter)0 4791 y(tw)o(o)27 b(are)f(v)n(ariable-length)d(encodings)i(that)i(store)f(each)g(Unicode) f(character)g(in)i(one)f(or)g(more)f(bytes.)44 b(The)26 b(def)o(ault)f(encoding)g(is)0 4891 y(normally)20 b(set)j(to)h Fm(A)t(S)t(C)t(I)t(I)r Fs(,)g(which)d(passes)h(through)e(characters)h (in)h(the)g(range)f(0)h(to)g(127)f(and)g(rejects)h(an)o(y)g(other)f (characters)g(with)h(an)0 4990 y(error)-5 b(.)28 b(When)22 b(a)g(Unicode)e(string)h(is)i(printed,)d(written)h(to)h(a)g(\002le,)g (or)f(con)m(v)o(erted)e(with)j Fp(str\(\))p Fs(,)f(con)m(v)o(ersion)e (tak)o(es)j(place)f(using)g(this)0 5090 y(def)o(ault)e(encoding.)p 0 5549 3901 4 v 0 5649 a Fo(14)2091 b(Chapter)23 b(3.)52 b(An)24 b(Inf)n(or)r(mal)f(Introduction)h(to)f(Python)p eop end %%Page: 15 23 TeXDict begin 15 22 bop 236 83 a Fk(>>>)45 b(u"abc")236 174 y(u'abc')236 266 y(>>>)g(str\(u"abc"\))236 357 y('abc')236 448 y(>>>)g(u"\344\366\374")236 540 y(u'\\xe4\\xf6\\xfc')236 631 y(>>>)g(str\(u"\344\366\374"\))236 722 y(Traceback)f(\(most)g (recent)f(call)i(last\):)326 814 y(File)f("",)f(line)h(1,)h(in)f (?)236 905 y(UnicodeEncodeError:)e('ascii')i(codec)g(can't)f(encode)h (characters)f(in)i(position)e(0-2:)i(ordinal)e(not)i(in)f(range\(128\)) 0 1191 y Fs(T)-7 b(o)27 b(con)m(v)o(ert)e(a)i(Unicode)f(string)g(into)h (an)g(8-bit)f(string)h(using)f(a)h(speci\002c)g(encoding,)g(Unicode)e (objects)i(pro)o(vide)e(an)h Fp(encode\(\))0 1291 y Fs(method)19 b(that)h(tak)o(es)h(one)e(ar)o(gument,)f(the)i(name)g(of)g(the)g (encoding.)j(Lo)n(wercase)c(names)h(for)f(encodings)g(are)h(preferred.) 236 1529 y Fk(>>>)45 b(u"\344\366\374".encode\('utf-8'\))236 1620 y('\\xc3\\xa4\\xc3\\xb6\\xc3\\xbc')0 1906 y Fs(If)23 b(you)f(ha)n(v)o(e)g(data)h(in)g(a)g(speci\002c)g(encoding)e(and)i(w)o (ant)g(to)g(produce)e(a)i(corresponding)c(Unicode)j(string)h(from)f (it,)i(you)e(can)g(use)i(the)0 2006 y Fp(unicode\(\))19 b Fs(function)g(with)h(the)g(encoding)e(name)i(as)h(the)f(second)f(ar)o (gument.)236 2244 y Fk(>>>)45 b (unicode\('\\xc3\\xa4\\xc3\\xb6\\xc3\\xbc',)38 b('utf-8'\))236 2336 y(u'\\xe4\\xf6\\xfc')0 2758 y Fj(3.1.4)100 b(Lists)0 2961 y Fs(Python)23 b(kno)n(ws)g(a)h(number)e(of)i Fq(compound)d Fs(data)j(types,)g(used)f(to)h(group)e(together)h(other)g(v)n(alues.)35 b(The)24 b(most)g(v)o(ersatile)f(is)i(the)f Fq(list)p Fs(,)0 3060 y(which)18 b(can)g(be)g(written)g(as)g(a)h(list)g(of)f (comma-separated)e(v)n(alues)h(\(items\))h(between)g(square)f(brack)o (ets.)24 b(List)18 b(items)h(need)f(not)f(all)i(ha)n(v)o(e)0 3160 y(the)h(same)h(type.)236 3398 y Fk(>>>)45 b(a)f(=)h(['spam',)e ('eggs',)h(100,)g(1234])236 3490 y(>>>)h(a)236 3581 y(['spam',)f ('eggs',)f(100,)i(1234])0 3867 y Fs(Lik)o(e)20 b(string)g(indices,)g (list)h(indices)f(start)h(at)f(0,)g(and)g(lists)i(can)e(be)g(sliced,)g (concatenated)e(and)i(so)g(on:)236 4105 y Fk(>>>)45 b(a[0])236 4197 y('spam')236 4288 y(>>>)g(a[3])236 4379 y(1234)236 4471 y(>>>)g(a[-2])236 4562 y(100)236 4653 y(>>>)g(a[1:-1])236 4745 y(['eggs',)f(100])236 4836 y(>>>)h(a[:2])f(+)g(['bacon',)g(2)1268 4849 y(*)1313 4836 y(2])236 4927 y(['spam',)g('eggs',)f('bacon',)h(4]) 236 5019 y(>>>)h(3)461 5032 y(*)506 5019 y(a[:3])e(+)i(['Boo!'])236 5110 y(['spam',)f('eggs',)f(100,)i('spam',)e('eggs',)h(100,)g('spam',)g ('eggs',)f(100,)h('Boo!'])0 5396 y Fs(Unlik)o(e)20 b(strings,)g(which)f (are)i Fq(immutable)p Fs(,)e(it)i(is)g(possible)f(to)g(change)f(indi)n (vidual)f(elements)i(of)g(a)h(list:)p 0 5549 3901 4 v 0 5649 a Fo(3.1.)52 b(Using)23 b(Python)h(as)f(a)g(Calculator)2548 b(15)p eop end %%Page: 16 24 TeXDict begin 16 23 bop 236 174 a Fk(>>>)45 b(a)236 266 y(['spam',)f('eggs',)f(100,)i(1234])236 357 y(>>>)g(a[2])f(=)g(a[2])h (+)f(23)236 448 y(>>>)h(a)236 540 y(['spam',)f('eggs',)f(123,)i(1234])0 826 y Fs(Assignment)19 b(to)i(slices)g(is)g(also)g(possible,)e(and)h (this)h(can)f(e)n(v)o(en)f(change)g(the)h(size)h(of)f(the)g(list)h(or)f (clear)g(it)h(entirely:)236 1064 y Fk(>>>)45 b(#)f(Replace)g(some)g (items:)236 1155 y(...)h(a[0:2])e(=)i([1,)f(12])236 1247 y(>>>)h(a)236 1338 y([1,)g(12,)f(123,)g(1234])236 1429 y(>>>)h(#)f(Remove)g(some:)236 1521 y(...)h(a[0:2])e(=)i([])236 1612 y(>>>)g(a)236 1703 y([123,)f(1234])236 1795 y(>>>)h(#)f(Insert)g (some:)236 1886 y(...)h(a[1:1])e(=)i(['bletch',)e('xyzzy'])236 1977 y(>>>)i(a)236 2069 y([123,)f('bletch',)g('xyzzy',)f(1234])236 2160 y(>>>)i(#)f(Insert)g(\(a)h(copy)f(of\))g(itself)g(at)g(the)h (beginning)236 2251 y(>>>)g(a[:0])f(=)g(a)236 2343 y(>>>)h(a)236 2434 y([123,)f('bletch',)g('xyzzy',)f(1234,)h(123,)g('bletch',)f ('xyzzy',)h(1234])236 2525 y(>>>)h(#)f(Clear)g(the)h(list:)f(replace)f (all)i(items)e(with)i(an)f(empty)g(list)236 2617 y(>>>)h(a[:])f(=)g([]) 236 2708 y(>>>)h(a)236 2799 y([])0 3085 y Fs(The)20 b(b)n(uilt-in)f (function)g Fp(len\(\))h Fs(also)g(applies)g(to)h(lists:)236 3324 y Fk(>>>)45 b(len\(a\))236 3415 y(8)0 3701 y Fs(It)20 b(is)i(possible)e(to)g(nest)g(lists)i(\(create)e(lists)h(containing)d (other)i(lists\),)h(for)e(e)o(xample:)236 3939 y Fk(>>>)45 b(q)f(=)h([2,)f(3])236 4031 y(>>>)h(p)f(=)h([1,)f(q,)h(4])236 4122 y(>>>)g(len\(p\))236 4213 y(3)236 4305 y(>>>)g(p[1])236 4396 y([2,)g(3])236 4487 y(>>>)g(p[1][0])236 4579 y(2)236 4670 y(>>>)g(p[1].append\('xtra'\))221 b(#)44 b(See)h(section)e(5.1)236 4761 y(>>>)i(p)236 4852 y([1,)g([2,)f(3,)g('xtra'],)g(4])236 4944 y(>>>)h(q)236 5035 y([2,)g(3,)f('xtra'])0 5321 y Fs(Note)20 b(that)g(in)h(the)f(last)h(e)o(xample,)e Fp(p[1])h Fs(and)f Fp(q)i Fs(really)f(refer)f(to)h(the)h(same)f(object!)k(W)-7 b(e')o(ll)22 b(come)d(back)h(to)g Fq(object)g(semantics)g Fs(later)-5 b(.)p 0 5549 3901 4 v 0 5649 a Fo(16)2091 b(Chapter)23 b(3.)52 b(An)24 b(Inf)n(or)r(mal)f(Introduction)h(to)f (Python)p eop end %%Page: 17 25 TeXDict begin 17 24 bop 0 88 a Fl(3.2)121 b(First)32 b(Steps)i(T)-14 b(o)n(w)n(ards)35 b(Prog)o(r)o(amming)0 321 y Fs(Of)27 b(course,)g(we)g(can)f(use)g(Python)g(for)g(more)f (complicated)g(tasks)i(than)f(adding)f(tw)o(o)i(and)f(tw)o(o)h (together)-5 b(.)42 b(F)o(or)27 b(instance,)g(we)g(can)0 420 y(write)20 b(an)g(initial)h(sub-sequence)d(of)i(the)g Fq(F)l(ibonacci)f Fs(series)i(as)g(follo)n(ws:)236 659 y Fk(>>>)45 b(#)f(Fibonacci)g(series:)236 750 y(...)h(#)f(the)h(sum)f (of)g(two)h(elements)e(defines)h(the)g(next)236 841 y(...)h(a,)f(b)h(=) f(0,)h(1)236 933 y(>>>)g(while)f(b)g(<)h(10:)236 1024 y(...)314 b(print)44 b(b)236 1115 y(...)314 b(a,)44 b(b)h(=)f(b,)h(a+b) 236 1206 y(...)236 1298 y(1)236 1389 y(1)236 1480 y(2)236 1572 y(3)236 1663 y(5)236 1754 y(8)0 2041 y Fs(This)20 b(e)o(xample)f(introduces)g(se)n(v)o(eral)g(ne)n(w)h(features.)137 2271 y(\225)42 b(The)21 b(\002rst)i(line)f(contains)f(a)h Fq(multiple)g(assignment)p Fs(:)28 b(the)21 b(v)n(ariables)g Fp(a)i Fs(and)e Fp(b)h Fs(simultaneously)f(get)g(the)h(ne)n(w)g(v)n (alues)f(0)h(and)g(1.)208 2370 y(On)j(the)h(last)g(line)g(this)g(is)h (used)e(again,)h(demonstrating)d(that)j(the)f(e)o(xpressions)g(on)g (the)g(right-hand)e(side)j(are)g(all)g(e)n(v)n(aluated)208 2470 y(\002rst)20 b(before)f(an)o(y)g(of)g(the)h(assignments)f(tak)o(e) h(place.)25 b(The)19 b(right-hand)f(side)i(e)o(xpressions)f(are)g(e)n (v)n(aluated)g(from)g(the)g(left)i(to)f(the)208 2569 y(right.)137 2735 y(\225)42 b(The)19 b Fp(while)f Fs(loop)h(e)o(x)o (ecutes)f(as)i(long)f(as)h(the)f(condition)f(\(here:)24 b Fp(b)49 b(<)h(10)p Fs(\))19 b(remains)g(true.)24 b(In)19 b(Python,)f(lik)o(e)i(in)f(C,)h(an)o(y)f(non-)208 2835 y(zero)k(inte)o(ger)g(v)n(alue)g(is)i(true;)h(zero)e(is)h(f)o(alse.)37 b(The)23 b(condition)g(may)g(also)i(be)f(a)g(string)g(or)g(list)h(v)n (alue,)f(in)g(f)o(act)g(an)o(y)g(sequence;)208 2935 y(an)o(ything)g (with)j(a)f(non-zero)e(length)i(is)h(true,)h(empty)d(sequences)h(are)g (f)o(alse.)45 b(The)26 b(test)h(used)f(in)h(the)f(e)o(xample)f(is)j(a)f (simple)208 3034 y(comparison.)22 b(The)d(standard)f(comparison)g (operators)g(are)h(written)g(the)g(same)h(as)g(in)f(C:)i Fp(<)f Fs(\(less)g(than\),)e Fp(>)i Fs(\(greater)e(than\),)g Fp(==)208 3134 y Fs(\(equal)h(to\),)h Fp(<=)g Fs(\(less)h(than)f(or)f (equal)h(to\),)g Fp(>=)g Fs(\(greater)f(than)h(or)g(equal)f(to\))h(and) g Fp(!=)g Fs(\(not)g(equal)f(to\).)137 3300 y(\225)42 b(The)23 b Fq(body)h Fs(of)g(the)g(loop)f(is)j Fq(indented)p Fs(:)31 b(indentation)23 b(is)i(Python')-5 b(s)24 b(w)o(ay)g(of)g (grouping)d(statements.)38 b(Python)23 b(does)h(not)g(\(yet!\))208 3400 y(pro)o(vide)h(an)i(intelligent)f(input)h(line)g(editing)g(f)o (acility)-5 b(,)28 b(so)g(you)e(ha)n(v)o(e)h(to)g(type)g(a)g(tab)h(or)f (space\(s\))f(for)h(each)g(indented)f(line.)208 3499 y(In)h(practice)g(you)g(will)h(prepare)e(more)h(complicated)f(input)h (for)g(Python)f(with)i(a)g(te)o(xt)g(editor;)i(most)e(te)o(xt)f (editors)h(ha)n(v)o(e)f(an)208 3599 y(auto-indent)18 b(f)o(acility)-5 b(.)25 b(When)20 b(a)h(compound)c(statement)j(is)i (entered)d(interacti)n(v)o(ely)-5 b(,)18 b(it)j(must)f(be)h(follo)n (wed)e(by)h(a)h(blank)e(line)h(to)208 3698 y(indicate)d(completion)f (\(since)i(the)g(parser)f(cannot)g(guess)h(when)g(you)f(ha)n(v)o(e)g (typed)g(the)h(last)h(line\).)24 b(Note)18 b(that)g(each)g(line)g (within)208 3798 y(a)i(basic)h(block)e(must)h(be)g(indented)f(by)h(the) g(same)g(amount.)137 3964 y(\225)42 b(The)18 b Fp(print)h Fs(statement)f(writes)i(the)f(v)n(alue)f(of)h(the)g(e)o(xpression\(s\)) d(it)k(is)g(gi)n(v)o(en.)j(It)c(dif)n(fers)g(from)e(just)j(writing)e (the)h(e)o(xpression)208 4064 y(you)i(w)o(ant)i(to)g(write)g(\(as)g(we) g(did)f(earlier)g(in)h(the)g(calculator)e(e)o(xamples\))g(in)i(the)g(w) o(ay)g(it)g(handles)f(multiple)g(e)o(xpressions)f(and)208 4163 y(strings.)48 b(Strings)28 b(are)g(printed)f(without)h(quotes,)h (and)e(a)i(space)f(is)h(inserted)f(between)f(items,)j(so)f(you)e(can)h (format)f(things)208 4263 y(nicely)-5 b(,)19 b(lik)o(e)h(this:)444 4487 y Fk(>>>)44 b(i)h(=)f(256)937 4500 y(*)982 4487 y(256)444 4579 y(>>>)g(print)g('The)g(value)g(of)h(i)f(is',)h(i)444 4670 y(The)f(value)g(of)h(i)f(is)h(65536)208 4942 y Fs(A)20 b(trailing)g(comma)f(a)n(v)n(oids)h(the)g(ne)n(wline)g(after)g(the)g (output:)p 0 5549 3901 4 v 0 5649 a Fo(3.2.)52 b(First)23 b(Steps)g(T)-10 b(o)o(w)o(ards)22 b(Prog)o(r)o(amming)2374 b(17)p eop end %%Page: 18 26 TeXDict begin 18 25 bop 444 174 a Fk(>>>)44 b(a,)h(b)f(=)h(0,)f(1)444 266 y(>>>)g(while)g(b)h(<)f(1000:)444 357 y(...)223 b(print)44 b(b,)444 448 y(...)223 b(a,)45 b(b)g(=)f(b,)h(a+b)444 540 y(...)444 631 y(1)f(1)h(2)g(3)f(5)h(8)g(13)f(21)h(34)f(55)h(89)f (144)h(233)f(377)g(610)h(987)208 903 y Fs(Note)20 b(that)g(the)g (interpreter)f(inserts)h(a)h(ne)n(wline)e(before)g(it)i(prints)f(the)g (ne)o(xt)g(prompt)e(if)j(the)f(last)h(line)f(w)o(as)h(not)f(completed.) p 0 5549 3901 4 v 0 5649 a Fo(18)2091 b(Chapter)23 b(3.)52 b(An)24 b(Inf)n(or)r(mal)f(Introduction)h(to)f(Python)p eop end %%Page: 19 27 TeXDict begin 19 26 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3561 427 y Fn(FOUR)p 0 515 V 1621 978 a Fy(More)57 b(Control)g(Flo)m(w)g(T) -25 b(ools)0 1457 y Fs(Besides)31 b(the)f Fp(while)g Fs(statement)g(just)h(introduced,)f(Python)f(kno)n(ws)g(the)h(usual)g (control)f(\003o)n(w)h(statements)h(kno)n(wn)d(from)h(other)0 1556 y(languages,)19 b(with)h(some)g(twists.)0 1884 y Fl(4.1)121 b Ff(if)32 b Fl(Statements)0 2117 y Fs(Perhaps)20 b(the)g(most)g(well-kno)n(wn)e(statement)i(type)g(is)h(the)f Fp(if)h Fs(statement.)j(F)o(or)c(e)o(xample:)236 2355 y Fk(>>>)45 b(x)f(=)h(int\(raw_input\("Please)c(enter)j(an)h(integer:)e ("\)\))236 2446 y(>>>)i(if)f(x)h(<)f(0:)236 2537 y(...)269 b(x)44 b(=)h(0)236 2629 y(...)269 b(print)44 b('Negative)f(changed)h (to)g(zero')236 2720 y(...)h(elif)f(x)g(==)h(0:)236 2811 y(...)269 b(print)44 b('Zero')236 2903 y(...)h(elif)f(x)g(==)h(1:)236 2994 y(...)269 b(print)44 b('Single')236 3085 y(...)h(else:)236 3177 y(...)269 b(print)44 b('More')236 3268 y(...)0 3554 y Fs(There)20 b(can)h(be)g(zero)f(or)h(more)f Fp(elif)h Fs(parts,)g(and)f(the)h Fp(else)g Fs(part)g(is)h(optional.)k(The)20 b(k)o(e)o(yw)o(ord)f(`)p Fp(elif)p Fs(')i(is)g(short)g(for)f(`else)i (if)5 b(',)20 b(and)0 3654 y(is)25 b(useful)f(to)g(a)n(v)n(oid)g(e)o (xcessi)n(v)o(e)f(indentation.)36 b(An)24 b Fp(if)g Fs(.)12 b(.)g(.)38 b Fp(elif)24 b Fs(.)12 b(.)g(.)37 b Fp(elif)24 b Fs(.)12 b(.)g(.)37 b(sequence)24 b(is)h(a)f(substitute)g(for)g(the)g Fp(switch)g Fs(or)0 3753 y Fp(case)c Fs(statements)g(found)f(in)h (other)f(languages.)0 4081 y Fl(4.2)121 b Ff(for)32 b Fl(Statements)0 4314 y Fs(The)21 b Fp(for)g Fs(statement)g(in)h(Python) e(dif)n(fers)g(a)i(bit)g(from)e(what)h(you)g(may)f(be)i(used)f(to)g(in) h(C)g(or)f(P)o(ascal.)29 b(Rather)21 b(than)g(al)o(w)o(ays)g(iterating) 0 4413 y(o)o(v)o(er)j(an)i(arithmetic)e(progression)g(of)h(numbers)f (\(lik)o(e)h(in)h(P)o(ascal\),)h(or)e(gi)n(ving)f(the)h(user)h(the)f (ability)h(to)f(de\002ne)g(both)g(the)g(iteration)0 4513 y(step)19 b(and)g(halting)f(condition)f(\(as)i(C\),)h(Python')-5 b(s)18 b Fp(for)h Fs(statement)g(iterates)g(o)o(v)o(er)f(the)h(items)g (of)g(an)o(y)f(sequence)g(\(a)h(list)h(or)f(a)g(string\),)f(in)0 4613 y(the)i(order)f(that)h(the)o(y)g(appear)f(in)h(the)g(sequence.)k (F)o(or)c(e)o(xample)f(\(no)g(pun)h(intended\):)p 0 5549 3901 4 v 3808 5649 a Fo(19)p eop end %%Page: 20 28 TeXDict begin 20 27 bop 236 174 a Fk(>>>)45 b(#)f(Measure)g(some)g (strings:)236 266 y(...)h(a)f(=)h(['cat',)f('window',)f ('defenestrate'])236 357 y(>>>)i(for)f(x)h(in)f(a:)236 448 y(...)224 b(print)44 b(x,)g(len\(x\))236 540 y(...)236 631 y(cat)h(3)236 722 y(window)f(6)236 814 y(defenestrate)f(12)0 1100 y Fs(It)21 b(is)g(not)f(safe)g(to)h(modify)d(the)i(sequence)g (being)f(iterated)h(o)o(v)o(er)e(in)j(the)f(loop)g(\(this)g(can)g(only) f(happen)g(for)h(mutable)f(sequence)g(types,)0 1199 y(such)h(as)i (lists\).)27 b(If)20 b(you)g(need)g(to)g(modify)f(the)i(list)h(you)d (are)i(iterating)f(o)o(v)o(er)f(\(for)g(e)o(xample,)g(to)i(duplicate)f (selected)g(items\))h(you)f(must)0 1299 y(iterate)g(o)o(v)o(er)f(a)i (cop)o(y)-5 b(.)23 b(The)d(slice)h(notation)e(mak)o(es)h(this)h (particularly)d(con)m(v)o(enient:)236 1537 y Fk(>>>)45 b(for)f(x)h(in)f(a[:]:)g(#)h(make)f(a)h(slice)f(copy)g(of)g(the)g (entire)g(list)236 1629 y(...)179 b(if)45 b(len\(x\))e(>)i(6:)g (a.insert\(0,)e(x\))236 1720 y(...)236 1811 y(>>>)i(a)236 1903 y(['defenestrate',)d('cat',)i('window',)g('defenestrate'])0 2348 y Fl(4.3)121 b(The)34 b Ff(range\(\))d Fl(Function)0 2581 y Fs(If)18 b(you)g(do)f(need)h(to)g(iterate)h(o)o(v)o(er)e(a)h (sequence)f(of)h(numbers,)f(the)i(b)n(uilt-in)e(function)g Fp(range\(\))g Fs(comes)h(in)h(handy)-5 b(.)22 b(It)d(generates)e (lists)0 2681 y(containing)h(arithmetic)i(progressions:)236 2919 y Fk(>>>)45 b(range\(10\))236 3010 y([0,)g(1,)f(2,)h(3,)f(4,)h(5,) f(6,)h(7,)f(8,)h(9])0 3296 y Fs(The)26 b(gi)n(v)o(en)e(end)i(point)f (is)i(ne)n(v)o(er)e(part)g(of)h(the)g(generated)f(list;)30 b Fp(range\(10\))25 b Fs(generates)g(a)h(list)h(of)f(10)g(v)n(alues,)g (the)g(le)o(gal)g(indices)0 3396 y(for)e(items)i(of)f(a)g(sequence)f (of)h(length)f(10.)39 b(It)25 b(is)h(possible)f(to)g(let)g(the)g(range) f(start)i(at)f(another)f(number)m(,)g(or)g(to)h(specify)g(a)g(dif)n (ferent)0 3496 y(increment)19 b(\(e)n(v)o(en)g(ne)o(gati)n(v)o(e;)f (sometimes)i(this)g(is)h(called)f(the)h(`step'\):)236 3734 y Fk(>>>)45 b(range\(5,)e(10\))236 3825 y([5,)i(6,)f(7,)h(8,)f(9]) 236 3917 y(>>>)h(range\(0,)e(10,)h(3\))236 4008 y([0,)h(3,)f(6,)h(9]) 236 4099 y(>>>)g(range\(-10,)e(-100,)h(-30\))236 4191 y([-10,)g(-40,)g(-70])0 4477 y Fs(T)-7 b(o)20 b(iterate)h(o)o(v)o(er)d (the)j(indices)f(of)f(a)i(sequence,)e(combine)g Fp(range\(\))g Fs(and)h Fp(len\(\))f Fs(as)i(follo)n(ws:)p 0 5549 3901 4 v 0 5649 a Fo(20)2453 b(Chapter)23 b(4.)52 b(More)23 b(Control)h(Flo)o(w)f(T)-10 b(ools)p eop end %%Page: 21 29 TeXDict begin 21 28 bop 236 174 a Fk(>>>)45 b(a)f(=)h(['Mary',)e ('had',)h('a',)g('little',)g('lamb'])236 266 y(>>>)h(for)f(i)h(in)f (range\(len\(a\)\):)236 357 y(...)224 b(print)44 b(i,)g(a[i])236 448 y(...)236 540 y(0)h(Mary)236 631 y(1)g(had)236 722 y(2)g(a)236 814 y(3)g(little)236 905 y(4)g(lamb)0 1354 y Fl(4.4)121 b Ff(break)32 b Fl(and)i Ff(continue)d Fl(Statements)n(,) 36 b(and)e Ff(else)f Fl(Clauses)h(on)g(Loops)0 1586 y Fs(The)20 b Fp(break)g Fs(statement,)f(lik)o(e)i(in)f(C,)h(breaks)f (out)f(of)h(the)h(smallest)f(enclosing)f Fp(for)i Fs(or)e Fp(while)h Fs(loop.)0 1733 y(The)g Fp(continue)f Fs(statement,)h(also)g (borro)n(wed)e(from)h(C,)i(continues)e(with)i(the)f(ne)o(xt)f (iteration)h(of)g(the)g(loop.)0 1880 y(Loop)c(statements)h(may)g(ha)n (v)o(e)f(an)h Fp(else)g Fs(clause;)h(it)g(is)g(e)o(x)o(ecuted)d(when)i (the)g(loop)f(terminates)h(through)e(e)o(xhaustion)g(of)h(the)i(list)g (\(with)0 1980 y Fp(for)p Fs(\))h(or)g(when)g(the)h(condition)e (becomes)g(f)o(alse)i(\(with)g Fp(while)p Fs(\),)e(b)n(ut)i(not)f(when) g(the)g(loop)g(is)i(terminated)d(by)h(a)h Fp(break)f Fs(statement.)0 2079 y(This)h(is)h(e)o(x)o(empli\002ed)e(by)h(the)g (follo)n(wing)e(loop,)h(which)h(searches)g(for)g(prime)f(numbers:)236 2318 y Fk(>>>)45 b(for)f(n)h(in)f(range\(2,)g(10\):)236 2409 y(...)224 b(for)44 b(x)h(in)f(range\(2,)g(n\):)236 2500 y(...)403 b(if)45 b(n)f(\045)h(x)g(==)f(0:)236 2591 y(...)583 b(print)44 b(n,)g('equals',)f(x,)i(')1985 2604 y(*)2030 2591 y(',)f(n/x)236 2683 y(...)583 b(break)236 2774 y(...)224 b(else:)236 2865 y(...)403 b(#)45 b(loop)f(fell)g (through)g(without)f(finding)h(a)h(factor)236 2957 y(...)403 b(print)44 b(n,)h('is)f(a)h(prime)f(number')236 3048 y(...)236 3139 y(2)h(is)f(a)h(prime)f(number)236 3231 y(3)h(is)f(a)h(prime)f(number)236 3322 y(4)h(equals)f(2)729 3335 y(*)819 3322 y(2)236 3413 y(5)h(is)f(a)h(prime)f(number)236 3505 y(6)h(equals)f(2)729 3518 y(*)819 3505 y(3)236 3596 y(7)h(is)f(a)h(prime)f(number)236 3687 y(8)h(equals)f(2)729 3700 y(*)819 3687 y(4)236 3779 y(9)h(equals)f(3)729 3792 y(*)819 3779 y(3)0 4227 y Fl(4.5)121 b Ff(pass)32 b Fl(Statements)0 4460 y Fs(The)20 b Fp(pass)h Fs(statement)f(does)h(nothing.)k(It)c(can) f(be)h(used)f(when)g(a)i(statement)e(is)i(required)d(syntactically)h(b) n(ut)g(the)h(program)e(requires)0 4560 y(no)h(action.)k(F)o(or)c(e)o (xample:)236 4798 y Fk(>>>)45 b(while)f(True:)236 4889 y(...)314 b(pass)44 b(#)g(Busy-wait)g(for)g(keyboard)f(interrupt)236 4981 y(...)p 0 5549 3901 4 v 0 5649 a Fo(4.4.)52 b Fp(break)22 b Fo(and)i Fp(continue)d Fo(Statements)o(,)j(and)g Fp(else)e Fo(Clauses)h(on)g(Loops)1250 b(21)p eop end %%Page: 22 30 TeXDict begin 22 29 bop 0 88 a Fl(4.6)121 b(De\002ning)34 b(Functions)0 321 y Fs(W)-7 b(e)21 b(can)f(create)g(a)h(function)d (that)j(writes)f(the)g(Fibonacci)g(series)g(to)h(an)f(arbitrary)f (boundary:)236 559 y Fk(>>>)45 b(def)f(fib\(n\):)178 b(#)45 b(write)f(Fibonacci)f(series)h(up)g(to)h(n)236 650 y(...)224 b("""Print)43 b(a)i(Fibonacci)e(series)h(up)h(to)f(n.""") 236 742 y(...)224 b(a,)44 b(b)h(=)g(0,)f(1)236 833 y(...)224 b(while)44 b(b)h(<)f(n:)236 924 y(...)403 b(print)44 b(b,)236 1016 y(...)403 b(a,)45 b(b)f(=)h(b,)g(a+b)236 1107 y(...)236 1198 y(>>>)g(#)f(Now)h(call)f(the)g(function)g(we)g (just)g(defined:)236 1290 y(...)h(fib\(2000\))236 1381 y(1)g(1)g(2)f(3)h(5)g(8)f(13)h(21)f(34)h(55)f(89)h(144)f(233)g(377)h (610)f(987)g(1597)0 1667 y Fs(The)17 b(k)o(e)o(yw)o(ord)f Fp(def)i Fs(introduces)e(a)i(function)e Fq(de\002nition)p Fs(.)23 b(It)18 b(must)g(be)f(follo)n(wed)g(by)g(the)h(function)e(name) h(and)g(the)h(parenthesized)d(list)0 1767 y(of)g(formal)g(parameters.) 22 b(The)15 b(statements)h(that)g(form)e(the)i(body)e(of)i(the)f (function)f(start)i(at)h(the)e(ne)o(xt)g(line,)i(and)e(must)g(be)h (indented.)22 b(The)0 1866 y(\002rst)f(statement)g(of)f(the)h(function) d(body)i(can)g(optionally)f(be)h(a)i(string)e(literal;)h(this)g(string) f(literal)h(is)h(the)e(function')-5 b(s)20 b(documentation)0 1966 y(string,)g(or)g Fq(docstring)p Fs(.)0 2113 y(There)29 b(are)h(tools)g(which)g(use)g(docstrings)f(to)h(automatically)e (produce)g(online)h(or)h(printed)f(documentation,)g(or)h(to)g(let)h (the)f(user)0 2212 y(interacti)n(v)o(ely)25 b(bro)n(wse)h(through)f (code;)k(it')-5 b(s)28 b(good)d(practice)h(to)h(include)f(docstrings)f (in)i(code)f(that)h(you)f(write,)i(so)f(try)g(to)g(mak)o(e)f(a)0 2312 y(habit)20 b(of)g(it.)0 2459 y(The)e Fq(e)n(xecution)g Fs(of)h(a)g(function)e(introduces)h(a)h(ne)n(w)f(symbol)g(table)h(used) g(for)f(the)h(local)g(v)n(ariables)f(of)g(the)h(function.)k(More)18 b(precisely)-5 b(,)0 2559 y(all)18 b(v)n(ariable)f(assignments)h(in)g (a)g(function)e(store)i(the)g(v)n(alue)f(in)h(the)g(local)g(symbol)e (table;)j(whereas)f(v)n(ariable)e(references)h(\002rst)h(look)f(in)0 2658 y(the)k(local)g(symbol)f(table,)h(then)g(in)g(the)g(global)f (symbol)h(table,)g(and)f(then)h(in)g(the)g(table)g(of)g(b)n(uilt-in)f (names.)28 b(Thus,)20 b(global)g(v)n(ariables)0 2758 y(cannot)j(be)h(directly)f(assigned)h(a)g(v)n(alue)f(within)h(a)h (function)d(\(unless)i(named)e(in)j(a)f Fp(global)g Fs(statement\),)g (although)e(the)o(y)h(may)h(be)0 2857 y(referenced.)0 3004 y(The)i(actual)f(parameters)g(\(ar)o(guments\))e(to)j(a)g (function)e(call)j(are)e(introduced)f(in)i(the)g(local)f(symbol)g (table)h(of)g(the)g(called)f(function)0 3104 y(when)e(it)i(is)g (called;)h(thus,)f(ar)o(guments)d(are)i(passed)g(using)g Fq(call)g(by)g(value)f Fs(\(where)g(the)i Fq(value)e Fs(is)i(al)o(w)o(ays)g(an)f(object)f Fq(r)m(efer)m(ence)p Fs(,)i(not)0 3204 y(the)20 b(v)n(alue)g(of)g(the)g(object\).)786 3173 y Fi(1)843 3204 y Fs(When)g(a)h(function)d(calls)j(another)e (function,)f(a)j(ne)n(w)f(local)g(symbol)f(table)h(is)h(created)f(for)f (that)i(call.)0 3350 y(A)h(function)e(de\002nition)h(introduces)f(the)i (function)e(name)h(in)h(the)g(current)e(symbol)h(table.)29 b(The)22 b(v)n(alue)f(of)g(the)h(function)e(name)h(has)h(a)0 3450 y(type)c(that)h(is)g(recognized)e(by)h(the)g(interpreter)f(as)i(a) g(user)n(-de\002ned)e(function.)23 b(This)c(v)n(alue)e(can)i(be)f (assigned)g(to)h(another)e(name)h(which)0 3550 y(can)i(then)g(also)g (be)g(used)g(as)h(a)g(function.)i(This)d(serv)o(es)g(as)h(a)g(general)e (renaming)f(mechanism:)236 3788 y Fk(>>>)45 b(fib)236 3879 y()236 3970 y(>>>)h(f)f(=)h(fib) 236 4062 y(>>>)g(f\(100\))236 4153 y(1)g(1)g(2)f(3)h(5)g(8)f(13)h(21)f (34)h(55)f(89)0 4439 y Fs(Y)-9 b(ou)23 b(might)g(object)g(that)g Fp(fib)h Fs(is)g(not)f(a)h(function)e(b)n(ut)i(a)g(procedure.)32 b(In)23 b(Python,)g(lik)o(e)h(in)f(C,)i(procedures)c(are)i(just)h (functions)e(that)0 4539 y(don')o(t)d(return)h(a)h(v)n(alue.)26 b(In)21 b(f)o(act,)g(technically)f(speaking,)f(procedures)g(do)h (return)g(a)h(v)n(alue,)f(albeit)h(a)h(rather)e(boring)f(one.)26 b(This)21 b(v)n(alue)0 4639 y(is)h(called)f Fp(None)g Fs(\(it')-5 b(s)22 b(a)g(b)n(uilt-in)e(name\).)27 b(Writing)21 b(the)g(v)n(alue)g Fp(None)g Fs(is)h(normally)d(suppressed)h(by)h(the)g (interpreter)f(if)h(it)h(w)o(ould)f(be)0 4738 y(the)f(only)g(v)n(alue)f (written.)25 b(Y)-9 b(ou)19 b(can)h(see)h(it)g(if)g(you)e(really)h(w)o (ant)g(to:)p 0 4809 1560 4 v 90 4865 a Fh(1)120 4888 y Fm(Actually)l(,)i Fe(call)f(by)f(object)h(r)n(efer)n(ence)g Fm(w)o(ould)g(be)f(a)g(better)h(description,)i(since)e(if)f(a)g (mutable)h(object)h(is)d(passed,)i(the)g(caller)g(will)g(see)f(an)o(y)g (changes)i(the)e(callee)0 4967 y(mak)o(es)e(to)f(it)g(\(items)h (inserted)h(into)f(a)f(list\).)p 0 5549 3901 4 v 0 5649 a Fo(22)2453 b(Chapter)23 b(4.)52 b(More)23 b(Control)h(Flo)o(w)f(T)-10 b(ools)p eop end %%Page: 23 31 TeXDict begin 23 30 bop 236 174 a Fk(>>>)45 b(print)f(fib\(0\))236 266 y(None)0 552 y Fs(It)20 b(is)i(simple)e(to)g(write)g(a)h(function)e (that)h(returns)f(a)i(list)g(of)f(the)g(numbers)f(of)h(the)g(Fibonacci) f(series,)i(instead)f(of)g(printing)e(it:)236 790 y Fk(>>>)45 b(def)f(fib2\(n\):)f(#)i(return)f(Fibonacci)f(series)h(up)g(to)h(n)236 881 y(...)224 b("""Return)43 b(a)i(list)f(containing)f(the)h(Fibonacci) g(series)g(up)g(to)h(n.""")236 973 y(...)224 b(result)44 b(=)g([])236 1064 y(...)224 b(a,)44 b(b)h(=)g(0,)f(1)236 1155 y(...)224 b(while)44 b(b)h(<)f(n:)236 1247 y(...)403 b(result.append\(b\))177 b(#)45 b(see)f(below)236 1338 y(...)403 b(a,)45 b(b)f(=)h(b,)g(a+b)236 1429 y(...)224 b(return)44 b(result)236 1521 y(...)236 1612 y(>>>)h(f100)f(=)g (fib2\(100\))178 b(#)45 b(call)f(it)236 1703 y(>>>)h(f100)716 b(#)45 b(write)f(the)g(result)236 1795 y([1,)h(1,)f(2,)h(3,)f(5,)h(8,)f (13,)g(21,)h(34,)f(55,)g(89])0 2081 y Fs(This)20 b(e)o(xample,)f(as)i (usual,)f(demonstrates)e(some)i(ne)n(w)g(Python)f(features:)137 2293 y(\225)42 b(The)20 b Fp(return)h Fs(statement)f(returns)h(with)g (a)g(v)n(alue)g(from)f(a)h(function.)26 b Fp(return)20 b Fs(without)g(an)h(e)o(xpression)f(ar)o(gument)e(returns)208 2392 y Fp(None)p Fs(.)24 b(F)o(alling)c(of)n(f)g(the)g(end)f(of)h(a)h (procedure)d(also)i(returns)f Fp(None)p Fs(.)137 2551 y(\225)42 b(The)24 b(statement)g Fp(result.append\(b\))f Fs(calls)i(a)g Fq(method)f Fs(of)g(the)h(list)g(object)g Fp(result)p Fs(.)37 b(A)26 b(method)d(is)i(a)h(function)c(that)208 2651 y(`belongs')14 b(to)i(an)g(object)f(and)h(is)h(named)e Fp(obj.methodname)p Fs(,)f(where)i Fp(obj)g Fs(is)h(some)f(object)f (\(this)h(may)g(be)g(an)g(e)o(xpression\),)208 2750 y(and)k Fp(methodname)f Fs(is)j(the)e(name)g(of)h(a)g(method)e(that)i(is)g (de\002ned)f(by)g(the)h(object')-5 b(s)20 b(type.)26 b(Dif)n(ferent)20 b(types)g(de\002ne)g(dif)n(ferent)208 2850 y(methods.)i(Methods)15 b(of)g(dif)n(ferent)f(types)h(may)g(ha)n (v)o(e)g(the)h(same)f(name)g(without)g(causing)g(ambiguity)-5 b(.)21 b(\(It)16 b(is)g(possible)f(to)h(de\002ne)208 2950 y(your)21 b(o)n(wn)h(object)g(types)g(and)g(methods,)f(using)h Fq(classes)p Fs(,)i(as)f(discussed)f(later)h(in)g(this)g(tutorial.\))30 b(The)22 b(method)f Fp(append\(\))208 3049 y Fs(sho)n(wn)e(in)h(the)g (e)o(xample)e(is)j(de\002ned)e(for)g(list)i(objects;)f(it)h(adds)e(a)h (ne)n(w)g(element)f(at)i(the)f(end)f(of)h(the)g(list.)26 b(In)19 b(this)i(e)o(xample)d(it)j(is)208 3149 y(equi)n(v)n(alent)d(to) i(`)p Fp(result)49 b(=)g(result)g(+)h([b])p Fs(',)19 b(b)n(ut)h(more)g(ef)n(\002cient.)0 3473 y Fl(4.7)121 b(More)33 b(on)h(De\002ning)h(Functions)0 3706 y Fs(It)30 b(is)h(also)f(possible)f(to)h(de\002ne)f(functions)g(with)g(a)i(v)n (ariable)d(number)g(of)i(ar)o(guments.)51 b(There)29 b(are)h(three)f(forms,)i(which)e(can)h(be)0 3806 y(combined.)0 4087 y Fj(4.7.1)100 b(Def)m(ault)29 b(Argument)g(V)-7 b(alues)0 4290 y Fs(The)18 b(most)f(useful)h(form)e(is)j(to)f(specify)f (a)i(def)o(ault)e(v)n(alue)g(for)g(one)g(or)h(more)f(ar)o(guments.)22 b(This)c(creates)g(a)g(function)e(that)i(can)g(be)g(called)0 4390 y(with)i(fe)n(wer)g(ar)o(guments)e(than)i(it)h(is)g(de\002ned)e (to)h(allo)n(w)-5 b(.)25 b(F)o(or)20 b(e)o(xample:)236 4628 y Fk(def)45 b(ask_ok\(prompt,)d(retries=4,)h(complaint='Yes)g(or)h (no,)g(please!'\):)416 4719 y(while)g(True:)595 4811 y(ok)g(=)h(raw_input\(prompt\))595 4902 y(if)f(ok)h(in)f(\('y',)g ('ye',)g('yes'\):)g(return)g(True)595 4993 y(if)g(ok)h(in)f(\('n',)g ('no',)g('nop',)g('nope'\):)g(return)f(False)595 5085 y(retries)h(=)g(retries)g(-)h(1)595 5176 y(if)f(retries)g(<)h(0:)f (raise)g(IOError,)g('refusenik)f(user')595 5267 y(print)h(complaint)p 0 5549 3901 4 v 0 5649 a Fo(4.7.)52 b(More)23 b(on)h(De\002ning)f (Functions)2603 b(23)p eop end %%Page: 24 32 TeXDict begin 24 31 bop 0 83 a Fs(This)35 b(function)f(can)h(be)g (called)g(either)g(lik)o(e)h(this:)55 b Fp(ask_ok\('Do)48 b(you)i(really)e(want)h(to)h(quit?'\))69 b Fs(or)35 b(lik)o(e)h(this:)0 183 y Fp(ask_ok\('OK)48 b(to)h(overwrite)g(the)g(file?',)f(2\))p Fs(.)0 330 y(This)20 b(e)o(xample)f(also)i(introduces)d(the)i Fp(in)h Fs(k)o(e)o(yw)o(ord.)i(This)d(tests)i(whether)d(or)h(not)g(a)g (sequence)f(contains)h(a)g(certain)g(v)n(alue.)0 476 y(The)g(def)o(ault)f(v)n(alues)h(are)g(e)n(v)n(aluated)f(at)i(the)f (point)f(of)h(function)f(de\002nition)g(in)h(the)g Fq(de\002ning)f Fs(scope,)g(so)i(that)236 715 y Fk(i)45 b(=)g(5)236 897 y(def)g(f\(arg=i\):)416 989 y(print)f(arg)236 1171 y(i)h(=)g(6)236 1263 y(f\(\))0 1549 y Fs(will)21 b(print)f Fp(5)p Fs(.)0 1696 y Fr(Important)f(war)o(ning:)25 b Fs(The)19 b(def)o(ault)g(v)n (alue)g(is)i(e)n(v)n(aluated)d(only)h(once.)25 b(This)20 b(mak)o(es)f(a)h(dif)n(ference)e(when)i(the)f(def)o(ault)g(is)i(a)g (mutable)0 1795 y(object)26 b(such)g(as)i(a)f(list,)i(dictionary)-5 b(,)25 b(or)i(instances)f(of)g(most)h(classes.)45 b(F)o(or)26 b(e)o(xample,)h(the)f(follo)n(wing)f(function)g(accumulates)h(the)0 1895 y(ar)o(guments)18 b(passed)i(to)h(it)f(on)g(subsequent)f(calls:) 236 2042 y Fk(def)45 b(f\(a,)f(L=[]\):)416 2133 y(L.append\(a\))416 2224 y(return)f(L)236 2407 y(print)h(f\(1\))236 2498 y(print)g(f\(2\))236 2590 y(print)g(f\(3\))0 2876 y Fs(This)20 b(will)h(print)236 3023 y Fk([1])236 3114 y([1,)45 b(2])236 3205 y([1,)g(2,)f(3])0 3492 y Fs(If)20 b(you)f(don')o(t)g(w)o(ant)h (the)g(def)o(ault)g(to)g(be)g(shared)g(between)f(subsequent)g(calls,)i (you)e(can)h(write)g(the)g(function)f(lik)o(e)h(this)h(instead:)236 3638 y Fk(def)45 b(f\(a,)f(L=None\):)416 3730 y(if)g(L)h(is)f(None:)595 3821 y(L)h(=)f([])416 3912 y(L.append\(a\))416 4004 y(return)f(L)0 4426 y Fj(4.7.2)100 b(K)l(e)n(yw)o(ord)29 b(Arguments)0 4629 y Fs(Functions)24 b(can)g(also)h(be)f(called)h(using)f(k)o(e)o(yw) o(ord)e(ar)o(guments)h(of)h(the)h(form)e(`)p Fq(k)o(e)n(ywor)m(d)51 b Fp(=)e Fq(value)p Fs('.)37 b(F)o(or)25 b(instance,)g(the)f(follo)n (wing)0 4729 y(function:)p 0 5549 3901 4 v 0 5649 a Fo(24)2453 b(Chapter)23 b(4.)52 b(More)23 b(Control)h(Flo)o(w)f(T)-10 b(ools)p eop end %%Page: 25 33 TeXDict begin 25 32 bop 236 174 a Fk(def)45 b(parrot\(voltage,)d (state='a)h(stiff',)h(action='voom',)e(type='Norwegian)h(Blue'\):)416 266 y(print)h("--)g(This)g(parrot)g(wouldn't",)f(action,)416 357 y(print)h("if)g(you)g(put",)g(voltage,)g("volts)f(through)h(it.") 416 448 y(print)g("--)g(Lovely)g(plumage,)f(the",)h(type)416 540 y(print)g("--)g(It's",)g(state,)g("!")0 826 y Fs(could)19 b(be)h(called)g(in)h(an)o(y)e(of)h(the)g(follo)n(wing)f(w)o(ays:)236 1064 y Fk(parrot\(1000\))236 1155 y(parrot\(action)43 b(=)i('VOOOOOM',)e(voltage)g(=)i(1000000\))236 1247 y(parrot\('a)f (thousand',)f(state)h(=)g('pushing)g(up)g(the)h(daisies'\))236 1338 y(parrot\('a)f(million',)f('bereft)h(of)g(life',)g('jump'\))0 1624 y Fs(b)n(ut)20 b(the)g(follo)n(wing)f(calls)i(w)o(ould)e(all)i(be) f(in)m(v)n(alid:)236 1862 y Fk(parrot\(\))940 b(#)45 b(required)e(argument)h(missing)236 1954 y(parrot\(voltage=5.0,)e ('dead'\))88 b(#)45 b(non-keyword)e(argument)g(following)h(keyword)236 2045 y(parrot\(110,)f(voltage=220\))222 b(#)45 b(duplicate)e(value)h (for)h(argument)236 2136 y(parrot\(actor='John)d(Cleese'\))88 b(#)45 b(unknown)f(keyword)0 2423 y Fs(In)31 b(general,)i(an)e(ar)o (gument)e(list)k(must)e(ha)n(v)o(e)g(an)o(y)f(positional)h(ar)o (guments)e(follo)n(wed)h(by)h(an)o(y)f(k)o(e)o(yw)o(ord)g(ar)o (guments,)i(where)f(the)0 2522 y(k)o(e)o(yw)o(ords)c(must)i(be)g (chosen)f(from)f(the)i(formal)f(parameter)f(names.)50 b(It')-5 b(s)30 b(not)e(important)f(whether)h(a)h(formal)f(parameter)f (has)i(a)0 2622 y(def)o(ault)22 b(v)n(alue)g(or)h(not.)32 b(No)23 b(ar)o(gument)d(may)j(recei)n(v)o(e)e(a)j(v)n(alue)e(more)g (than)g(once)g(\227)i(formal)d(parameter)h(names)g(corresponding)d(to)0 2721 y(positional)f(ar)o(guments)e(cannot)i(be)h(used)f(as)h(k)o(e)o (yw)o(ords)f(in)g(the)h(same)g(calls.)25 b(Here')-5 b(s)19 b(an)g(e)o(xample)e(that)i(f)o(ails)g(due)f(to)h(this)g(restriction:) 236 2960 y Fk(>>>)45 b(def)f(function\(a\):)236 3051 y(...)224 b(pass)236 3142 y(...)236 3234 y(>>>)45 b(function\(0,)e (a=0\))236 3325 y(Traceback)h(\(most)g(recent)f(call)i(last\):)326 3416 y(File)f("",)f(line)h(1,)h(in)f(?)236 3508 y(TypeError:)f(function\(\))h(got)g(multiple)f(values)h(for)g(keyword)g (argument)g('a')0 3794 y Fs(When)24 b(a)h(\002nal)g(formal)f(parameter) f(of)h(the)h(form)1469 3809 y Fp(**)1569 3794 y Fq(name)f Fs(is)h(present,)g(it)g(recei)n(v)o(es)f(a)h(dictionary)e(containing)f (all)k(k)o(e)o(yw)o(ord)c(ar)o(gu-)0 3893 y(ments)i(e)o(xcept)g(for)g (those)g(corresponding)d(to)j(a)h(formal)f(parameter)-5 b(.)36 b(This)25 b(may)f(be)g(combined)f(with)h(a)h(formal)e(parameter) g(of)i(the)0 3993 y(form)182 4008 y Fp(*)232 3993 y Fq(name)20 b Fs(\(described)e(in)j(the)f(ne)o(xt)g(subsection\))f(which)h(recei)n (v)o(es)f(a)i(tuple)f(containing)e(the)j(positional)e(ar)o(guments)f (be)o(yond)h(the)0 4093 y(formal)g(parameter)g(list.)26 b(\()771 4108 y Fp(*)821 4093 y Fq(name)19 b Fs(must)h(occur)f(before) 1641 4108 y Fp(**)1741 4093 y Fq(name)p Fs(.\))24 b(F)o(or)19 b(e)o(xample,)g(if)h(we)h(de\002ne)f(a)g(function)f(lik)o(e)h(this:)236 4239 y Fk(def)45 b(cheeseshop\(kind,)1178 4252 y(*)1223 4239 y(arguments,)1716 4252 y(**)1806 4239 y(keywords\):)416 4331 y(print)f("--)g(Do)g(you)h(have)f(any",)g(kind,)g('?')416 4422 y(print)g("--)g(I'm)g(sorry,)g(we're)g(all)g(out)h(of",)f(kind)416 4513 y(for)g(arg)g(in)h(arguments:)e(print)h(arg)416 4605 y(print)g('-')820 4618 y(*)865 4605 y(40)416 4696 y(keys)g(=)g(keywords.keys\(\))416 4787 y(keys.sort\(\))416 4879 y(for)g(kw)g(in)h(keys:)f(print)g(kw,)g(':',)g(keywords[kw])0 5165 y Fs(It)20 b(could)g(be)g(called)g(lik)o(e)g(this:)p 0 5549 3901 4 v 0 5649 a Fo(4.7.)52 b(More)23 b(on)h(De\002ning)f (Functions)2603 b(25)p eop end %%Page: 26 34 TeXDict begin 26 33 bop 236 83 a Fk(cheeseshop\('Limburger',)41 b("It's)j(very)g(runny,)g(sir.",)729 174 y("It's)g(really)g(very,)g (VERY)g(runny,)g(sir.",)729 266 y(client='John)f(Cleese',)729 357 y(shopkeeper='Michael)f(Palin',)729 448 y(sketch='Cheese)h(Shop)h (Sketch'\))0 734 y Fs(and)20 b(of)g(course)f(it)i(w)o(ould)e(print:)236 881 y Fk(--)45 b(Do)f(you)h(have)f(any)g(Limburger)f(?)236 973 y(--)i(I'm)f(sorry,)g(we're)g(all)g(out)h(of)f(Limburger)236 1064 y(It's)g(very)h(runny,)e(sir.)236 1155 y(It's)h(really)g(very,)g (VERY)g(runny,)g(sir.)236 1247 y (----------------------------------------)236 1338 y(client)g(:)h(John) f(Cleese)236 1429 y(shopkeeper)f(:)i(Michael)f(Palin)236 1521 y(sketch)g(:)h(Cheese)f(Shop)g(Sketch)0 1807 y Fs(Note)28 b(that)f(the)h Fp(sort\(\))f Fs(method)f(of)h(the)h(list)g(of)g(k)o(e)o (yw)o(ord)d(ar)o(gument)h(names)h(is)h(called)g(before)e(printing)g (the)h(contents)g(of)h(the)0 1906 y Fp(keywords)19 b Fs(dictionary;)g(if)h(this)h(is)g(not)f(done,)f(the)h(order)f(in)h (which)g(the)g(ar)o(guments)e(are)i(printed)f(is)j(unde\002ned.)0 2191 y Fj(4.7.3)100 b(Arbitr)o(ar)s(y)29 b(Argument)h(Lists)0 2394 y Fs(Finally)-5 b(,)24 b(the)g(least)h(frequently)d(used)h(option) g(is)i(to)f(specify)g(that)g(a)g(function)e(can)i(be)g(called)g(with)g (an)g(arbitrary)e(number)h(of)g(ar)o(gu-)0 2494 y(ments.)h(These)19 b(ar)o(guments)d(will)j(be)g(wrapped)e(up)h(in)h(a)f(tuple.)24 b(Before)18 b(the)h(v)n(ariable)e(number)g(of)h(ar)o(guments,)f(zero)h (or)g(more)g(normal)0 2594 y(ar)o(guments)g(may)i(occur)-5 b(.)236 2832 y Fk(def)45 b(fprintf\(file,)d(format,)1402 2845 y(*)1447 2832 y(args\):)416 2923 y(file.write\(format)g(\045)i (args\))0 3345 y Fj(4.7.4)100 b(Unpac)n(king)30 b(Argument)f(Lists)0 3548 y Fs(The)22 b(re)n(v)o(erse)g(situation)h(occurs)f(when)g(the)h (ar)o(guments)d(are)j(already)f(in)h(a)g(list)h(or)e(tuple)h(b)n(ut)f (need)h(to)g(be)f(unpack)o(ed)f(for)h(a)h(function)0 3648 y(call)g(requiring)e(separate)i(positional)f(ar)o(guments.)31 b(F)o(or)23 b(instance,)g(the)g(b)n(uilt-in)f Fp(range\(\))h Fs(function)e(e)o(xpects)h(separate)h Fq(start)i Fs(and)0 3747 y Fq(stop)17 b Fs(ar)o(guments.)22 b(If)16 b(the)o(y)h(are)f(not)h (a)n(v)n(ailable)f(separately)-5 b(,)16 b(write)h(the)g(function)f (call)h(with)g(the)2726 3762 y Fp(*)2776 3747 y Fs(-operator)d(to)j (unpack)f(the)h(ar)o(guments)0 3847 y(out)j(of)g(a)g(list)i(or)e (tuple:)236 4085 y Fk(>>>)45 b(range\(3,)e(6\))583 b(#)44 b(normal)g(call)g(with)g(separate)g(arguments)236 4177 y([3,)h(4,)f(5])236 4268 y(>>>)h(args)f(=)g([3,)h(6])236 4359 y(>>>)g(range\()686 4372 y(*)731 4359 y(args\))536 b(#)44 b(call)g(with)g(arguments)g(unpacked)f(from)h(a)h(list)236 4451 y([3,)g(4,)f(5])0 4737 y Fs(In)20 b(the)g(same)g(f)o(ashion,)g (dictionaries)f(can)h(deli)n(v)o(er)f(k)o(e)o(yw)o(ord)f(ar)o(guments)g (with)j(the)2445 4752 y Fp(**)2545 4737 y Fs(-operator:)p 0 5549 3901 4 v 0 5649 a Fo(26)2453 b(Chapter)23 b(4.)52 b(More)23 b(Control)h(Flo)o(w)f(T)-10 b(ools)p eop end %%Page: 27 35 TeXDict begin 27 34 bop 236 174 a Fk(>>>)45 b(def)f(parrot\(voltage,)e (state='a)i(stiff',)f(action='voom'\):)236 266 y(...)224 b(print)44 b("--)g(This)g(parrot)g(wouldn't",)f(action,)236 357 y(...)224 b(print)44 b("if)g(you)h(put",)f(voltage,)f("volts)h (through)f(it.",)236 448 y(...)224 b(print)44 b("E's",)g(state,)g("!") 236 540 y(...)236 631 y(>>>)h(d)f(=)h({"voltage":)e("four)h(million",)f ("state":)h("bleedin')f(demised",)g("action":)h("VOOM"})236 722 y(>>>)h(parrot\()731 735 y(**)821 722 y(d\))236 814 y(--)g(This)f(parrot)g(wouldn't)f(VOOM)h(if)h(you)f(put)g(four)h (million)e(volts)h(through)g(it.)g(E's)h(bleedin')e(demised)h(!)0 1236 y Fj(4.7.5)100 b(Lambda)30 b(F)m(or)r(ms)0 1439 y Fs(By)20 b(popular)f(demand,)f(a)i(fe)n(w)g(features)g(commonly)e (found)g(in)i(functional)e(programming)f(languages)h(lik)o(e)j(Lisp)f (ha)n(v)o(e)f(been)h(added)0 1538 y(to)28 b(Python.)47 b(W)m(ith)28 b(the)g Fp(lambda)f Fs(k)o(e)o(yw)o(ord,)h(small)g(anon)o (ymous)e(functions)g(can)i(be)g(created.)47 b(Here')-5 b(s)28 b(a)g(function)e(that)i(returns)0 1638 y(the)23 b(sum)g(of)f(its)i(tw)o(o)f(ar)o(guments:)28 b(`)p Fp(lambda)49 b(a,)g(b:)99 b(a+b)p Fs('.)33 b(Lambda)21 b(forms)h(can)h(be)g(used)f (where)n(v)o(er)f(function)g(objects)i(are)0 1738 y(required.)48 b(The)o(y)27 b(are)i(syntactically)e(restricted)h(to)h(a)g(single)f(e)o (xpression.)48 b(Semantically)-5 b(,)29 b(the)o(y)f(are)g(just)h (syntactic)f(sugar)f(for)h(a)0 1837 y(normal)17 b(function)f (de\002nition.)23 b(Lik)o(e)17 b(nested)h(function)e(de\002nitions,)h (lambda)g(forms)g(can)h(reference)e(v)n(ariables)h(from)g(the)g (containing)0 1937 y(scope:)236 2175 y Fk(>>>)45 b(def)f (make_incrementor\(n\):)236 2266 y(...)224 b(return)44 b(lambda)g(x:)g(x)h(+)f(n)236 2358 y(...)236 2449 y(>>>)h(f)f(=)h (make_incrementor\(42\))236 2540 y(>>>)g(f\(0\))236 2632 y(42)236 2723 y(>>>)g(f\(1\))236 2814 y(43)0 3239 y Fj(4.7.6)100 b(Documentation)30 b(Str)q(ings)0 3442 y Fs(There)19 b(are)h(emer)o(ging)e(con)m(v)o(entions)g(about)h(the)h(content)f(and)h (formatting)e(of)i(documentation)d(strings.)0 3589 y(The)22 b(\002rst)h(line)g(should)e(al)o(w)o(ays)i(be)f(a)h(short,)f(concise)g (summary)f(of)h(the)g(object')-5 b(s)22 b(purpose.)30 b(F)o(or)22 b(bre)n(vity)-5 b(,)21 b(it)i(should)f(not)g(e)o(xplicitly) 0 3688 y(state)29 b(the)e(object')-5 b(s)28 b(name)f(or)h(type,)h (since)f(these)g(are)f(a)n(v)n(ailable)h(by)f(other)g(means)h(\(e)o (xcept)e(if)i(the)g(name)f(happens)g(to)h(be)f(a)i(v)o(erb)0 3788 y(describing)19 b(a)h(function')-5 b(s)19 b(operation\).)k(This)d (line)g(should)g(be)o(gin)f(with)h(a)h(capital)f(letter)g(and)g(end)f (with)i(a)f(period.)0 3935 y(If)i(there)f(are)h(more)f(lines)h(in)g (the)g(documentation)d(string,)j(the)g(second)e(line)i(should)f(be)h (blank,)f(visually)g(separating)g(the)h(summary)0 4035 y(from)e(the)h(rest)g(of)g(the)g(description.)26 b(The)21 b(follo)n(wing)e(lines)j(should)e(be)h(one)f(or)h(more)f(paragraphs)f (describing)g(the)i(object')-5 b(s)21 b(calling)0 4134 y(con)m(v)o(entions,)c(its)k(side)g(ef)n(fects,)f(etc.)0 4281 y(The)i(Python)f(parser)h(does)g(not)f(strip)i(indentation)d(from) h(multi-line)h(string)g(literals)g(in)h(Python,)e(so)i(tools)f(that)g (process)g(documen-)0 4381 y(tation)g(ha)n(v)o(e)g(to)g(strip)h (indentation)e(if)h(desired.)31 b(This)23 b(is)g(done)e(using)h(the)h (follo)n(wing)d(con)m(v)o(ention.)29 b(The)22 b(\002rst)h(non-blank)d (line)i Fq(after)0 4480 y Fs(the)28 b(\002rst)g(line)f(of)h(the)f (string)h(determines)e(the)h(amount)g(of)g(indentation)f(for)g(the)i (entire)f(documentation)e(string.)46 b(\(W)-7 b(e)29 b(can')o(t)d(use)0 4580 y(the)c(\002rst)g(line)f(since)h(it)g(is)h (generally)d(adjacent)h(to)g(the)h(string')-5 b(s)21 b(opening)f(quotes)h(so)h(its)g(indentation)e(is)j(not)e(apparent)f(in) h(the)h(string)0 4680 y(literal.\))28 b(Whitespace)21 b(\223equi)n(v)n(alent\224)e(to)i(this)h(indentation)d(is)j(then)f (stripped)f(from)h(the)g(start)g(of)g(all)h(lines)g(of)f(the)g(string.) 27 b(Lines)21 b(that)0 4779 y(are)j(indented)f(less)j(should)d(not)i (occur)m(,)f(b)n(ut)g(if)h(the)o(y)f(occur)f(all)i(their)f(leading)g (whitespace)g(should)f(be)i(stripped.)37 b(Equi)n(v)n(alence)22 b(of)0 4879 y(whitespace)e(should)f(be)h(tested)g(after)g(e)o(xpansion) e(of)i(tabs)h(\(to)f(8)g(spaces,)g(normally\).)0 5026 y(Here)g(is)h(an)f(e)o(xample)f(of)h(a)h(multi-line)e(docstring:)p 0 5549 3901 4 v 0 5649 a Fo(4.7.)52 b(More)23 b(on)h(De\002ning)f (Functions)2603 b(27)p eop end %%Page: 28 36 TeXDict begin 28 35 bop 236 174 a Fk(>>>)45 b(def)f(my_function\(\):) 236 266 y(...)224 b("""Do)44 b(nothing,)f(but)i(document)e(it.)236 357 y(...)236 448 y(...)224 b(No,)44 b(really,)g(it)g(doesn't)g(do)h (anything.)236 540 y(...)224 b(""")236 631 y(...)g(pass)236 722 y(...)236 814 y(>>>)45 b(print)f(my_function.__doc__)236 905 y(Do)h(nothing,)e(but)i(document)e(it.)416 1088 y(No,)h(really,)g (it)g(doesn't)g(do)g(anything.)p 0 5549 3901 4 v 0 5649 a Fo(28)2453 b(Chapter)23 b(4.)52 b(More)23 b(Control)h(Flo)o(w)f(T)-10 b(ools)p eop end %%Page: 29 37 TeXDict begin 29 36 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3634 427 y Fn(FIVE)p 0 515 V 2405 978 a Fy(Data)57 b(Str)s(uctures)0 1457 y Fs(This)20 b(chapter)f(describes)h(some)g(things)g(you')l(v)o(e) e(learned)h(about)g(already)g(in)i(more)e(detail,)h(and)g(adds)f(some)h (ne)n(w)g(things)g(as)h(well.)0 1784 y Fl(5.1)121 b(More)33 b(on)h(Lists)0 2017 y Fs(The)20 b(list)h(data)f(type)g(has)g(some)g (more)g(methods.)k(Here)c(are)g(all)h(of)e(the)i(methods)e(of)h(list)h (objects:)0 2164 y Fd(append)p Fp(\()p Fq(x)p Fp(\))208 2263 y Fs(Add)e(an)h(item)h(to)f(the)g(end)g(of)g(the)g(list;)h(equi)n (v)n(alent)e(to)h Fp(a[len\(a\):])98 b(=)50 b([)p Fq(x)p Fp(])p Fs(.)0 2410 y Fd(extend)p Fp(\()p Fq(L)p Fp(\))208 2510 y Fs(Extend)19 b(the)h(list)h(by)f(appending)d(all)k(the)f(items)h (in)f(the)h(gi)n(v)o(en)e(list;)i(equi)n(v)n(alent)d(to)j Fp(a[len\(a\):])97 b(=)50 b Fq(L)p Fs(.)0 2657 y Fd(insert)p Fp(\()p Fq(i,)19 b(x)p Fp(\))208 2756 y Fs(Insert)38 b(an)h(item)g(at)h(a)f(gi)n(v)o(en)f(position.)81 b(The)39 b(\002rst)g(ar)o(gument)e(is)j(the)f(inde)o(x)f(of)h(the)g(element)f (before)g(which)h(to)g(in-)208 2856 y(sert,)33 b(so)f Fp(a.insert\(0,)47 b Fq(x)p Fp(\))32 b Fs(inserts)f(at)g(the)g(front)f (of)h(the)g(list,)j(and)c Fp(a.insert\(len\(a\),)47 b Fq(x)p Fp(\))31 b Fs(is)h(equi)n(v)n(alent)d(to)208 2956 y Fp(a.append\()p Fq(x)p Fp(\))p Fs(.)0 3102 y Fd(remove)p Fp(\()p Fq(x)p Fp(\))208 3202 y Fs(Remo)o(v)o(e)19 b(the)h(\002rst)h (item)f(from)f(the)h(list)i(whose)e(v)n(alue)f(is)i Fq(x)p Fs(.)26 b(It)20 b(is)h(an)g(error)e(if)h(there)g(is)h(no)f(such)g (item.)0 3349 y Fd(pop)p Fp(\()p Fc([)p Fq(i)12 b Fc(])p Fp(\))208 3449 y Fs(Remo)o(v)o(e)26 b(the)i(item)g(at)h(the)f(gi)n(v)o (en)e(position)h(in)h(the)g(list,)j(and)c(return)g(it.)49 b(If)28 b(no)f(inde)o(x)g(is)i(speci\002ed,)g Fp(a.pop\(\))e Fs(remo)o(v)o(es)208 3548 y(and)j(returns)f(the)i(last)h(item)f(in)f (the)h(list.)58 b(\(The)30 b(square)g(brack)o(ets)g(around)e(the)j Fq(i)g Fs(in)g(the)g(method)e(signature)h(denote)f(that)208 3648 y(the)24 b(parameter)f(is)i(optional,)f(not)g(that)h(you)f(should) f(type)h(square)g(brack)o(ets)f(at)i(that)g(position.)37 b(Y)-9 b(ou)24 b(will)h(see)g(this)g(notation)208 3747 y(frequently)18 b(in)i(the)g Fq(Python)f(Libr)o(ary)h(Refer)m(ence)p Fs(.\))0 3894 y Fd(index)p Fp(\()p Fq(x)p Fp(\))208 3994 y Fs(Return)f(the)i(inde)o(x)d(in)j(the)f(list)h(of)f(the)g(\002rst)h (item)g(whose)f(v)n(alue)f(is)i Fq(x)p Fs(.)26 b(It)20 b(is)h(an)g(error)e(if)h(there)g(is)h(no)f(such)g(item.)0 4141 y Fd(count)p Fp(\()p Fq(x)p Fp(\))208 4240 y Fs(Return)f(the)i (number)d(of)i(times)g Fq(x)h Fs(appears)f(in)g(the)g(list.)0 4387 y Fd(sort)p Fp(\(\))208 4487 y Fs(Sort)g(the)g(items)h(of)e(the)i (list,)g(in)f(place.)0 4634 y Fd(reverse)p Fp(\(\))208 4733 y Fs(Re)n(v)o(erse)f(the)i(elements)f(of)f(the)i(list,)g(in)f (place.)0 4880 y(An)g(e)o(xample)f(that)h(uses)h(most)f(of)g(the)g (list)i(methods:)p 0 5549 3901 4 v 3808 5649 a Fo(29)p eop end %%Page: 30 38 TeXDict begin 30 37 bop 236 174 a Fk(>>>)45 b(a)f(=)h([66.25,)f(333,)g (333,)g(1,)g(1234.5])236 266 y(>>>)h(print)f(a.count\(333\),)e (a.count\(66.25\),)g(a.count\('x'\))236 357 y(2)j(1)g(0)236 448 y(>>>)g(a.insert\(2,)e(-1\))236 540 y(>>>)i(a.append\(333\))236 631 y(>>>)g(a)236 722 y([66.25,)f(333,)g(-1,)g(333,)h(1,)f(1234.5,)g (333])236 814 y(>>>)h(a.index\(333\))236 905 y(1)236 996 y(>>>)g(a.remove\(333\))236 1088 y(>>>)g(a)236 1179 y([66.25,)f(-1,)g(333,)g(1,)h(1234.5,)f(333])236 1270 y(>>>)h(a.reverse\(\))236 1362 y(>>>)g(a)236 1453 y([333,)f(1234.5,)g (1,)g(333,)h(-1,)f(66.25])236 1544 y(>>>)h(a.sort\(\))236 1636 y(>>>)g(a)236 1727 y([-1,)f(1,)h(66.25,)f(333,)g(333,)g(1234.5])0 2151 y Fj(5.1.1)100 b(Using)29 b(Lists)e(as)h(Stac)n(ks)0 2354 y Fs(The)22 b(list)i(methods)e(mak)o(e)g(it)i(v)o(ery)d(easy)i(to) g(use)g(a)g(list)h(as)f(a)h(stack,)f(where)f(the)h(last)g(element)g (added)e(is)j(the)f(\002rst)g(element)f(retrie)n(v)o(ed)0 2454 y(\(\223last-in,)i(\002rst-out\224\).)35 b(T)-7 b(o)24 b(add)f(an)h(item)g(to)g(the)f(top)h(of)f(the)h(stack,)h(use)f Fp(append\(\))p Fs(.)35 b(T)-7 b(o)24 b(retrie)n(v)o(e)e(an)i(item)g (from)f(the)h(top)f(of)h(the)0 2554 y(stack,)c(use)h Fp(pop\(\))e Fs(without)h(an)g(e)o(xplicit)g(inde)o(x.)j(F)o(or)d(e)o (xample:)236 2792 y Fk(>>>)45 b(stack)f(=)g([3,)h(4,)f(5])236 2883 y(>>>)h(stack.append\(6\))236 2974 y(>>>)g(stack.append\(7\))236 3066 y(>>>)g(stack)236 3157 y([3,)g(4,)f(5,)h(6,)f(7])236 3248 y(>>>)h(stack.pop\(\))236 3340 y(7)236 3431 y(>>>)g(stack)236 3522 y([3,)g(4,)f(5,)h(6])236 3614 y(>>>)g(stack.pop\(\))236 3705 y(6)236 3796 y(>>>)g(stack.pop\(\))236 3888 y(5)236 3979 y(>>>)g(stack)236 4070 y([3,)g(4])0 4495 y Fj(5.1.2)100 b(Using)29 b(Lists)e(as)h(Queues)0 4698 y Fs(Y)-9 b(ou)23 b(can)g(also)g(use)h(a)f(list)i(con)m(v)o(eniently)20 b(as)k(a)g(queue,)e(where)h(the)g(\002rst)h(element)f(added)f(is)i(the) f(\002rst)h(element)f(retrie)n(v)o(ed)e(\(\223\002rst-in,)0 4797 y(\002rst-out\224\).)28 b(T)-7 b(o)22 b(add)f(an)g(item)h(to)g (the)f(back)g(of)g(the)h(queue,)e(use)i Fp(append\(\))p Fs(.)28 b(T)-7 b(o)22 b(retrie)n(v)o(e)e(an)i(item)g(from)e(the)i (front)e(of)h(the)h(queue,)0 4897 y(use)e Fp(pop\(\))g Fs(with)h Fp(0)f Fs(as)h(the)f(inde)o(x.)k(F)o(or)c(e)o(xample:)p 0 5549 3901 4 v 0 5649 a Fo(30)2768 b(Chapter)23 b(5.)52 b(Data)23 b(Str)q(uctures)p eop end %%Page: 31 39 TeXDict begin 31 38 bop 236 174 a Fk(>>>)45 b(queue)f(=)g(["Eric",)g ("John",)f("Michael"])236 266 y(>>>)i(queue.append\("Terry"\))489 b(#)45 b(Terry)f(arrives)236 357 y(>>>)h(queue.append\("Graham"\))444 b(#)45 b(Graham)f(arrives)236 448 y(>>>)h(queue.pop\(0\))236 540 y('Eric')236 631 y(>>>)g(queue.pop\(0\))236 722 y('John')236 814 y(>>>)g(queue)236 905 y(['Michael',)e('Terry',)h('Graham'])0 1327 y Fj(5.1.3)100 b(Functional)29 b(Prog)o(r)o(amming)i(T)-12 b(ools)0 1530 y Fs(There)19 b(are)h(three)g(b)n(uilt-in)g(functions)f (that)h(are)g(v)o(ery)f(useful)h(when)f(used)h(with)h(lists:)26 b Fp(filter\(\))p Fs(,)19 b Fp(map\(\))p Fs(,)h(and)f Fp(reduce\(\))p Fs(.)0 1677 y(`)p Fp(filter\()p Fq(function)p Fp(,)46 b Fq(sequence)p Fp(\))p Fs(')29 b(returns)g(a)i(sequence)e (consisting)g(of)h(those)g(items)h(from)e(the)h(sequence)f(for)h(which) f Fq(func-)0 1777 y(tion)p Fp(\()p Fq(item)p Fp(\))22 b Fs(is)h(true.)32 b(If)23 b Fq(sequence)e Fs(is)j(a)f Fp(string)f Fs(or)g Fp(tuple)p Fs(,)h(the)f(result)h(will)g(be)g(of)f (the)h(same)g(type;)g(otherwise,)f(it)i(is)f(al)o(w)o(ays)g(a)0 1876 y Fp(list)p Fs(.)i(F)o(or)20 b(e)o(xample,)e(to)i(compute)f(some)h (primes:)236 2023 y Fk(>>>)45 b(def)f(f\(x\):)g(return)g(x)g(\045)h(2)g (!=)f(0)h(and)f(x)h(\045)f(3)h(!=)g(0)236 2114 y(...)236 2206 y(>>>)g(filter\(f,)e(range\(2,)g(25\)\))236 2297 y([5,)i(7,)f(11,)g(13,)h(17,)f(19,)g(23])0 2583 y Fs(`)p Fp(map\()p Fq(function)p Fp(,)j Fq(sequence)p Fp(\))p Fs(')26 b(calls)k Fq(function)p Fp(\()p Fq(item)p Fp(\))c Fs(for)i(each)h(of)f(the)g(sequence')-5 b(s)28 b(items)h(and)f(returns) f(a)i(list)h(of)e(the)h(return)0 2683 y(v)n(alues.)c(F)o(or)19 b(e)o(xample,)g(to)h(compute)f(some)h(cubes:)236 2921 y Fk(>>>)45 b(def)f(cube\(x\):)f(return)h(x)1357 2934 y(*)1402 2921 y(x)1447 2934 y(*)1492 2921 y(x)236 3012 y(...)236 3104 y(>>>)h(map\(cube,)e(range\(1,)g(11\)\))236 3195 y([1,)i(8,)f(27,)g(64,)h(125,)f(216,)g(343,)g(512,)g(729,)g(1000]) 0 3481 y Fs(More)23 b(than)g(one)g(sequence)g(may)g(be)g(passed;)j(the) d(function)f(must)i(then)f(ha)n(v)o(e)g(as)h(man)o(y)f(ar)o(guments)e (as)k(there)e(are)g(sequences)g(and)0 3581 y(is)k(called)e(with)h(the)g (corresponding)c(item)k(from)f(each)g(sequence)g(\(or)g Fp(None)h Fs(if)g(some)g(sequence)e(is)j(shorter)e(than)g(another\).)40 b(F)o(or)0 3681 y(e)o(xample:)236 3919 y Fk(>>>)45 b(seq)f(=)h (range\(8\))236 4010 y(>>>)g(def)f(add\(x,)g(y\):)g(return)g(x+y)236 4101 y(...)236 4193 y(>>>)h(map\(add,)e(seq,)h(seq\))236 4284 y([0,)h(2,)f(4,)h(6,)f(8,)h(10,)f(12,)g(14])0 4570 y Fs(`)p Fp(reduce\()p Fq(function)p Fp(,)i Fq(sequence)p Fp(\))p Fs(')16 b(returns)h(a)i(single)f(v)n(alue)f(constructed)f(by)i (calling)f(the)h(binary)f(function)f Fq(function)g Fs(on)i(the)g (\002rst)0 4670 y(tw)o(o)24 b(items)h(of)f(the)g(sequence,)g(then)f(on) h(the)g(result)h(and)e(the)h(ne)o(xt)g(item,)h(and)e(so)i(on.)36 b(F)o(or)24 b(e)o(xample,)f(to)i(compute)d(the)j(sum)f(of)g(the)0 4769 y(numbers)19 b(1)h(through)e(10:)p 0 5549 3901 4 v 0 5649 a Fo(5.1.)52 b(More)23 b(on)h(Lists)3119 b(31)p eop end %%Page: 32 40 TeXDict begin 32 39 bop 236 174 a Fk(>>>)45 b(def)f(add\(x,y\):)f (return)h(x+y)236 266 y(...)236 357 y(>>>)h(reduce\(add,)e(range\(1,)g (11\)\))236 448 y(55)0 734 y Fs(If)20 b(there')-5 b(s)20 b(only)g(one)f(item)i(in)f(the)g(sequence,)f(its)i(v)n(alue)f(is)h (returned;)d(if)j(the)f(sequence)f(is)i(empty)-5 b(,)19 b(an)h(e)o(xception)e(is)j(raised.)0 881 y(A)i(third)g(ar)o(gument)d (can)j(be)g(passed)g(to)g(indicate)f(the)h(starting)g(v)n(alue.)33 b(In)22 b(this)i(case)f(the)g(starting)g(v)n(alue)f(is)i(returned)d (for)i(an)f(empty)0 981 y(sequence,)i(and)f(the)h(function)e(is)j (\002rst)g(applied)e(to)h(the)g(starting)f(v)n(alue)h(and)f(the)h (\002rst)h(sequence)e(item,)i(then)e(to)h(the)g(result)g(and)g(the)0 1081 y(ne)o(xt)19 b(item,)i(and)e(so)i(on.)j(F)o(or)c(e)o(xample,)236 1319 y Fk(>>>)45 b(def)f(sum\(seq\):)236 1410 y(...)224 b(def)44 b(add\(x,y\):)g(return)f(x+y)236 1501 y(...)224 b(return)44 b(reduce\(add,)f(seq,)h(0\))236 1593 y(...)236 1684 y(>>>)h(sum\(range\(1,)d(11\)\))236 1775 y(55)236 1867 y(>>>)j(sum\([]\))236 1958 y(0)0 2244 y Fs(Don')o(t)24 b(use)h(this)h(e)o(xample')-5 b(s)24 b(de\002nition)g(of)h Fp(sum\(\))p Fs(:)34 b(since)26 b(summing)e(numbers)f(is)j(such)f(a)h (common)d(need,)i(a)h(b)n(uilt-in)e(function)0 2344 y Fp(sum\()p Fq(sequence)p Fp(\))18 b Fs(is)j(already)e(pro)o(vided,)f (and)h(w)o(orks)h(e)o(xactly)f(lik)o(e)i(this.)51 b(Ne)n(w)21 b(in)f(v)o(ersion)f(2.3.)0 2629 y Fj(5.1.4)100 b(List)28 b(Comprehensions)0 2832 y Fs(List)i(comprehensions)d(pro)o(vide)h(a)i (concise)f(w)o(ay)h(to)f(create)h(lists)h(without)e(resorting)f(to)i (use)f(of)h Fp(map\(\))p Fs(,)h Fp(filter\(\))e Fs(and/or)0 2931 y Fp(lambda)p Fs(.)61 b(The)32 b(resulting)f(list)j(de\002nition)d (tends)h(often)g(to)h(be)f(clearer)g(than)f(lists)j(b)n(uilt)f(using)f (those)g(constructs.)61 b(Each)31 b(list)0 3031 y(comprehension)18 b(consists)k(of)f(an)g(e)o(xpression)e(follo)n(wed)h(by)h(a)g Fp(for)g Fs(clause,)h(then)e(zero)h(or)g(more)f Fp(for)h Fs(or)g Fp(if)h Fs(clauses.)28 b(The)21 b(result)0 3131 y(will)h(be)e(a)h(list)h(resulting)e(from)g(e)n(v)n(aluating)f(the)i(e) o(xpression)e(in)i(the)g(conte)o(xt)e(of)i(the)f Fp(for)h Fs(and)f Fp(if)h Fs(clauses)g(which)g(follo)n(w)f(it.)27 b(If)21 b(the)0 3230 y(e)o(xpression)e(w)o(ould)g(e)n(v)n(aluate)g(to)i (a)f(tuple,)g(it)h(must)f(be)g(parenthesized.)p 0 5549 3901 4 v 0 5649 a Fo(32)2768 b(Chapter)23 b(5.)52 b(Data)23 b(Str)q(uctures)p eop end %%Page: 33 41 TeXDict begin 33 40 bop 236 174 a Fk(>>>)45 b(freshfruit)e(=)h([')90 b(banana',)43 b(')90 b(loganberry)43 b(',)h('passion)g(fruit)89 b('])236 266 y(>>>)45 b([weapon.strip\(\))d(for)i(weapon)g(in)h (freshfruit])236 357 y(['banana',)e('loganberry',)g('passion)h(fruit']) 236 448 y(>>>)h(vec)f(=)h([2,)f(4,)g(6])236 540 y(>>>)h([3)506 553 y(*)551 540 y(x)f(for)g(x)h(in)f(vec])236 631 y([6,)h(12,)f(18])236 722 y(>>>)h([3)506 735 y(*)551 722 y(x)f(for)g(x)h(in)f(vec)h(if)f(x)h (>)f(3])236 814 y([12,)g(18])236 905 y(>>>)h([3)506 918 y(*)551 905 y(x)f(for)g(x)h(in)f(vec)h(if)f(x)h(<)f(2])236 996 y([])236 1088 y(>>>)h([[x,x)641 1101 y(**)731 1088 y(2])e(for)h(x)h(in)f(vec])236 1179 y([[2,)g(4],)h([4,)f(16],)g([6,)h (36]])236 1270 y(>>>)g([x,)f(x)640 1283 y(**)730 1270 y(2)g(for)g(x)h(in)g(vec])f(#)g(error)g(-)h(parens)f(required)f(for)i (tuples)326 1362 y(File)f("",)f(line)h(1,)h(in)f(?)416 1453 y([x,)g(x)640 1466 y(**)730 1453 y(2)g(for)g(x)h(in)g(vec])909 1544 y(^)236 1636 y(SyntaxError:)e(invalid)h(syntax)236 1727 y(>>>)h([\(x,)f(x)685 1740 y(**)775 1727 y(2\))g(for)g(x)h(in)f (vec])236 1818 y([\(2,)g(4\),)h(\(4,)f(16\),)g(\(6,)h(36\)])236 1910 y(>>>)g(vec1)f(=)g([2,)h(4,)f(6])236 2001 y(>>>)h(vec2)f(=)g([4,)h (3,)f(-9])236 2092 y(>>>)h([x)506 2105 y(*)551 2092 y(y)f(for)g(x)h(in) f(vec1)g(for)h(y)f(in)h(vec2])236 2183 y([8,)g(6,)f(-18,)g(16,)h(12,)f (-36,)g(24,)g(18,)h(-54])236 2275 y(>>>)g([x+y)f(for)g(x)h(in)f(vec1)g (for)h(y)f(in)h(vec2])236 2366 y([6,)g(5,)f(-7,)g(8,)h(7,)f(-5,)h(10,)f (9,)h(-3])236 2457 y(>>>)g([vec1[i])776 2470 y(*)821 2457 y(vec2[i])d(for)i(i)h(in)f(range\(len\(vec1\)\)])236 2549 y([8,)h(12,)f(-54])0 2835 y Fs(List)32 b(comprehensions)c(are)k (much)e(more)g(\003e)o(xible)h(than)g Fp(map\(\))g Fs(and)g(can)g(be)g (applied)f(to)i(comple)o(x)d(e)o(xpressions)h(and)h(nested)0 2935 y(functions:)236 3173 y Fk(>>>)45 b([str\(round\(355/113.0,)c (i\)\))j(for)h(i)f(in)h(range\(1,6\)])236 3264 y(['3.1',)f('3.14',)g ('3.142',)f('3.1416',)g('3.14159'])0 3710 y Fl(5.2)121 b(The)34 b Ff(del)e Fl(statement)0 3943 y Fs(There)20 b(is)h(a)h(w)o(ay)e(to)h(remo)o(v)o(e)e(an)h(item)h(from)f(a)h(list)h (gi)n(v)o(en)d(its)j(inde)o(x)d(instead)i(of)f(its)i(v)n(alue:)j(the)c Fp(del)g Fs(statement.)26 b(This)21 b(dif)n(fers)f(from)0 4042 y(the)f Fp(pop\(\))f Fs(method)f(which)h(returns)g(a)h(v)n(alue.) 24 b(The)18 b Fp(del)h Fs(statement)f(can)g(also)h(be)g(used)f(to)h (remo)o(v)o(e)e(slices)i(from)f(a)h(list)h(or)e(clear)h(the)0 4142 y(entire)h(list)h(\(which)e(we)i(did)f(earlier)g(by)f(assignment)h (of)g(an)g(empty)f(list)i(to)g(the)f(slice\).)25 b(F)o(or)20 b(e)o(xample:)236 4380 y Fk(>>>)45 b(a)f(=)h([-1,)f(1,)h(66.25,)e(333,) i(333,)f(1234.5])236 4472 y(>>>)h(del)f(a[0])236 4563 y(>>>)h(a)236 4654 y([1,)g(66.25,)e(333,)i(333,)f(1234.5])236 4745 y(>>>)h(del)f(a[2:4])236 4837 y(>>>)h(a)236 4928 y([1,)g(66.25,)e(1234.5])236 5019 y(>>>)i(del)f(a[:])236 5111 y(>>>)h(a)236 5202 y([])p 0 5549 3901 4 v 0 5649 a Fo(5.2.)52 b(The)23 b Fp(del)g Fo(statement)2915 b(33)p eop end %%Page: 34 42 TeXDict begin 34 41 bop 0 83 a Fp(del)20 b Fs(can)g(also)h(be)f(used)g (to)g(delete)g(entire)g(v)n(ariables:)236 321 y Fk(>>>)45 b(del)f(a)0 607 y Fs(Referencing)22 b(the)j(name)e Fp(a)i Fs(hereafter)e(is)i(an)f(error)f(\(at)h(least)h(until)f(another)f(v)n (alue)g(is)j(assigned)d(to)i(it\).)37 b(W)-7 b(e')o(ll)25 b(\002nd)f(other)f(uses)i(for)0 707 y Fp(del)20 b Fs(later)-5 b(.)0 1034 y Fl(5.3)121 b(T)-14 b(uples)33 b(and)i(Sequences)0 1267 y Fs(W)-7 b(e)26 b(sa)o(w)f(that)g(lists)h(and)e(strings)h(ha)n(v) o(e)f(man)o(y)g(common)e(properties,)j(such)f(as)h(inde)o(xing)e(and)h (slicing)h(operations.)37 b(The)o(y)24 b(are)g(tw)o(o)0 1367 y(e)o(xamples)i(of)h Fq(sequence)f Fs(data)h(types.)45 b(Since)27 b(Python)f(is)i(an)f(e)n(v)n(olving)f(language,)g(other)h (sequence)f(data)g(types)h(may)g(be)g(added.)0 1467 y(There)19 b(is)i(also)g(another)e(standard)g(sequence)g(data)h(type:)25 b(the)20 b Fq(tuple)p Fs(.)0 1613 y(A)h(tuple)e(consists)i(of)f(a)h (number)d(of)i(v)n(alues)g(separated)f(by)h(commas,)f(for)h(instance:) 236 1852 y Fk(>>>)45 b(t)f(=)h(12345,)f(54321,)g('hello!')236 1943 y(>>>)h(t[0])236 2034 y(12345)236 2126 y(>>>)g(t)236 2217 y(\(12345,)f(54321,)g('hello!'\))236 2308 y(>>>)h(#)f(Tuples)g (may)g(be)h(nested:)236 2400 y(...)g(u)f(=)h(t,)f(\(1,)h(2,)f(3,)h(4,)f (5\))236 2491 y(>>>)h(u)236 2582 y(\(\(12345,)f(54321,)g('hello!'\),)f (\(1,)h(2,)h(3,)f(4,)g(5\)\))0 2868 y Fs(As)24 b(you)f(see,)i(on)e (output)g(tuples)g(are)h(al)o(w)o(ays)g(enclosed)f(in)h(parentheses,)f (so)h(that)g(nested)f(tuples)h(are)f(interpreted)f(correctly;)i(the)o (y)0 2968 y(may)d(be)h(input)e(with)i(or)f(without)g(surrounding)d (parentheses,)j(although)e(often)i(parentheses)g(are)g(necessary)g(an)o (yw)o(ay)f(\(if)h(the)h(tuple)0 3068 y(is)f(part)f(of)g(a)h(lar)o(ger)d (e)o(xpression\).)0 3214 y(T)l(uples)26 b(ha)n(v)o(e)g(man)o(y)f(uses.) 45 b(F)o(or)26 b(e)o(xample:)36 b(\(x,)28 b(y\))e(coordinate)f(pairs,)i (emplo)o(yee)e(records)h(from)f(a)i(database,)g(etc.)45 b(T)l(uples,)27 b(lik)o(e)0 3314 y(strings,)g(are)f(immutable:)36 b(it)26 b(is)h(not)f(possible)g(to)g(assign)g(to)g(the)g(indi)n(vidual) e(items)j(of)e(a)i(tuple)e(\(you)g(can)h(simulate)g(much)f(of)h(the)0 3414 y(same)20 b(ef)n(fect)f(with)h(slicing)f(and)g(concatenation,)f (though\).)k(It)e(is)h(also)f(possible)f(to)h(create)f(tuples)h(which)f (contain)f(mutable)h(objects,)0 3513 y(such)h(as)h(lists.)0 3660 y(A)k(special)f(problem)f(is)i(the)f(construction)f(of)h(tuples)g (containing)f(0)h(or)g(1)g(items:)34 b(the)25 b(syntax)e(has)i(some)f (e)o(xtra)g(quirks)f(to)i(accom-)0 3760 y(modate)c(these.)32 b(Empty)21 b(tuples)h(are)g(constructed)f(by)g(an)i(empty)e(pair)h(of)g (parentheses;)g(a)h(tuple)f(with)g(one)g(item)g(is)h(constructed)e(by)0 3859 y(follo)n(wing)f(a)i(v)n(alue)f(with)h(a)g(comma)f(\(it)h(is)g (not)g(suf)n(\002cient)f(to)h(enclose)f(a)h(single)g(v)n(alue)f(in)g (parentheses\).)28 b(Ugly)-5 b(,)21 b(b)n(ut)h(ef)n(fecti)n(v)o(e.)28 b(F)o(or)0 3959 y(e)o(xample:)236 4197 y Fk(>>>)45 b(empty)f(=)g(\(\)) 236 4289 y(>>>)h(singleton)e(=)i('hello',)178 b(#)44 b(<--)h(note)f(trailing)f(comma)236 4380 y(>>>)i(len\(empty\))236 4471 y(0)236 4563 y(>>>)g(len\(singleton\))236 4654 y(1)236 4745 y(>>>)g(singleton)236 4837 y(\('hello',\))0 5123 y Fs(The)19 b(statement)h Fp(t)49 b(=)h(12345,)f(54321,)f('hello!')25 b Fs(is)20 b(an)g(e)o(xample)e(of)i Fq(tuple)f(pac)n(king)p Fs(:)24 b(the)19 b(v)n(alues)h Fp(12345)p Fs(,)f Fp(54321)g Fs(and)0 5222 y Fp('hello!')24 b Fs(are)c(pack)o(ed)f(together)g(in)i (a)f(tuple.)25 b(The)20 b(re)n(v)o(erse)f(operation)f(is)j(also)g (possible:)p 0 5549 3901 4 v 0 5649 a Fo(34)2768 b(Chapter)23 b(5.)52 b(Data)23 b(Str)q(uctures)p eop end %%Page: 35 43 TeXDict begin 35 42 bop 236 174 a Fk(>>>)45 b(x,)f(y,)h(z)f(=)h(t)0 461 y Fs(This)26 b(is)h(called,)g(appropriately)c(enough,)i Fq(sequence)f(unpac)n(king)p Fs(.)40 b(Sequence)25 b(unpacking)e (requires)i(the)h(list)h(of)e(v)n(ariables)g(on)h(the)0 560 y(left)c(to)f(ha)n(v)o(e)g(the)h(same)f(number)f(of)h(elements)g (as)h(the)g(length)e(of)h(the)h(sequence.)27 b(Note)22 b(that)f(multiple)g(assignment)g(is)h(really)f(just)h(a)0 660 y(combination)c(of)i(tuple)g(packing)e(and)i(sequence)f(unpacking!) 0 807 y(There)i(is)h(a)g(small)g(bit)g(of)f(asymmetry)f(here:)28 b(packing)20 b(multiple)g(v)n(alues)i(al)o(w)o(ays)g(creates)f(a)h (tuple,)f(and)g(unpacking)e(w)o(orks)i(for)g(an)o(y)0 906 y(sequence.)0 1234 y Fl(5.4)121 b(Sets)0 1467 y Fs(Python)26 b(also)i(includes)e(a)i(data)f(type)g(for)f Fq(sets)p Fs(.)47 b(A)28 b(set)g(is)g(an)f(unordered)e(collection)h(with)h(no)g (duplicate)f(elements.)46 b(Basic)28 b(uses)0 1566 y(include)23 b(membership)g(testing)h(and)g(eliminating)f(duplicate)h(entries.)37 b(Set)25 b(objects)f(also)h(support)e(mathematical)g(operations)g(lik)o (e)0 1666 y(union,)c(intersection,)g(dif)n(ference,)e(and)j(symmetric)f (dif)n(ference.)0 1813 y(Here)h(is)h(a)g(brief)e(demonstration:)236 2051 y Fk(>>>)45 b(basket)e(=)i(['apple',)e('orange',)h('apple',)f ('pear',)h('orange',)f('banana'])236 2142 y(>>>)i(fruit)f(=)g (set\(basket\))671 b(#)44 b(create)g(a)h(set)f(without)g(duplicates)236 2233 y(>>>)h(fruit)236 2325 y(set\(['orange',)e('pear',)g('apple',)h ('banana']\))236 2416 y(>>>)h('orange')e(in)i(fruit)761 b(#)44 b(fast)h(membership)e(testing)236 2507 y(True)236 2599 y(>>>)i('crabgrass')e(in)h(fruit)236 2690 y(False)236 2873 y(>>>)h(#)f(Demonstrate)f(set)i(operations)e(on)h(unique)g (letters)g(from)g(two)g(words)236 2964 y(...)236 3055 y(>>>)h(a)f(=)h(set\('abracadabra'\))236 3147 y(>>>)g(b)f(=)h (set\('alacazam'\))236 3238 y(>>>)g(a)1524 b(#)44 b(unique)g(letters)g (in)g(a)236 3329 y(set\(['a',)g('r',)g('b',)g('c',)g('d']\))236 3421 y(>>>)h(a)f(-)h(b)1345 b(#)44 b(letters)g(in)g(a)h(but)f(not)h(in) f(b)236 3512 y(set\(['r',)g('d',)g('b']\))236 3603 y(>>>)h(a)f(|)h(b) 1345 b(#)44 b(letters)g(in)g(either)g(a)h(or)f(b)236 3695 y(set\(['a',)g('c',)g('r',)g('d',)g('b',)g('m',)g('z',)g('l']\)) 236 3786 y(>>>)h(a)f(&)h(b)1345 b(#)44 b(letters)g(in)g(both)h(a)f(and) h(b)236 3877 y(set\(['a',)f('c']\))236 3969 y(>>>)h(a)f(^)h(b)1345 b(#)44 b(letters)g(in)g(a)h(or)g(b)f(but)h(not)f(both)236 4060 y(set\(['r',)g('d',)g('b',)g('m',)g('z',)g('l']\))0 4506 y Fl(5.5)121 b(Dictionar)r(ies)0 4739 y Fs(Another)27 b(useful)h(data)g(type)g(b)n(uilt)h(into)f(Python)g(is)h(the)g Fq(dictionary)p Fs(.)48 b(Dictionaries)28 b(are)h(sometimes)f(found)f (in)h(other)g(languages)0 4838 y(as)k(\223associati)n(v)o(e)f (memories\224)g(or)g(\223associati)n(v)o(e)g(arrays\224.)59 b(Unlik)o(e)31 b(sequences,)j(which)d(are)g(inde)o(x)o(ed)f(by)h(a)h (range)f(of)g(numbers,)0 4938 y(dictionaries)22 b(are)g(inde)o(x)o(ed)f (by)h Fq(k)o(e)n(ys)p Fs(,)h(which)f(can)g(be)h(an)o(y)f(immutable)f (type;)i(strings)g(and)f(numbers)f(can)h(al)o(w)o(ays)h(be)g(k)o(e)o (ys.)32 b(T)l(uples)0 5037 y(can)d(be)g(used)g(as)h(k)o(e)o(ys)e(if)i (the)o(y)e(contain)g(only)h(strings,)i(numbers,)e(or)g(tuples;)34 b(if)29 b(a)h(tuple)e(contains)h(an)o(y)f(mutable)g(object)h(either)0 5137 y(directly)20 b(or)i(indirectly)-5 b(,)19 b(it)j(cannot)e(be)i (used)f(as)h(a)f(k)o(e)o(y)-5 b(.)28 b(Y)-9 b(ou)20 b(can')o(t)h(use)g (lists)i(as)f(k)o(e)o(ys,)f(since)g(lists)i(can)e(be)h(modi\002ed)e(in) h(place)g(using)0 5237 y(inde)o(x)e(assignments,)g(slice)i (assignments,)f(or)g(methods)f(lik)o(e)h Fp(append\(\))f Fs(and)h Fp(extend\(\))p Fs(.)0 5384 y(It)h(is)g(best)g(to)f(think)g (of)g(a)h(dictionary)d(as)j(an)f(unordered)e(set)j(of)f Fq(k)o(e)n(y:)25 b(value)20 b Fs(pairs,)g(with)g(the)h(requirement)d (that)i(the)h(k)o(e)o(ys)f(are)g(unique)p 0 5549 3901 4 v 0 5649 a Fo(5.4.)52 b(Sets)3452 b(35)p eop end %%Page: 36 44 TeXDict begin 36 43 bop 0 83 a Fs(\(within)17 b(one)g(dictionary\).)22 b(A)c(pair)f(of)g(braces)h(creates)f(an)h(empty)e(dictionary:)22 b Fp({})p Fs(.)j(Placing)17 b(a)h(comma-separated)c(list)19 b(of)e(k)o(e)o(y:v)n(alue)0 183 y(pairs)26 b(within)g(the)g(braces)f (adds)h(initial)g(k)o(e)o(y:v)n(alue)e(pairs)i(to)g(the)g(dictionary;)h (this)g(is)f(also)h(the)f(w)o(ay)g(dictionaries)e(are)i(written)g(on)0 282 y(output.)0 429 y(The)18 b(main)h(operations)e(on)h(a)h(dictionary) e(are)i(storing)f(a)h(v)n(alue)f(with)h(some)f(k)o(e)o(y)g(and)g(e)o (xtracting)f(the)i(v)n(alue)f(gi)n(v)o(en)g(the)g(k)o(e)o(y)-5 b(.)24 b(It)19 b(is)g(also)0 529 y(possible)k(to)h(delete)f(a)h(k)o(e)o (y:v)n(alue)d(pair)j(with)f Fp(del)p Fs(.)35 b(If)23 b(you)g(store)g(using)g(a)h(k)o(e)o(y)f(that)g(is)h(already)f(in)g (use,)i(the)e(old)g(v)n(alue)g(associated)0 628 y(with)d(that)h(k)o(e)o (y)e(is)i(for)o(gotten.)i(It)d(is)h(an)f(error)g(to)g(e)o(xtract)f(a)i (v)n(alue)e(using)h(a)h(non-e)o(xistent)d(k)o(e)o(y)-5 b(.)0 775 y(The)18 b Fp(keys\(\))f Fs(method)f(of)i(a)g(dictionary)e (object)i(returns)f(a)h(list)h(of)e(all)i(the)f(k)o(e)o(ys)f(used)h(in) g(the)g(dictionary)-5 b(,)16 b(in)i(arbitrary)e(order)h(\(if)g(you)0 875 y(w)o(ant)22 b(it)h(sorted,)f(just)g(apply)g(the)g Fp(sort\(\))f Fs(method)g(to)h(the)g(list)h(of)f(k)o(e)o(ys\).)30 b(T)-7 b(o)22 b(check)g(whether)f(a)h(single)g(k)o(e)o(y)g(is)h(in)f (the)g(dictionary)-5 b(,)0 975 y(either)20 b(use)g(the)g(dictionary')-5 b(s)19 b Fp(has_key\(\))g Fs(method)g(or)h(the)g Fp(in)h Fs(k)o(e)o(yw)o(ord.)0 1121 y(Here)f(is)h(a)g(small)f(e)o(xample)f (using)h(a)h(dictionary:)236 1360 y Fk(>>>)45 b(tel)f(=)h({'jack':)e (4098,)h('sape':)g(4139})236 1451 y(>>>)h(tel['guido'])d(=)j(4127)236 1542 y(>>>)g(tel)236 1634 y({'sape':)f(4139,)g('guido':)f(4127,)h ('jack':)g(4098})236 1725 y(>>>)h(tel['jack'])236 1816 y(4098)236 1908 y(>>>)g(del)f(tel['sape'])236 1999 y(>>>)h(tel['irv'])e (=)h(4127)236 2090 y(>>>)h(tel)236 2182 y({'guido':)f(4127,)g('irv':)f (4127,)h('jack':)g(4098})236 2273 y(>>>)h(tel.keys\(\))236 2364 y(['guido',)f('irv',)f('jack'])236 2455 y(>>>)i (tel.has_key\('guido'\))236 2547 y(True)236 2638 y(>>>)g('guido')e(in)i (tel)236 2729 y(True)0 3016 y Fs(The)26 b Fp(dict\(\))h Fs(constructor)d(b)n(uilds)j(dictionaries)f(directly)g(from)f(lists)j (of)f(k)o(e)o(y-v)n(alue)d(pairs)j(stored)f(as)i(tuples.)44 b(When)27 b(the)g(pairs)0 3115 y(form)19 b(a)i(pattern,)e(list)i (comprehensions)c(can)j(compactly)f(specify)h(the)g(k)o(e)o(y-v)n(alue) e(list.)236 3353 y Fk(>>>)45 b(dict\([\('sape',)d(4139\),)i(\('guido',) f(4127\),)h(\('jack',)f(4098\)]\))236 3445 y({'sape':)h(4139,)g ('jack':)f(4098,)h('guido':)g(4127})236 3536 y(>>>)h(dict\([\(x,)e(x) 909 3549 y(**)999 3536 y(2\))h(for)g(x)h(in)f(\(2,)h(4,)f(6\)]\))224 b(#)44 b(use)h(a)f(list)g(comprehension)236 3627 y({2:)h(4,)f(4:)h(16,) f(6:)g(36})0 3914 y Fs(Later)21 b(in)g(the)h(tutorial,)e(we)i(will)g (learn)f(about)f(Generator)g(Expressions)g(which)h(are)g(e)n(v)o(en)f (better)h(suited)g(for)g(the)g(task)h(of)e(supplying)0 4013 y(k)o(e)o(y-v)n(alues)e(pairs)i(to)h(the)f Fp(dict\(\))g Fs(constructor)-5 b(.)0 4160 y(When)20 b(the)g(k)o(e)o(ys)g(are)g (simple)g(strings,)g(it)h(is)g(sometimes)f(easier)h(to)f(specify)f (pairs)i(using)e(k)o(e)o(yw)o(ord)g(ar)o(guments:)236 4398 y Fk(>>>)45 b(dict\(sape=4139,)d(guido=4127,)h(jack=4098\))236 4490 y({'sape':)h(4139,)g('jack':)f(4098,)h('guido':)g(4127})0 4935 y Fl(5.6)121 b(Looping)35 b(T)-14 b(echniques)0 5168 y Fs(When)31 b(looping)f(through)f(dictionaries,)j(the)g(k)o(e)o (y)e(and)h(corresponding)d(v)n(alue)i(can)h(be)g(retrie)n(v)o(ed)f(at)i (the)f(same)g(time)h(using)f(the)0 5268 y Fp(iteritems\(\))19 b Fs(method.)p 0 5549 3901 4 v 0 5649 a Fo(36)2768 b(Chapter)23 b(5.)52 b(Data)23 b(Str)q(uctures)p eop end %%Page: 37 45 TeXDict begin 37 44 bop 236 174 a Fk(>>>)45 b(knights)e(=)i ({'gallahad':)e('the)h(pure',)g('robin':)f('the)h(brave'})236 266 y(>>>)h(for)f(k,)g(v)h(in)g(knights.iteritems\(\):)236 357 y(...)224 b(print)44 b(k,)g(v)236 448 y(...)236 540 y(gallahad)g(the)g(pure)236 631 y(robin)g(the)h(brave)0 917 y Fs(When)19 b(looping)f(through)f(a)j(sequence,)e(the)h(position)g (inde)o(x)f(and)g(corresponding)e(v)n(alue)j(can)g(be)g(retrie)n(v)o (ed)f(at)i(the)f(same)h(time)f(using)0 1017 y(the)h Fp(enumerate\(\))f Fs(function.)236 1255 y Fk(>>>)45 b(for)f(i,)g(v)h(in)g (enumerate\(['tic',)d('tac',)h('toe']\):)236 1346 y(...)224 b(print)44 b(i,)g(v)236 1438 y(...)236 1529 y(0)h(tic)236 1620 y(1)g(tac)236 1712 y(2)g(toe)0 1998 y Fs(T)-7 b(o)20 b(loop)g(o)o(v)o(er)f(tw)o(o)h(or)g(more)f(sequences)h(at)g(the)g(same) h(time,)f(the)g(entries)g(can)g(be)g(paired)g(with)g(the)g Fp(zip\(\))g Fs(function.)236 2236 y Fk(>>>)45 b(questions)e(=)i (['name',)e('quest',)h('favorite)f(color'])236 2327 y(>>>)i(answers)e (=)i(['lancelot',)e('the)h(holy)g(grail',)g('blue'])236 2419 y(>>>)h(for)f(q,)g(a)h(in)g(zip\(questions,)d(answers\):)236 2510 y(...)224 b(print)44 b('What)g(is)g(your)h(\045s?)89 b(It)44 b(is)h(\045s.')f(\045)g(\(q,)h(a\))236 2601 y(...)236 2693 y(What)f(is)h(your)f(name?)89 b(It)44 b(is)h(lancelot.)236 2784 y(What)f(is)h(your)f(quest?)89 b(It)44 b(is)h(the)f(holy)g(grail.) 236 2875 y(What)g(is)h(your)f(favorite)g(color?)88 b(It)45 b(is)f(blue.)0 3161 y Fs(T)-7 b(o)23 b(loop)f(o)o(v)o(er)f(a)i (sequence)f(in)h(re)n(v)o(erse,)f(\002rst)h(specify)f(the)h(sequence)f (in)h(a)g(forw)o(ard)e(direction)g(and)i(then)f(call)h(the)g Fp(reversed\(\))0 3261 y Fs(function.)236 3499 y Fk(>>>)45 b(for)f(i)h(in)f(reversed\(xrange\(1,10,2\)\):)236 3591 y(...)224 b(print)44 b(i)236 3682 y(...)236 3773 y(9)236 3865 y(7)236 3956 y(5)236 4047 y(3)236 4139 y(1)0 4425 y Fs(T)-7 b(o)19 b(loop)g(o)o(v)o(er)e(a)j(sequence)e(in)h(sorted)g (order)m(,)e(use)i(the)g Fp(sorted\(\))g Fs(function)e(which)i(returns) f(a)h(ne)n(w)g(sorted)g(list)h(while)f(lea)n(ving)g(the)0 4524 y(source)g(unaltered.)p 0 5549 3901 4 v 0 5649 a Fo(5.6.)52 b(Looping)25 b(T)-10 b(echniques)2880 b(37)p eop end %%Page: 38 46 TeXDict begin 38 45 bop 236 174 a Fk(>>>)45 b(basket)e(=)i(['apple',)e ('orange',)h('apple',)f('pear',)h('orange',)f('banana'])236 266 y(>>>)i(for)f(f)h(in)f(sorted\(set\(basket\)\):)236 357 y(...)224 b(print)44 b(f)236 448 y(...)236 540 y(apple)236 631 y(banana)236 722 y(orange)236 814 y(pear)0 1262 y Fl(5.7)121 b(More)33 b(on)h(Conditions)0 1495 y Fs(The)20 b(conditions)f(used)g(in)i Fp(while)f Fs(and)f Fp(if)i Fs(statements)f(can)g(contain)f(an)o(y)g(operators,)g(not)h(just)h (comparisons.)0 1642 y(The)15 b(comparison)f(operators)g Fp(in)i Fs(and)f Fp(not)50 b(in)16 b Fs(check)f(whether)f(a)i(v)n(alue) f(occurs)g(\(does)h(not)f(occur\))f(in)i(a)g(sequence.)23 b(The)15 b(operators)0 1741 y Fp(is)24 b Fs(and)g Fp(is)49 b(not)24 b Fs(compare)f(whether)g(tw)o(o)h(objects)g(are)g(really)g (the)g(same)g(object;)i(this)e(only)g(matters)g(for)f(mutable)g (objects)h(lik)o(e)0 1841 y(lists.)i(All)21 b(comparison)d(operators)h (ha)n(v)o(e)h(the)g(same)g(priority)-5 b(,)18 b(which)i(is)h(lo)n(wer)f (than)g(that)g(of)g(all)h(numerical)d(operators.)0 1988 y(Comparisons)h(can)h(be)g(chained.)k(F)o(or)c(e)o(xample,)e Fp(a)50 b(<)f(b)h(==)f(c)21 b Fs(tests)g(whether)f Fp(a)g Fs(is)h(less)h(than)d Fp(b)i Fs(and)e(moreo)o(v)o(er)f Fp(b)i Fs(equals)g Fp(c)p Fs(.)0 2135 y(Comparisons)e(may)g(be)h (combined)e(using)h(the)h(Boolean)g(operators)e Fp(and)i Fs(and)f Fp(or)p Fs(,)i(and)e(the)h(outcome)e(of)i(a)g(comparison)e (\(or)i(of)f(an)o(y)0 2234 y(other)g(Boolean)g(e)o(xpression\))f(may)h (be)h(ne)o(gated)e(with)i Fp(not)p Fs(.)25 b(These)18 b(ha)n(v)o(e)h(lo)n(wer)f(priorities)g(than)h(comparison)e(operators;)g (between)0 2334 y(them,)h Fp(not)g Fs(has)h(the)f(highest)g(priority)e (and)i Fp(or)g Fs(the)h(lo)n(west,)f(so)h(that)f Fp(A)50 b(and)f(not)g(B)h(or)f(C)19 b Fs(is)g(equi)n(v)n(alent)e(to)h Fp(\(A)49 b(and)h(\(not)0 2434 y(B\)\))f(or)h(C)p Fs(.)20 b(As)h(al)o(w)o(ays,)f(parentheses)f(can)h(be)h(used)f(to)g(e)o(xpress) f(the)i(desired)e(composition.)0 2581 y(The)25 b(Boolean)f(operators)g Fp(and)h Fs(and)g Fp(or)g Fs(are)h(so-called)e Fq(short-cir)m(cuit)h Fs(operators:)34 b(their)25 b(ar)o(guments)e(are)i(e)n(v)n(aluated)f (from)g(left)h(to)0 2680 y(right,)g(and)g(e)n(v)n(aluation)e(stops)i (as)h(soon)e(as)i(the)f(outcome)f(is)i(determined.)37 b(F)o(or)25 b(e)o(xample,)f(if)i Fp(A)f Fs(and)f Fp(C)i Fs(are)f(true)f(b)n(ut)h Fp(B)h Fs(is)g(f)o(alse,)g Fp(A)0 2780 y(and)49 b(B)h(and)f(C)26 b Fs(does)e(not)h(e)n(v)n(aluate)f(the)h (e)o(xpression)f Fp(C)p Fs(.)h(When)g(used)f(as)i(a)g(general)e(v)n (alue)g(and)g(not)h(as)h(a)f(Boolean,)h(the)f(return)0 2879 y(v)n(alue)20 b(of)f(a)i(short-circuit)e(operator)f(is)j(the)f (last)h(e)n(v)n(aluated)e(ar)o(gument.)0 3026 y(It)h(is)i(possible)e (to)g(assign)g(the)g(result)h(of)f(a)g(comparison)e(or)i(other)g (Boolean)f(e)o(xpression)g(to)h(a)h(v)n(ariable.)i(F)o(or)d(e)o (xample,)236 3173 y Fk(>>>)45 b(string1,)e(string2,)h(string3)f(=)i ('',)f('Trondheim',)f('Hammer)h(Dance')236 3265 y(>>>)h(non_null)e(=)i (string1)e(or)i(string2)f(or)g(string3)236 3356 y(>>>)h(non_null)236 3447 y('Trondheim')0 3733 y Fs(Note)23 b(that)f(in)h(Python,)f(unlik)o (e)g(C,)i(assignment)e(cannot)f(occur)h(inside)g(e)o(xpressions.)32 b(C)23 b(programmers)d(may)j(grumble)e(about)g(this,)0 3833 y(b)n(ut)16 b(it)g(a)n(v)n(oids)g(a)g(common)e(class)j(of)e (problems)g(encountered)e(in)i(C)i(programs:)k(typing)15 b Fp(=)h Fs(in)g(an)g(e)o(xpression)e(when)h Fp(==)h Fs(w)o(as)g(intended.)0 4160 y Fl(5.8)121 b(Compar)r(ing)34 b(Sequences)i(and)e(Other)h(T)-14 b(ypes)0 4393 y Fs(Sequence)14 b(objects)h(may)g(be)g(compared)f(to)h(other)g(objects)g(with)g(the)h (same)f(sequence)g(type.)22 b(The)15 b(comparison)f(uses)i Fq(le)n(xico)o(gr)o(aphical)0 4493 y Fs(ordering:)31 b(\002rst)25 b(the)g(\002rst)g(tw)o(o)f(items)h(are)f(compared,)f(and)h (if)h(the)o(y)e(dif)n(fer)g(this)i(determines)e(the)i(outcome)e(of)h (the)g(comparison;)g(if)0 4593 y(the)o(y)j(are)g(equal,)h(the)f(ne)o (xt)g(tw)o(o)g(items)h(are)f(compared,)g(and)f(so)i(on,)g(until)f (either)g(sequence)f(is)j(e)o(xhausted.)44 b(If)27 b(tw)o(o)h(items)f (to)h(be)0 4692 y(compared)19 b(are)i(themselv)o(es)f(sequences)g(of)h (the)g(same)g(type,)f(the)h(le)o(xicographical)e(comparison)g(is)i (carried)f(out)h(recursi)n(v)o(ely)-5 b(.)25 b(If)c(all)0 4792 y(items)f(of)g(tw)o(o)g(sequences)f(compare)f(equal,)h(the)g (sequences)g(are)h(considered)e(equal.)24 b(If)c(one)f(sequence)f(is)j (an)f(initial)g(sub-sequence)0 4891 y(of)30 b(the)f(other)m(,)i(the)f (shorter)f(sequence)g(is)h(the)g(smaller)g(\(lesser\))g(one.)53 b(Le)o(xicographical)26 b(ordering)i(for)h(strings)h(uses)g(the)i Fm(A)t(S)t(C)t(I)t(I)0 4991 y Fs(ordering)18 b(for)i(indi)n(vidual)e (characters.)24 b(Some)c(e)o(xamples)f(of)h(comparisons)e(between)i (sequences)f(of)h(the)g(same)g(type:)p 0 5549 3901 4 v 0 5649 a Fo(38)2768 b(Chapter)23 b(5.)52 b(Data)23 b(Str)q(uctures)p eop end %%Page: 39 47 TeXDict begin 39 46 bop 236 174 a Fk(\(1,)45 b(2,)f(3\))627 b(<)45 b(\(1,)f(2,)h(4\))236 266 y([1,)g(2,)f(3])627 b(<)45 b([1,)f(2,)h(4])236 357 y('ABC')f(<)h('C')f(<)h('Pascal')e(<)i ('Python')236 448 y(\(1,)g(2,)f(3,)h(4\))492 b(<)45 b(\(1,)f(2,)h(4\)) 236 540 y(\(1,)g(2\))761 b(<)45 b(\(1,)f(2,)h(-1\))236 631 y(\(1,)g(2,)f(3\))583 b(==)44 b(\(1.0,)g(2.0,)g(3.0\))236 722 y(\(1,)h(2,)f(\('aa',)g('ab'\)\))133 b(<)45 b(\(1,)f(2,)h(\('abc',) e('a'\),)h(4\))0 1008 y Fs(Note)30 b(that)h(comparing)d(objects)i(of)g (dif)n(ferent)e(types)i(is)h(le)o(gal.)55 b(The)30 b(outcome)f(is)i (deterministic)e(b)n(ut)h(arbitrary:)44 b(the)31 b(types)f(are)0 1108 y(ordered)18 b(by)h(their)g(name.)24 b(Thus,)19 b(a)h(list)h(is)f(al)o(w)o(ays)g(smaller)g(than)f(a)h(string,)f(a)h (string)f(is)i(al)o(w)o(ays)f(smaller)g(than)f(a)h(tuple,)f(etc.)3626 1078 y Fi(1)3684 1108 y Fs(Mix)o(ed)0 1208 y(numeric)g(types)h(are)g (compared)e(according)g(to)j(their)e(numeric)g(v)n(alue,)h(so)g(0)g (equals)g(0.0,)g(etc.)p 0 5321 1560 4 v 90 5376 a Fh(1)120 5400 y Fm(The)c(rules)i(for)f(comparing)i(objects)g(of)e(dif)n(ferent)i (types)f(should)g(not)f(be)h(relied)g(upon;)g(the)o(y)g(may)f(change)i (in)e(a)g(future)h(v)o(ersion)h(of)e(the)g(language.)p 0 5549 3901 4 v 0 5649 a Fo(5.8.)52 b(Compar)q(ing)24 b(Sequences)g(and)f(Other)g(T)-10 b(ypes)2140 b(39)p eop end %%Page: 40 48 TeXDict begin 40 47 bop 0 5549 3901 4 v 0 5649 a Fo(40)p eop end %%Page: 41 49 TeXDict begin 41 48 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3707 427 y Fn(SIX)p 0 515 V 3062 978 a Fy(Modules)0 1457 y Fs(If)21 b(you)g(quit)h(from)e(the)i(Python)e(interpreter)g(and)h (enter)g(it)i(again,)d(the)i(de\002nitions)f(you)f(ha)n(v)o(e)h(made)g (\(functions)f(and)h(v)n(ariables\))g(are)0 1556 y(lost.)33 b(Therefore,)20 b(if)j(you)f(w)o(ant)g(to)h(write)g(a)g(some)n(what)f (longer)f(program,)f(you)i(are)h(better)f(of)n(f)g(using)g(a)h(te)o(xt) f(editor)g(to)h(prepare)e(the)0 1656 y(input)27 b(for)g(the)h (interpreter)e(and)h(running)e(it)k(with)e(that)h(\002le)g(as)h(input)e (instead.)47 b(This)28 b(is)g(kno)n(wn)e(as)j(creating)d(a)i Fq(script)p Fs(.)48 b(As)29 b(your)0 1756 y(program)23 b(gets)j(longer)m(,)f(you)f(may)h(w)o(ant)g(to)h(split)g(it)g(into)f (se)n(v)o(eral)f(\002les)j(for)d(easier)i(maintenance.)38 b(Y)-9 b(ou)25 b(may)g(also)h(w)o(ant)f(to)h(use)f(a)0 1855 y(handy)19 b(function)f(that)i(you')l(v)o(e)e(written)i(in)h(se)n (v)o(eral)e(programs)f(without)i(cop)o(ying)e(its)k(de\002nition)d (into)h(each)f(program.)0 2002 y(T)-7 b(o)19 b(support)f(this,)i (Python)d(has)j(a)f(w)o(ay)g(to)h(put)e(de\002nitions)h(in)g(a)g (\002le)h(and)e(use)i(them)e(in)i(a)f(script)g(or)g(in)g(an)g (interacti)n(v)o(e)f(instance)h(of)g(the)0 2102 y(interpreter)-5 b(.)34 b(Such)23 b(a)i(\002le)f(is)h(called)e(a)h Fq(module)p Fs(;)h(de\002nitions)e(from)f(a)i(module)f(can)g(be)h Fq(imported)f Fs(into)g(other)g(modules)g(or)g(into)h(the)0 2201 y Fq(main)d Fs(module)g(\(the)g(collection)f(of)i(v)n(ariables)f (that)g(you)g(ha)n(v)o(e)g(access)h(to)g(in)g(a)g(script)g(e)o(x)o (ecuted)d(at)k(the)e(top)h(le)n(v)o(el)f(and)g(in)h(calculator)0 2301 y(mode\).)0 2448 y(A)27 b(module)e(is)i(a)g(\002le)g(containing)d (Python)h(de\002nitions)h(and)g(statements.)43 b(The)26 b(\002le)h(name)f(is)h(the)f(module)f(name)h(with)h(the)f(suf)n(\002x)0 2547 y(`)p Ft(.p)n(y)p Fs(')j(appended.)50 b(W)m(ithin)29 b(a)h(module,)g(the)g(module')-5 b(s)28 b(name)h(\(as)h(a)f(string\))g (is)h(a)n(v)n(ailable)f(as)h(the)g(v)n(alue)f(of)g(the)g(global)g(v)n (ariable)0 2647 y Fp(__name__)p Fs(.)d(F)o(or)21 b(instance,)f(use)i (your)d(f)o(a)n(v)n(orite)i(te)o(xt)g(editor)f(to)h(create)g(a)g (\002le)h(called)f(`)p Ft(\002bo)m(.p)n(y)p Fs(')e(in)j(the)f(current)e (directory)g(with)j(the)0 2747 y(follo)n(wing)d(contents:)236 2894 y Fk(#)45 b(Fibonacci)e(numbers)h(module)236 3076 y(def)h(fib\(n\):)178 b(#)44 b(write)g(Fibonacci)g(series)f(up)i(to)f (n)416 3167 y(a,)g(b)h(=)f(0,)h(1)416 3259 y(while)f(b)g(<)h(n:)595 3350 y(print)f(b,)595 3441 y(a,)g(b)h(=)g(b,)f(a+b)236 3624 y(def)h(fib2\(n\):)e(#)i(return)f(Fibonacci)f(series)h(up)g(to)h (n)416 3715 y(result)e(=)i([])416 3807 y(a,)f(b)h(=)f(0,)h(1)416 3898 y(while)f(b)g(<)h(n:)595 3989 y(result.append\(b\))595 4081 y(a,)f(b)h(=)g(b,)f(a+b)416 4172 y(return)f(result)0 4458 y Fs(No)n(w)20 b(enter)g(the)g(Python)f(interpreter)g(and)g (import)g(this)i(module)e(with)h(the)g(follo)n(wing)f(command:)236 4605 y Fk(>>>)45 b(import)e(fibo)0 4891 y Fs(This)21 b(does)g(not)f(enter)g(the)h(names)g(of)f(the)h(functions)f(de\002ned)f (in)i Fp(fibo)g Fs(directly)f(in)h(the)g(current)e(symbol)h(table;)h (it)h(only)e(enters)h(the)0 4991 y(module)e(name)g Fp(fibo)h Fs(there.)25 b(Using)20 b(the)g(module)f(name)h(you)f(can)h(access)h (the)f(functions:)p 0 5549 3901 4 v 3808 5649 a Fo(41)p eop end %%Page: 42 50 TeXDict begin 42 49 bop 236 174 a Fk(>>>)45 b(fibo.fib\(1000\))236 266 y(1)g(1)g(2)f(3)h(5)g(8)f(13)h(21)f(34)h(55)f(89)h(144)f(233)g(377) h(610)f(987)236 357 y(>>>)h(fibo.fib2\(100\))236 448 y([1,)g(1,)f(2,)h(3,)f(5,)h(8,)f(13,)g(21,)h(34,)f(55,)g(89])236 540 y(>>>)h(fibo.__name__)236 631 y('fibo')0 917 y Fs(If)20 b(you)f(intend)h(to)g(use)g(a)h(function)d(often)i(you)f(can)h(assign)g (it)h(to)g(a)f(local)g(name:)236 1155 y Fk(>>>)45 b(fib)f(=)h(fibo.fib) 236 1247 y(>>>)g(fib\(500\))236 1338 y(1)g(1)g(2)f(3)h(5)g(8)f(13)h(21) f(34)h(55)f(89)h(144)f(233)g(377)0 1784 y Fl(6.1)121 b(More)33 b(on)h(Modules)0 2017 y Fs(A)19 b(module)e(can)h(contain)f(e) o(x)o(ecutable)f(statements)j(as)g(well)g(as)g(function)d (de\002nitions.)24 b(These)18 b(statements)g(are)g(intended)f(to)h (initialize)0 2116 y(the)i(module.)k(The)o(y)19 b(are)h(e)o(x)o(ecuted) e(only)i(the)g Fq(\002r)o(st)h Fs(time)f(the)h(module)d(is)k(imported)c (some)n(where.)2839 2086 y Fi(1)0 2263 y Fs(Each)25 b(module)f(has)i (its)h(o)n(wn)e(pri)n(v)n(ate)f(symbol)h(table,)h(which)f(is)i(used)e (as)h(the)g(global)e(symbol)h(table)h(by)f(all)h(functions)e(de\002ned) g(in)0 2363 y(the)g(module.)34 b(Thus,)24 b(the)f(author)g(of)g(a)h (module)e(can)i(use)g(global)e(v)n(ariables)h(in)h(the)g(module)e (without)h(w)o(orrying)f(about)g(accidental)0 2462 y(clashes)h(with)g (a)g(user')-5 b(s)23 b(global)f(v)n(ariables.)32 b(On)23 b(the)g(other)f(hand,)g(if)h(you)f(kno)n(w)f(what)i(you)f(are)h(doing)e (you)h(can)h(touch)e(a)j(module')-5 b(s)0 2562 y(global)19 b(v)n(ariables)h(with)g(the)g(same)h(notation)e(used)g(to)i(refer)e(to) i(its)g(functions,)d Fp(modname.itemname)p Fs(.)0 2709 y(Modules)i(can)h(import)f(other)g(modules.)27 b(It)21 b(is)h(customary)e(b)n(ut)h(not)g(required)e(to)i(place)g(all)h Fp(import)e Fs(statements)h(at)h(the)f(be)o(ginning)0 2808 y(of)28 b(a)h(module)e(\(or)h(script,)j(for)d(that)g(matter\).)49 b(The)29 b(imported)d(module)i(names)g(are)g(placed)g(in)h(the)f (importing)f(module')-5 b(s)27 b(global)0 2908 y(symbol)19 b(table.)0 3055 y(There)k(is)i(a)g(v)n(ariant)e(of)h(the)g Fp(import)g Fs(statement)f(that)i(imports)e(names)h(from)f(a)h(module)f (directly)g(into)h(the)g(importing)f(module')-5 b(s)0 3155 y(symbol)19 b(table.)25 b(F)o(or)20 b(e)o(xample:)236 3393 y Fk(>>>)45 b(from)f(fibo)g(import)g(fib,)g(fib2)236 3484 y(>>>)h(fib\(500\))236 3575 y(1)g(1)g(2)f(3)h(5)g(8)f(13)h(21)f (34)h(55)f(89)h(144)f(233)g(377)0 3862 y Fs(This)16 b(does)g(not)g (introduce)e(the)i(module)f(name)g(from)g(which)h(the)g(imports)f(are)h (tak)o(en)g(in)g(the)g(local)g(symbol)f(table)h(\(so)g(in)g(the)g(e)o (xample,)0 3961 y Fp(fibo)k Fs(is)h(not)f(de\002ned\).)0 4108 y(There)f(is)i(e)n(v)o(en)f(a)g(v)n(ariant)g(to)g(import)f(all)i (names)f(that)g(a)h(module)d(de\002nes:)236 4346 y Fk(>>>)45 b(from)f(fibo)g(import)1178 4359 y(*)236 4438 y(>>>)h(fib\(500\))236 4529 y(1)g(1)g(2)f(3)h(5)g(8)f(13)h(21)f(34)h(55)f(89)h(144)f(233)g (377)0 4815 y Fs(This)20 b(imports)g(all)h(names)e(e)o(xcept)h(those)g (be)o(ginning)d(with)k(an)f(underscore)e(\()p Fp(_)p Fs(\).)p 0 4886 1560 4 v 90 4942 a Fh(1)120 4965 y Fm(In)d(f)o(act)h (function)h(de\002nitions)g(are)f(also)g(`statements')h(that)f(are)g (`e)o(x)o(ecuted';)j(the)d(e)o(x)o(ecution)i(enters)e(the)g(function)h (name)e(in)g(the)h(module')l(s)g(global)h(symbol)e(table.)p 0 5549 3901 4 v 0 5649 a Fo(42)3032 b(Chapter)23 b(6.)52 b(Modules)p eop end %%Page: 43 51 TeXDict begin 43 50 bop 0 83 a Fj(6.1.1)100 b(Ex)m(ecuting)29 b(modules)h(as)d(scr)q(ipts)0 286 y Fs(When)20 b(you)f(run)h(a)g (Python)f(module)g(with)236 524 y Fk(python)44 b(fibo.py)g() 0 810 y Fs(the)18 b(code)f(in)h(the)g(module)f(will)h(be)g(e)o(x)o (ecuted,)e(just)j(as)f(if)g(you)f(imported)g(it,)h(b)n(ut)g(with)g(the) g Fp(__name__)f Fs(set)i(to)f Fp("__main__")p Fs(.)23 b(That)0 910 y(means)d(that)g(by)g(adding)f(this)h(code)g(at)h(the)f (end)f(of)h(your)f(module:)236 1057 y Fk(if)45 b(__name__)e(==)i ("__main__":)416 1148 y(import)e(sys)416 1239 y (fib\(int\(sys.argv[1]\)\))0 1526 y Fs(you)22 b(can)h(mak)o(e)g(the)g (\002le)h(usable)f(as)h(a)g(script)f(as)h(well)g(as)f(an)h(importable)d (module,)i(because)f(the)h(code)g(that)g(parses)g(the)h(command)0 1625 y(line)c(only)g(runs)f(if)i(the)f(module)f(is)i(e)o(x)o(ecuted)d (as)j(the)f(\223main\224)g(\002le:)236 1772 y Fk($)45 b(python)f(fibo.py)f(50)236 1863 y(1)i(1)g(2)f(3)h(5)g(8)f(13)h(21)f (34)0 2150 y Fs(If)20 b(the)g(module)f(is)i(imported,)e(the)h(code)f (is)i(not)f(run:)236 2297 y Fk(>>>)45 b(import)e(fibo)236 2388 y(>>>)0 2674 y Fs(This)17 b(is)h(often)f(used)f(either)h(to)g(pro) o(vide)e(a)j(con)m(v)o(enient)c(user)j(interf)o(ace)f(to)i(a)f(module,) f(or)h(for)f(testing)h(purposes)f(\(running)f(the)i(module)0 2774 y(as)k(a)g(script)f(e)o(x)o(ecutes)f(a)h(test)i(suite\).)0 3059 y Fj(6.1.2)100 b(The)29 b(Module)g(Search)g(P)l(ath)0 3261 y Fs(When)20 b(a)h(module)e(named)h Fp(spam)g Fs(is)h(imported,)e (the)h(interpreter)f(searches)h(for)g(a)h(\002le)g(named)e(`)p Ft(spam.p)n(y)p Fs(')h(in)g(the)h(current)e(directory)-5 b(,)0 3361 y(and)22 b(then)g(in)h(the)f(list)i(of)e(directories)f (speci\002ed)i(by)f(the)g(en)m(vironment)e(v)n(ariable)h(PYTHONP)-8 b(A)f(TH.)22 b(This)h(has)g(the)f(same)h(syntax)f(as)0 3461 y(the)17 b(shell)g(v)n(ariable)f(P)-8 b(A)f(TH,)17 b(that)g(is,)i(a)e(list)h(of)f(directory)e(names.)24 b(When)16 b(PYTHONP)-8 b(A)f(TH)17 b(is)h(not)f(set,)h(or)f(when)f(the) h(\002le)h(is)g(not)e(found)0 3560 y(there,)j(the)i(search)e(continues) g(in)i(an)f(installation-dependent)d(def)o(ault)i(path;)h(on)i(U)t Fm(N)t(I)t(X)r Fs(,)f(this)f(is)h(usually)f(`)p Ft(.:/usr/local/lib/p)n (ython)o Fs('.)0 3707 y(Actually)-5 b(,)18 b(modules)g(are)h(searched)g (in)g(the)g(list)h(of)f(directories)g(gi)n(v)o(en)e(by)i(the)g(v)n (ariable)g Fp(sys.path)f Fs(which)g(is)j(initialized)d(from)h(the)0 3807 y(directory)f(containing)g(the)i(input)f(script)h(\(or)f(the)h (current)e(directory\),)g(PYTHONP)-8 b(A)f(TH)20 b(and)f(the)h (installation-dependent)d(def)o(ault.)0 3906 y(This)25 b(allo)n(ws)h(Python)d(programs)h(that)h(kno)n(w)f(what)h(the)o(y')l (re)e(doing)h(to)h(modify)f(or)h(replace)f(the)h(module)f(search)g (path.)39 b(Note)25 b(that)0 4006 y(because)d(the)h(directory)e (containing)g(the)i(script)g(being)f(run)g(is)i(on)e(the)h(search)g (path,)f(it)i(is)g(important)d(that)i(the)g(script)g(not)f(ha)n(v)o(e)h (the)0 4106 y(same)d(name)g(as)g(a)h(standard)e(module,)f(or)i(Python)f (will)h(attempt)g(to)g(load)g(the)g(script)g(as)g(a)h(module)d(when)i (that)g(module)f(is)h(imported.)0 4205 y(This)g(will)h(generally)e(be)h (an)g(error)-5 b(.)24 b(See)d(section)f(6.2,)f(\223Standard)g(Modules,) -6 b(\224)19 b(for)h(more)f(information.)0 4490 y Fj(6.1.3)100 b(\223Compiled\224)30 b(Python)f(\002les)0 4693 y Fs(As)e(an)e (important)f(speed-up)h(of)g(the)h(start-up)f(time)h(for)f(short)g (programs)f(that)i(use)g(a)g(lot)g(of)g(standard)e(modules,)i(if)g(a)g (\002le)h(called)0 4793 y(`)p Ft(spam.p)n(yc)p Fs(')e(e)o(xists)h(in)f (the)h(directory)d(where)i(`)p Ft(spam.p)n(y)p Fs(')g(is)h(found,)f (this)h(is)g(assumed)f(to)h(contain)e(an)i (already-\223byte-compiled\224)0 4892 y(v)o(ersion)18 b(of)i(the)f(module)g Fp(spam)p Fs(.)24 b(The)19 b(modi\002cation)f (time)i(of)g(the)f(v)o(ersion)g(of)g(`)p Ft(spam.p)n(y)p Fs(')g(used)g(to)h(create)f(`)p Ft(spam.p)n(yc)p Fs(')g(is)i(recorded)0 4992 y(in)f(`)p Ft(spam.p)n(yc)p Fs(',)g(and)f(the)h(`)p Ft(.p)n(yc)p Fs(')g(\002le)h(is)g(ignored)e(if)h(these)h(don')o(t)d (match.)0 5139 y(Normally)-5 b(,)19 b(you)h(don')o(t)f(need)h(to)h(do)f (an)o(ything)f(to)h(create)h(the)g(`)p Ft(spam.p)n(yc)p Fs(')f(\002le.)27 b(Whene)n(v)o(er)19 b(`)p Ft(spam.p)n(y)p Fs(')h(is)h(successfully)f(compiled,)0 5239 y(an)g(attempt)f(is)h(made) g(to)f(write)h(the)g(compiled)e(v)o(ersion)g(to)i(`)p Ft(spam.p)n(yc)p Fs('.)k(It)c(is)h(not)e(an)h(error)e(if)i(this)h (attempt)e(f)o(ails;)h(if)g(for)f(an)o(y)g(reason)0 5338 y(the)g(\002le)g(is)g(not)g(written)f(completely)-5 b(,)17 b(the)h(resulting)g(`)p Ft(spam.p)n(yc)p Fs(')g(\002le)h(will)g(be)g (recognized)d(as)j(in)m(v)n(alid)f(and)g(thus)g(ignored)f(later)-5 b(.)25 b(The)p 0 5549 3901 4 v 0 5649 a Fo(6.1.)52 b(More)23 b(on)h(Modules)2977 b(43)p eop end %%Page: 44 52 TeXDict begin 44 51 bop 0 83 a Fs(contents)20 b(of)h(the)g(`)p Ft(spam.p)n(yc)p Fs(')g(\002le)g(are)g(platform)f(independent,)e(so)k (a)f(Python)f(module)g(directory)f(can)i(be)g(shared)f(by)h(machines)f (of)0 183 y(dif)n(ferent)f(architectures.)0 330 y(Some)h(tips)h(for)e (e)o(xperts:)137 559 y(\225)42 b(When)21 b(the)h(Python)e(interpreter)h (is)h(in)m(v)n(ok)o(ed)e(with)i(the)g Fr(-O)f Fs(\003ag,)h(optimized)f (code)g(is)h(generated)e(and)i(stored)f(in)h(`)p Ft(.p)n(y)o(o)p Fs(')e(\002les.)208 659 y(The)d(optimizer)g(currently)f(doesn')o(t)g (help)i(much;)g(it)g(only)f(remo)o(v)o(es)f Fp(assert)i Fs(statements.)24 b(When)18 b Fr(-O)f Fs(is)i(used,)f Fq(all)g Fs(bytecode)208 759 y(is)j(optimized;)e Fp(.pyc)h Fs(\002les)h(are)f(ignored)e(and)i Fp(.py)g Fs(\002les)h(are)f (compiled)f(to)h(optimized)f(bytecode.)137 925 y(\225)42 b(P)o(assing)19 b(tw)o(o)g Fr(-O)g Fs(\003ags)g(to)h(the)f(Python)f (interpreter)f(\()p Fr(-OO)p Fs(\))h(will)h(cause)h(the)f(bytecode)e (compiler)h(to)h(perform)e(optimizations)208 1024 y(that)j(could)g(in)h (some)f(rare)g(cases)i(result)e(in)h(malfunctioning)d(programs.)24 b(Currently)19 b(only)h Fp(__doc__)g Fs(strings)g(are)h(remo)o(v)o(ed) 208 1124 y(from)29 b(the)i(bytecode,)g(resulting)f(in)g(more)g(compact) g(`)p Ft(.p)n(y)o(o)p Fs(')f(\002les.)57 b(Since)30 b(some)h(programs)d (may)i(rely)h(on)f(ha)n(ving)f(these)208 1224 y(a)n(v)n(ailable,)19 b(you)g(should)g(only)h(use)g(this)h(option)e(if)h(you)g(kno)n(w)f (what)h(you')l(re)e(doing.)137 1390 y(\225)42 b(A)19 b(program)e(doesn')o(t)g(run)h(an)o(y)g(f)o(aster)h(when)g(it)g(is)h (read)f(from)e(a)j(`)p Ft(.p)n(yc)p Fs(')e(or)h(`)p Ft(.p)n(y)o(o)p Fs(')e(\002le)j(than)e(when)h(it)g(is)h(read)e(from)g(a)i(`)p Ft(.p)n(y)p Fs(')e(\002le;)208 1489 y(the)i(only)f(thing)h(that')-5 b(s)20 b(f)o(aster)h(about)e(`)p Ft(.p)n(yc)p Fs(')h(or)g(`)p Ft(.p)n(y)o(o)p Fs(')e(\002les)j(is)g(the)g(speed)e(with)i(which)e(the) o(y)h(are)g(loaded.)137 1655 y(\225)42 b(When)23 b(a)i(script)f(is)h (run)e(by)h(gi)n(ving)e(its)j(name)f(on)f(the)h(command)e(line,)j(the)f (bytecode)e(for)i(the)g(script)g(is)h(ne)n(v)o(er)d(written)i(to)g(a) 208 1755 y(`)p Ft(.p)n(yc)p Fs(')e(or)h(`)p Ft(.p)n(y)o(o)p Fs(')e(\002le.)33 b(Thus,)23 b(the)g(startup)f(time)h(of)g(a)g(script)g (may)f(be)h(reduced)e(by)i(mo)o(ving)d(most)j(of)g(its)h(code)e(to)h(a) g(module)208 1855 y(and)g(ha)n(ving)h(a)h(small)g(bootstrap)e(script)h (that)h(imports)e(that)i(module.)36 b(It)25 b(is)h(also)e(possible)g (to)h(name)f(a)h(`)p Ft(.p)n(yc)p Fs(')f(or)g(`)p Ft(.p)n(y)o(o)p Fs(')f(\002le)208 1954 y(directly)c(on)h(the)g(command)e(line.)137 2120 y(\225)42 b(It)23 b(is)h(possible)f(to)g(ha)n(v)o(e)f(a)i(\002le)f (called)g(`)p Ft(spam.p)n(yc)p Fs(')g(\(or)f(`)p Ft(spam.p)n(y)o(o)p Fs(')f(when)h Fr(-O)h Fs(is)h(used\))f(without)f(a)h(\002le)h(`)p Ft(spam.p)n(y)p Fs(')e(for)h(the)208 2220 y(same)18 b(module.)23 b(This)18 b(can)g(be)g(used)g(to)g(distrib)n(ute)g(a)g(library)f(of)h (Python)f(code)g(in)h(a)h(form)e(that)h(is)h(moderately)d(hard)h(to)i (re)n(v)o(erse)208 2319 y(engineer)-5 b(.)137 2486 y(\225)42 b(The)18 b(module)f Fp(compileall)g Fs(can)h(create)g(`)p Ft(.p)n(yc)p Fs(')g(\002les)i(\(or)e(`)p Ft(.p)n(y)o(o)p Fs(')e(\002les)k(when)e Fr(-O)g Fs(is)h(used\))f(for)g(all)h(modules)e (in)i(a)g(directory)-5 b(.)0 2813 y Fl(6.2)121 b(Standard)35 b(Modules)0 3046 y Fs(Python)25 b(comes)i(with)f(a)h(library)f(of)g (standard)f(modules,)i(described)e(in)i(a)g(separate)f(document,)g(the) g Fq(Python)g(Libr)o(ary)g(Refer)m(ence)0 3145 y Fs(\(\223Library)14 b(Reference\224)h(hereafter\).)22 b(Some)16 b(modules)f(are)h(b)n(uilt) g(into)g(the)g(interpreter;)g(these)h(pro)o(vide)d(access)i(to)h (operations)d(that)j(are)0 3245 y(not)j(part)h(of)f(the)h(core)f(of)g (the)h(language)e(b)n(ut)i(are)f(ne)n(v)o(ertheless)f(b)n(uilt)i(in,)g (either)f(for)g(ef)n(\002cienc)o(y)g(or)g(to)h(pro)o(vide)d(access)k (to)e(operating)0 3345 y(system)j(primiti)n(v)o(es)e(such)h(as)h (system)g(calls.)32 b(The)23 b(set)g(of)f(such)g(modules)g(is)h(a)g (con\002guration)d(option)h(which)h(also)g(depends)g(on)g(the)0 3444 y(underlying)g(platform)h(F)o(or)h(e)o(xample,)g(the)g Fp(amoeba)g Fs(module)f(is)i(only)f(pro)o(vided)e(on)i(systems)h(that)f (someho)n(w)g(support)f(Amoeba)0 3544 y(primiti)n(v)o(es.)42 b(One)27 b(particular)e(module)g(deserv)o(es)g(some)h(attention:)37 b Fp(sys)p Fs(,)28 b(which)e(is)h(b)n(uilt)f(into)g(e)n(v)o(ery)f (Python)h(interpreter)-5 b(.)42 b(The)0 3644 y(v)n(ariables)19 b Fp(sys.ps1)h Fs(and)f Fp(sys.ps2)h Fs(de\002ne)g(the)g(strings)g (used)g(as)h(primary)d(and)i(secondary)e(prompts:)236 3882 y Fk(>>>)45 b(import)e(sys)236 3973 y(>>>)i(sys.ps1)236 4064 y('>>>)f(')236 4156 y(>>>)h(sys.ps2)236 4247 y('...)f(')236 4338 y(>>>)h(sys.ps1)e(=)i('C>)f(')236 4430 y(C>)h(print)f('Yuck!')236 4521 y(Yuck!)236 4612 y(C>)0 4990 y Fs(These)20 b(tw)o(o)g(v)n (ariables)g(are)g(only)f(de\002ned)h(if)g(the)g(interpreter)f(is)i(in)f (interacti)n(v)o(e)f(mode.)0 5137 y(The)k(v)n(ariable)f Fp(sys.path)g Fs(is)i(a)g(list)g(of)f(strings)h(that)f(determines)f (the)h(interpreter')-5 b(s)22 b(search)h(path)g(for)f(modules.)33 b(It)24 b(is)g(initialized)0 5236 y(to)d(a)g(def)o(ault)f(path)g(tak)o (en)h(from)e(the)i(en)m(vironment)d(v)n(ariable)i(PYTHONP)-8 b(A)f(TH,)20 b(or)h(from)e(a)j(b)n(uilt-in)e(def)o(ault)g(if)h(PYTHONP) -8 b(A)f(TH)21 b(is)0 5336 y(not)f(set.)26 b(Y)-9 b(ou)19 b(can)h(modify)f(it)i(using)e(standard)g(list)j(operations:)p 0 5549 3901 4 v 0 5649 a Fo(44)3032 b(Chapter)23 b(6.)52 b(Modules)p eop end %%Page: 45 53 TeXDict begin 45 52 bop 236 174 a Fk(>>>)45 b(import)e(sys)236 266 y(>>>)i(sys.path.append\('/ufs/guido/lib/pyth)o(on'\))0 712 y Fl(6.3)121 b(The)34 b Ff(dir\(\))e Fl(Function)0 944 y Fs(The)20 b(b)n(uilt-in)f(function)g Fp(dir\(\))h Fs(is)h(used)f(to)g(\002nd)g(out)g(which)g(names)f(a)i(module)e (de\002nes.)25 b(It)20 b(returns)f(a)i(sorted)f(list)h(of)f(strings:) 236 1182 y Fk(>>>)45 b(import)e(fibo,)h(sys)236 1274 y(>>>)h(dir\(fibo\))236 1365 y(['__name__',)e('fib',)h('fib2'])236 1456 y(>>>)h(dir\(sys\))236 1548 y(['__displayhook__',)d('__doc__',)h ('__excepthook__',)f('__name__',)h('__stderr__',)281 1639 y('__stdin__',)g('__stdout__',)g('_getframe',)f('api_version',)h ('argv',)281 1730 y('builtin_module_names',)e('byteorder',)i ('callstats',)g('copyright',)281 1822 y('displayhook',)g('exc_clear',)f ('exc_info',)h('exc_type',)g('excepthook',)281 1913 y('exec_prefix',)g ('executable',)f('exit',)i('getdefaultencoding',)d('getdlopenflags',) 281 2004 y('getrecursionlimit',)h('getrefcount',)g('hexversion',)h ('maxint',)g('maxunicode',)281 2096 y('meta_path',)g('modules',)g ('path',)h('path_hooks',)e('path_importer_cache',)281 2187 y('platform',)h('prefix',)g('ps1',)h('ps2',)g('setcheckinterval',) e('setdlopenflags',)281 2278 y('setprofile',)h('setrecursionlimit',)e ('settrace',)i('stderr',)g('stdin',)h('stdout',)281 2370 y('version',)f('version_info',)g('warnoptions'])0 2656 y Fs(W)m(ithout)20 b(ar)o(guments,)d Fp(dir\(\))j Fs(lists)i(the)e (names)g(you)f(ha)n(v)o(e)h(de\002ned)f(currently:)236 2894 y Fk(>>>)45 b(a)f(=)h([1,)f(2,)h(3,)f(4,)h(5])236 2985 y(>>>)g(import)e(fibo)236 3077 y(>>>)i(fib)f(=)h(fibo.fib)236 3168 y(>>>)g(dir\(\))236 3259 y(['__builtins__',)d('__doc__',)i ('__file__',)e('__name__',)h('a',)i('fib',)e('fibo',)h('sys'])0 3546 y Fs(Note)20 b(that)g(it)h(lists)h(all)f(types)f(of)g(names:)25 b(v)n(ariables,)19 b(modules,)g(functions,)f(etc.)0 3692 y Fp(dir\(\))k Fs(does)h(not)f(list)i(the)e(names)h(of)f(b)n(uilt-in)g (functions)f(and)h(v)n(ariables.)32 b(If)22 b(you)g(w)o(ant)h(a)g(list) g(of)g(those,)g(the)o(y)f(are)g(de\002ned)g(in)h(the)0 3792 y(standard)c(module)g Fp(__builtin__)p Fs(:)p 0 5549 3901 4 v 0 5649 a Fo(6.3.)52 b(The)23 b Fp(dir\(\))f Fo(Function)2862 b(45)p eop end %%Page: 46 54 TeXDict begin 46 53 bop 236 174 a Fk(>>>)45 b(import)e(__builtin__)236 266 y(>>>)i(dir\(__builtin__\))236 357 y(['ArithmeticError',)d ('AssertionError',)g('AttributeError',)g('DeprecationWarning',)281 448 y('EOFError',)h('Ellipsis',)g('EnvironmentError',)f('Exception',)g ('False',)281 540 y('FloatingPointError',)f('FutureWarning',)h ('IOError',)i('ImportError',)281 631 y('IndentationError',)e ('IndexError',)g('KeyError',)h('KeyboardInterrupt',)281 722 y('LookupError',)g('MemoryError',)f('NameError',)h('None',)g ('NotImplemented',)281 814 y('NotImplementedError',)e('OSError',)i ('OverflowError',)281 905 y('PendingDeprecationWarning',)d ('ReferenceError',)i('RuntimeError',)281 996 y('RuntimeWarning',)g ('StandardError',)g('StopIteration',)g('SyntaxError',)281 1088 y('SyntaxWarning',)g('SystemError',)h('SystemExit',)f('TabError',) h('True',)281 1179 y('TypeError',)g('UnboundLocalError',)e ('UnicodeDecodeError',)281 1270 y('UnicodeEncodeError',)g ('UnicodeError',)i('UnicodeTranslateError',)281 1362 y('UserWarning',)g('ValueError',)f('Warning',)h('WindowsError',)281 1453 y('ZeroDivisionError',)f('_',)i('__debug__',)e('__doc__',)i ('__import__',)281 1544 y('__name__',)f('abs',)h('apply',)f ('basestring',)g('bool',)h('buffer',)281 1636 y('callable',)f('chr',)h ('classmethod',)e('cmp',)i('coerce',)f('compile',)281 1727 y('complex',)g('copyright',)g('credits',)g('delattr',)g('dict',)h ('dir',)g('divmod',)281 1818 y('enumerate',)f('eval',)h('execfile',)f ('exit',)g('file',)h('filter',)f('float',)281 1910 y('frozenset',)g ('getattr',)g('globals',)g('hasattr',)g('hash',)h('help',)g('hex',)281 2001 y('id',)g('input',)g('int',)f('intern',)h('isinstance',)e ('issubclass',)h('iter',)281 2092 y('len',)h('license',)f('list',)h ('locals',)f('long',)h('map',)f('max',)h('min',)281 2183 y('object',)f('oct',)h('open',)g('ord',)g('pow',)f('property',)g ('quit',)h('range',)281 2275 y('raw_input',)f('reduce',)g('reload',)h ('repr',)f('reversed',)g('round',)h('set',)281 2366 y('setattr',)f ('slice',)h('sorted',)f('staticmethod',)f('str',)i('sum',)g('super',) 281 2457 y('tuple',)g('type',)f('unichr',)h('unicode',)f('vars',)g ('xrange',)h('zip'])0 2903 y Fl(6.4)121 b(P)-5 b(ac)n(kages)0 3136 y Fs(P)o(ackages)25 b(are)h(a)g(w)o(ay)g(of)g(structuring)e (Python')-5 b(s)25 b(module)g(namespace)f(by)i(using)f(\223dotted)g (module)g(names\224.)41 b(F)o(or)26 b(e)o(xample,)g(the)0 3236 y(module)20 b(name)g Fp(A.B)h Fs(designates)g(a)g(submodule)f (named)g(`)p Fp(B)p Fs(')g(in)i(a)f(package)f(named)g(`)p Fp(A)p Fs('.)g(Just)i(lik)o(e)g(the)f(use)g(of)g(modules)f(sa)n(v)o(es) i(the)0 3335 y(authors)d(of)g(dif)n(ferent)g(modules)f(from)h(ha)n (ving)g(to)h(w)o(orry)f(about)g(each)g(other')-5 b(s)19 b(global)g(v)n(ariable)g(names,)g(the)h(use)g(of)g(dotted)f(module)0 3435 y(names)j(sa)n(v)o(es)g(the)g(authors)f(of)h(multi-module)d (packages)i(lik)o(e)h(NumPy)g(or)f(the)h(Python)f(Imaging)f(Library)h (from)g(ha)n(ving)f(to)j(w)o(orry)0 3535 y(about)c(each)h(other')-5 b(s)20 b(module)f(names.)0 3681 y(Suppose)h(you)h(w)o(ant)h(to)f (design)g(a)h(collection)f(of)g(modules)f(\(a)i(\223package\224\))d (for)i(the)g(uniform)f(handling)g(of)h(sound)f(\002les)j(and)e(sound)0 3781 y(data.)33 b(There)22 b(are)g(man)o(y)g(dif)n(ferent)f(sound)h (\002le)h(formats)f(\(usually)g(recognized)f(by)h(their)g(e)o (xtension,)g(for)g(e)o(xample:)29 b(`)p Ft(.w)o(a)o(v)p Fs(',)23 b(`)p Ft(.aiff)p Fs(',)0 3881 y(`)p Ft(.au)p Fs('\),)17 b(so)h(you)f(may)h(need)f(to)h(create)g(and)f(maintain)g(a)i (gro)n(wing)d(collection)h(of)h(modules)f(for)g(the)h(con)m(v)o(ersion) d(between)j(the)g(v)n(arious)0 3980 y(\002le)29 b(formats.)49 b(There)27 b(are)i(also)f(man)o(y)f(dif)n(ferent)g(operations)g(you)g (might)h(w)o(ant)h(to)f(perform)f(on)h(sound)f(data)h(\(such)g(as)h (mixing,)0 4080 y(adding)23 b(echo,)h(applying)f(an)h(equalizer)f (function,)g(creating)h(an)g(arti\002cial)g(stereo)h(ef)n(fect\),)f(so) g(in)h(addition)e(you)g(will)i(be)f(writing)g(a)0 4180 y(ne)n(v)o(er)n(-ending)18 b(stream)j(of)g(modules)f(to)i(perform)d (these)i(operations.)27 b(Here')-5 b(s)22 b(a)f(possible)g(structure)g (for)f(your)g(package)g(\(e)o(xpressed)0 4279 y(in)g(terms)h(of)e(a)i (hierarchical)e(\002lesystem\):)p 0 5549 3901 4 v 0 5649 a Fo(46)3032 b(Chapter)23 b(6.)52 b(Modules)p eop end %%Page: 47 55 TeXDict begin 47 54 bop 236 174 a Fk(sound/)1165 b(Top-level)43 b(package)505 266 y(__init__.py)671 b(Initialize)43 b(the)h(sound)g (package)505 357 y(formats/)806 b(Subpackage)43 b(for)h(file)g(format)g (conversions)864 448 y(__init__.py)864 540 y(wavread.py)864 631 y(wavwrite.py)864 722 y(aiffread.py)864 814 y(aiffwrite.py)864 905 y(auread.py)864 996 y(auwrite.py)864 1088 y(...)505 1179 y(effects/)806 b(Subpackage)43 b(for)h(sound)g(effects)864 1270 y(__init__.py)864 1362 y(echo.py)864 1453 y(surround.py)864 1544 y(reverse.py)864 1636 y(...)505 1727 y(filters/)806 b(Subpackage)43 b(for)h(filters)864 1818 y(__init__.py)864 1910 y(equalizer.py)864 2001 y(vocoder.py)864 2092 y(karaoke.py)864 2183 y(...)0 2470 y Fs(When)22 b(importing)e(the)i(package,)f(Python)g (searches)h(through)e(the)i(directories)f(on)h Fp(sys.path)f Fs(looking)f(for)i(the)g(package)e(subdi-)0 2569 y(rectory)-5 b(.)0 2716 y(The)20 b(`)p Ft(__init__.p)n(y)p Fs(')15 b(\002les)21 b(are)f(required)e(to)i(mak)o(e)f(Python)g(treat)h(the)g (directories)f(as)h(containing)e(packages;)h(this)i(is)f(done)f(to)h (pre)n(v)o(ent)0 2816 y(directories)d(with)h(a)g(common)e(name,)i(such) g(as)g(`)p Fp(string)p Fs(',)f(from)g(unintentionally)e(hiding)i(v)n (alid)g(modules)g(that)h(occur)f(later)h(on)g(the)0 2915 y(module)j(search)i(path.)32 b(In)22 b(the)h(simplest)g(case,)g(`)p Ft(__init__.p)n(y)p Fs(')c(can)j(just)i(be)e(an)h(empty)f(\002le,)h(b)n (ut)g(it)g(can)g(also)g(e)o(x)o(ecute)e(initialization)0 3015 y(code)f(for)f(the)h(package)f(or)h(set)h(the)f Fp(__all__)g Fs(v)n(ariable,)e(described)h(later)-5 b(.)0 3162 y(Users)21 b(of)f(the)g(package)f(can)h(import)f(indi)n(vidual)f (modules)h(from)g(the)i(package,)d(for)i(e)o(xample:)236 3400 y Fk(import)44 b(sound.effects.echo)0 3686 y Fs(This)20 b(loads)g(the)h(submodule)d Fp(sound.effects.echo)p Fs(.)k(It)e(must)h (be)f(referenced)e(with)i(its)h(full)f(name.)236 3833 y Fk(sound.effects.echo.echofilter\(input,)39 b(output,)44 b(delay=0.7,)f(atten=4\))0 4119 y Fs(An)20 b(alternati)n(v)o(e)f(w)o (ay)h(of)g(importing)e(the)j(submodule)d(is:)236 4266 y Fk(from)44 b(sound.effects)f(import)h(echo)0 4552 y Fs(This)20 b(also)h(loads)f(the)g(submodule)e Fp(echo)p Fs(,)i(and)g(mak)o(es)g(it)h(a)n(v)n(ailable)e(without)h(its)h(package) e(pre\002x,)g(so)h(it)h(can)f(be)h(used)e(as)i(follo)n(ws:)236 4699 y Fk(echo.echofilter\(input,)41 b(output,)j(delay=0.7,)f (atten=4\))0 4985 y Fs(Y)-8 b(et)20 b(another)f(v)n(ariation)g(is)i(to) f(import)g(the)g(desired)f(function)g(or)h(v)n(ariable)f(directly:)236 5132 y Fk(from)44 b(sound.effects.echo)e(import)i(echofilter)p 0 5549 3901 4 v 0 5649 a Fo(6.4.)52 b(P)m(ac)n(kages)3258 b(47)p eop end %%Page: 48 56 TeXDict begin 48 55 bop 0 83 a Fs(Again,)19 b(this)i(loads)f(the)g (submodule)e Fp(echo)p Fs(,)i(b)n(ut)g(this)h(mak)o(es)f(its)h (function)d Fp(echofilter\(\))h Fs(directly)g(a)n(v)n(ailable:)236 321 y Fk(echofilter\(input,)42 b(output,)i(delay=0.7,)f(atten=4\))0 607 y Fs(Note)30 b(that)f(when)g(using)g Fp(from)49 b Fq(pac)n(ka)o(g)o(e)f Fp(import)h Fq(item)p Fs(,)32 b(the)e(item)g(can) f(be)h(either)f(a)h(submodule)d(\(or)i(subpackage\))e(of)j(the)0 707 y(package,)23 b(or)h(some)g(other)f(name)h(de\002ned)f(in)h(the)g (package,)f(lik)o(e)i(a)f(function,)f(class)i(or)f(v)n(ariable.)36 b(The)23 b Fp(import)h Fs(statement)g(\002rst)0 807 y(tests)g(whether)e (the)h(item)h(is)g(de\002ned)e(in)h(the)g(package;)g(if)g(not,)g(it)h (assumes)g(it)f(is)h(a)g(module)d(and)i(attempts)g(to)g(load)g(it.)34 b(If)23 b(it)h(f)o(ails)f(to)0 906 y(\002nd)d(it,)h(an)f Fp(ImportError)e Fs(e)o(xception)h(is)i(raised.)0 1053 y(Contrarily)-5 b(,)15 b(when)h(using)g(syntax)g(lik)o(e)h Fp(import)48 b Fq(item.subitem.subsubitem)p Fs(,)15 b(each)h(item)h(e)o (xcept)f(for)f(the)i(last)g(must)g(be)f(a)h(package;)0 1153 y(the)j(last)h(item)g(can)f(be)g(a)g(module)f(or)h(a)h(package)d (b)n(ut)j(can')o(t)e(be)h(a)h(class)g(or)f(function)e(or)i(v)n(ariable) f(de\002ned)g(in)i(the)f(pre)n(vious)e(item.)0 1438 y Fj(6.4.1)100 b(Impor)t(ting)29 b(*)f(F)l(rom)f(a)h(P)l(ac)n(kage)0 1641 y Fs(No)n(w)23 b(what)h(happens)e(when)h(the)g(user)g(writes)h Fp(from)49 b(sound.effects)f(import)2679 1656 y(*)2729 1641 y Fs(?)35 b(Ideally)-5 b(,)22 b(one)h(w)o(ould)g(hope)f(that)i (this)0 1740 y(someho)n(w)f(goes)i(out)f(to)h(the)f(\002lesystem,)i (\002nds)f(which)f(submodules)e(are)j(present)f(in)h(the)f(package,)g (and)g(imports)g(them)g(all.)39 b(Un-)0 1840 y(fortunately)-5 b(,)17 b(this)k(operation)d(does)i(not)g(w)o(ork)g(v)o(ery)f(well)h(on) g(W)m(indo)n(ws)g(platforms,)f(where)g(the)h(\002lesystem)h(does)f(not) g(al)o(w)o(ays)g(ha)n(v)o(e)0 1939 y(accurate)j(information)e(about)i (the)h(case)g(of)g(a)g(\002lename!)35 b(On)24 b(these)g(platforms,)f (there)g(is)i(no)e(guaranteed)f(w)o(ay)i(to)g(kno)n(w)f(whether)0 2039 y(a)f(\002le)h(`)p Ft(ECHO)m(.PY)p Fs(')e(should)f(be)i(imported)e (as)j(a)f(module)e Fp(echo)p Fs(,)i Fp(Echo)g Fs(or)f Fp(ECHO)p Fs(.)g(\(F)o(or)g(e)o(xample,)g(W)m(indo)n(ws)g(95)g(has)h (the)g(anno)o(y-)0 2139 y(ing)h(practice)g(of)g(sho)n(wing)g(all)h (\002le)g(names)f(with)h(a)g(capitalized)e(\002rst)j(letter)-5 b(.\))35 b(The)23 b(DOS)h(8+3)f(\002lename)g(restriction)g(adds)g (another)0 2238 y(interesting)c(problem)g(for)g(long)h(module)e(names.) 0 2385 y(The)25 b(only)g(solution)g(is)i(for)e(the)h(package)e(author)h (to)h(pro)o(vide)d(an)j(e)o(xplicit)f(inde)o(x)g(of)g(the)h(package.)40 b(The)26 b(import)e(statement)i(uses)0 2485 y(the)g(follo)n(wing)e(con) m(v)o(ention:)33 b(if)27 b(a)f(package')-5 b(s)25 b(`)p Ft(__init__.p)n(y)p Fs(')c(code)k(de\002nes)h(a)g(list)h(named)e Fp(__all__)p Fs(,)h(it)h(is)g(tak)o(en)e(to)h(be)g(the)f(list)0 2584 y(of)e(module)e(names)i(that)g(should)f(be)h(imported)e(when)h Fp(from)49 b Fq(pac)n(ka)o(g)o(e)f Fp(import)2543 2599 y(*)2616 2584 y Fs(is)24 b(encountered.)30 b(It)24 b(is)g(up)e(to)h (the)g(package)0 2684 y(author)18 b(to)h(k)o(eep)g(this)g(list)h (up-to-date)d(when)i(a)g(ne)n(w)g(v)o(ersion)f(of)h(the)g(package)e(is) j(released.)k(P)o(ackage)19 b(authors)f(may)g(also)i(decide)e(not)0 2784 y(to)i(support)e(it,)i(if)g(the)o(y)f(don')o(t)f(see)j(a)f(use)g (for)f(importing)f(*)h(from)g(their)h(package.)j(F)o(or)c(e)o(xample,)f (the)i(\002le)g(`)p Ft(sounds/eff)n(ects/__init__-)0 2883 y(.p)n(y)p Fs(')g(could)f(contain)g(the)h(follo)n(wing)f(code:)236 3122 y Fk(__all__)44 b(=)h(["echo",)e("surround",)g("reverse"])0 3408 y Fs(This)34 b(w)o(ould)f(mean)g(that)h Fp(from)49 b(sound.effects)f(import)2091 3423 y(*)2175 3408 y Fs(w)o(ould)33 b(import)g(the)h(three)f(named)g(submodules)f(of)i(the)0 3507 y Fp(sound)20 b Fs(package.)0 3654 y(If)28 b Fp(__all__)f Fs(is)i(not)f(de\002ned,)h(the)f(statement)g Fp(from)49 b(sound.effects)e(import)2748 3669 y(*)2826 3654 y Fs(does)28 b Fq(not)g Fs(import)f(all)h(submodules)0 3754 y(from)18 b(the)h(package)f Fp(sound.effects)f Fs(into)i(the)g(current)f (namespace;)g(it)i(only)f(ensures)f(that)i(the)f(package)e Fp(sound.effects)0 3853 y Fs(has)29 b(been)f(imported)f(\(possibly)g (running)g(an)o(y)g(initialization)h(code)g(in)h(`)p Ft(__init__.p)n(y)p Fs('\))24 b(and)k(then)g(imports)g(whate)n(v)o(er)f (names)i(are)0 3953 y(de\002ned)22 b(in)g(the)h(package.)30 b(This)23 b(includes)f(an)o(y)g(names)g(de\002ned)f(\(and)h(submodules) f(e)o(xplicitly)g(loaded\))g(by)h(`)p Ft(__init__.p)n(y)p Fs('.)28 b(It)23 b(also)0 4053 y(includes)16 b(an)o(y)g(submodules)f (of)i(the)g(package)e(that)i(were)g(e)o(xplicitly)f(loaded)g(by)g(pre)n (vious)g(import)g(statements.)23 b(Consider)17 b(this)g(code:)236 4291 y Fk(import)44 b(sound.effects.echo)236 4382 y(import)g (sound.effects.surround)236 4474 y(from)g(sound.effects)f(import)1402 4487 y(*)0 4760 y Fs(In)19 b(this)g(e)o(xample,)f(the)h(echo)f(and)h (surround)e(modules)h(are)h(imported)e(in)i(the)g(current)f(namespace)g (because)g(the)o(y)g(are)h(de\002ned)f(in)i(the)0 4859 y Fp(sound.effects)e Fs(package)g(when)h(the)h Fp(from...import)e Fs(statement)i(is)g(e)o(x)o(ecuted.)j(\(This)d(also)g(w)o(orks)f(when)h Fp(__all__)f Fs(is)0 4959 y(de\002ned.\))0 5106 y(Note)e(that)g(in)g (general)f(the)h(practice)f(of)h(importing)1496 5121 y Fp(*)1563 5106 y Fs(from)f(a)h(module)f(or)h(package)e(is)j(fro)n (wned)d(upon,)h(since)h(it)h(often)e(causes)h(poorly)0 5205 y(readable)i(code.)25 b(Ho)n(we)n(v)o(er)m(,)19 b(it)i(is)g(okay)f(to)g(use)h(it)g(to)g(sa)n(v)o(e)g(typing)e(in)h (interacti)n(v)o(e)g(sessions,)h(and)e(certain)h(modules)g(are)g (designed)f(to)0 5305 y(e)o(xport)g(only)g(names)h(that)g(follo)n(w)g (certain)f(patterns.)p 0 5549 3901 4 v 0 5649 a Fo(48)3032 b(Chapter)23 b(6.)52 b(Modules)p eop end %%Page: 49 57 TeXDict begin 49 56 bop 0 83 a Fs(Remember)m(,)22 b(there)h(is)h (nothing)d(wrong)h(with)i(using)e Fp(from)49 b(Package)g(import)f (specific_submodule)p Fs(!)31 b(In)23 b(f)o(act,)h(this)0 183 y(is)19 b(the)e(recommended)e(notation)h(unless)i(the)g(importing)d (module)i(needs)g(to)h(use)f(submodules)f(with)i(the)g(same)g(name)f (from)f(dif)n(ferent)0 282 y(packages.)0 567 y Fj(6.4.2)100 b(Intr)o(a-pac)n(kage)30 b(Ref)m(erences)0 770 y Fs(The)20 b(submodules)f(often)g(need)h(to)h(refer)e(to)i(each)f(other)-5 b(.)25 b(F)o(or)20 b(e)o(xample,)f(the)i Fp(surround)e Fs(module)g(might)h(use)g(the)h Fp(echo)f Fs(module.)0 870 y(In)d(f)o(act,)h(such)f(references)f(are)h(so)g(common)f(that)h (the)g Fp(import)g Fs(statement)g(\002rst)h(looks)f(in)g(the)g (containing)f(package)f(before)h(looking)0 969 y(in)24 b(the)f(standard)g(module)f(search)h(path.)35 b(Thus,)23 b(the)h Fp(surround)f Fs(module)f(can)h(simply)g(use)h Fp(import)49 b(echo)23 b Fs(or)h Fp(from)49 b(echo)0 1069 y(import)g(echofilter)p Fs(.)54 b(If)31 b(the)f(imported)f(module) h(is)h(not)f(found)f(in)i(the)f(current)g(package)f(\(the)h(package)f (of)i(which)f(the)0 1169 y(current)19 b(module)g(is)i(a)f(submodule\),) e(the)i Fp(import)g Fs(statement)g(looks)f(for)h(a)h(top-le)n(v)o(el)d (module)h(with)h(the)h(gi)n(v)o(en)e(name.)0 1315 y(When)24 b(packages)g(are)g(structured)f(into)h(subpackages)f(\(as)i(with)f(the) h Fp(sound)f Fs(package)f(in)i(the)f(e)o(xample\),)g(you)f(can)i(use)f (absolute)0 1415 y(imports)17 b(to)g(refer)g(to)h(submodules)d(of)j (siblings)f(packages.)23 b(F)o(or)17 b(e)o(xample,)f(if)i(the)g(module) e Fp(sound.filters.vocoder)e Fs(needs)0 1515 y(to)20 b(use)h(the)f Fp(echo)g Fs(module)f(in)h(the)g Fp(sound.effects)f Fs(package,)f(it)j(can)f(use)g Fp(from)50 b(sound.effects)d(import)i (echo)p Fs(.)0 1662 y(Starting)31 b(with)g(Python)f(2.5,)i(in)g (addition)d(to)j(the)f(implicit)g(relati)n(v)o(e)f(imports)g(described) g(abo)o(v)o(e,)i(you)e(can)h(also)g(write)g(e)o(xplicit)0 1761 y(relati)n(v)o(e)21 b(imports)f(with)i(the)f Fp(from)49 b(module)g(import)f(name)21 b Fs(form)g(of)g(import)f(statement.)28 b(These)21 b(e)o(xplicit)g(relati)n(v)o(e)f(imports)0 1861 y(use)28 b(leading)e(dots)h(to)h(indicate)e(the)i(current)d(and)i (parent)g(packages)f(in)m(v)n(olv)o(ed)f(in)i(the)h(relati)n(v)o(e)e (import.)45 b(From)27 b(the)g Fp(surround)0 1960 y Fs(module)19 b(for)g(e)o(xample,)g(you)g(might)h(use:)236 2199 y Fk(from)44 b(.)h(import)f(echo)236 2290 y(from)g(..)h(import)f(formats)236 2381 y(from)g(..filters)g(import)g(equalizer)0 2667 y Fs(Note)27 b(that)g(both)g(e)o(xplicit)f(and)h(implicit)g(relati)n(v)o (e)f(imports)h(are)g(based)f(on)h(the)g(name)g(of)g(the)g(current)e (module.)45 b(Since)27 b(the)g(name)0 2767 y(of)d(the)f(main)h(module)e (is)j(al)o(w)o(ays)f Fp("__main__")p Fs(,)g(modules)e(intended)h(for)g (use)h(as)h(the)f(main)f(module)g(of)g(a)h(Python)f(application)0 2867 y(should)c(al)o(w)o(ays)i(use)f(absolute)g(imports.)0 3152 y Fj(6.4.3)100 b(P)l(ac)n(kages)29 b(in)g(Multiple)f(Director)q (ies)0 3355 y Fs(P)o(ackages)d(support)f(one)h(more)g(special)h(attrib) n(ute,)g Fp(__path__)p Fs(.)40 b(This)26 b(is)h(initialized)e(to)h(be)f (a)h(list)h(containing)d(the)h(name)g(of)h(the)0 3454 y(directory)d(holding)g(the)i(package')-5 b(s)23 b(`)p Ft(__init__.p)n(y)p Fs(')e(before)j(the)g(code)g(in)h(that)g(\002le)g (is)h(e)o(x)o(ecuted.)37 b(This)25 b(v)n(ariable)e(can)i(be)g (modi\002ed;)0 3554 y(doing)19 b(so)h(af)n(fects)g(future)f(searches)h (for)g(modules)f(and)h(subpackages)e(contained)h(in)h(the)g(package.)0 3701 y(While)h(this)f(feature)g(is)h(not)f(often)f(needed,)g(it)h(can)g (be)h(used)f(to)g(e)o(xtend)f(the)h(set)h(of)f(modules)f(found)f(in)j (a)f(package.)p 0 5549 3901 4 v 0 5649 a Fo(6.4.)52 b(P)m(ac)n(kages) 3258 b(49)p eop end %%Page: 50 58 TeXDict begin 50 57 bop 0 5549 3901 4 v 0 5649 a Fo(50)p eop end %%Page: 51 59 TeXDict begin 51 58 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3495 427 y Fn(SEVEN)p 0 515 V 2304 978 a Fy(Input)58 b(and)e(Output)0 1465 y Fs(There)19 b(are)g(se)n(v)o(eral)g(w)o(ays)h(to)f(present)g (the)g(output)g(of)g(a)h(program;)d(data)j(can)f(be)g(printed)f(in)i(a) g(human-readable)c(form,)i(or)h(written)g(to)0 1565 y(a)i(\002le)f(for) g(future)f(use.)25 b(This)c(chapter)e(will)i(discuss)f(some)g(of)g(the) g(possibilities.)0 1892 y Fl(7.1)121 b(F)-6 b(ancier)34 b(Output)h(F)l(or)s(matting)0 2125 y Fs(So)22 b(f)o(ar)f(we')l(v)o(e)g (encountered)e(tw)o(o)i(w)o(ays)h(of)g(writing)f(v)n(alues:)27 b Fq(e)n(xpr)m(ession)22 b(statements)f Fs(and)g(the)h Fp(print)f Fs(statement.)29 b(\(A)22 b(third)f(w)o(ay)0 2225 y(is)26 b(using)e(the)i Fp(write\(\))e Fs(method)f(of)i(\002le)h (objects;)h(the)e(standard)f(output)g(\002le)i(can)e(be)h(referenced)e (as)j Fp(sys.stdout)p Fs(.)38 b(See)26 b(the)0 2324 y(Library)19 b(Reference)g(for)h(more)f(information)f(on)h(this.\))0 2471 y(Often)h(you')o(ll)f(w)o(ant)h(more)f(control)g(o)o(v)o(er)g(the) h(formatting)e(of)h(your)g(output)g(than)h(simply)f(printing)g (space-separated)f(v)n(alues.)24 b(There)0 2571 y(are)j(tw)o(o)h(w)o (ays)f(to)h(format)e(your)g(output;)j(the)f(\002rst)g(w)o(ay)f(is)h(to) g(do)e(all)i(the)f(string)g(handling)f(yourself;)j(using)e(string)g (slicing)g(and)0 2670 y(concatenation)22 b(operations)h(you)h(can)g (create)h(an)o(y)e(layout)h(you)g(can)g(imagine.)37 b(The)24 b(standard)g(module)f Fp(string)h Fs(contains)g(some)0 2770 y(useful)16 b(operations)f(for)h(padding)f(strings)h(to)h(a)g(gi)n (v)o(en)e(column)g(width;)j(these)f(will)g(be)g(discussed)f(shortly)-5 b(.)23 b(The)16 b(second)f(w)o(ay)i(is)h(to)e(use)0 2870 y(the)i Fp(\045)g Fs(operator)d(with)j(a)g(string)g(as)g(the)f(left)h (ar)o(gument.)k(The)17 b Fp(\045)h Fs(operator)e(interprets)h(the)g (left)h(ar)o(gument)d(much)i(lik)o(e)h(a)g Fp(sprintf\(\))p Fs(-)0 2969 y(style)j(format)e(string)h(to)g(be)g(applied)f(to)i(the)f (right)f(ar)o(gument,)f(and)i(returns)f(the)h(string)g(resulting)f (from)g(this)i(formatting)d(operation.)0 3116 y(One)g(question)f (remains,)h(of)f(course:)24 b(ho)n(w)17 b(do)h(you)f(con)m(v)o(ert)f(v) n(alues)i(to)g(strings?)24 b(Luckily)-5 b(,)17 b(Python)g(has)h(w)o (ays)h(to)f(con)m(v)o(ert)e(an)o(y)h(v)n(alue)0 3216 y(to)j(a)g(string:)25 b(pass)c(it)f(to)g(the)g Fp(repr\(\))g Fs(or)f Fp(str\(\))h Fs(functions.)j(Re)n(v)o(erse)d(quotes)f(\()p Fp(\223)p Fs(\))h(are)g(equi)n(v)n(alent)e(to)i Fp(repr\(\))p Fs(,)f(b)n(ut)h(the)o(y)f(are)h(no)0 3315 y(longer)f(used)h(in)g (modern)f(Python)g(code)g(and)h(will)h(lik)o(ely)f(not)g(be)g(in)g (future)f(v)o(ersions)g(of)h(the)g(language.)0 3462 y(The)27 b Fp(str\(\))g Fs(function)f(is)i(meant)f(to)g(return)f (representations)g(of)h(v)n(alues)g(which)f(are)i(f)o(airly)f (human-readable,)e(while)i Fp(repr\(\))0 3562 y Fs(is)j(meant)e(to)g (generate)g(representations)f(which)h(can)g(be)h(read)f(by)g(the)h (interpreter)e(\(or)h(will)h(force)f(a)h Fp(SyntaxError)e Fs(if)i(there)0 3661 y(is)d(not)f(equi)n(v)n(alent)f(syntax\).)39 b(F)o(or)25 b(objects)g(which)g(don')o(t)f(ha)n(v)o(e)g(a)i(particular) e(representation)f(for)i(human)f(consumption,)g Fp(str\(\))0 3761 y Fs(will)h(return)f(the)h(same)g(v)n(alue)f(as)h Fp(repr\(\))p Fs(.)38 b(Man)o(y)24 b(v)n(alues,)h(such)g(as)g(numbers)f (or)g(structures)g(lik)o(e)h(lists)h(and)f(dictionaries,)f(ha)n(v)o(e)0 3861 y(the)30 b(same)g(representation)d(using)i(either)h(function.)51 b(Strings)30 b(and)f(\003oating)g(point)g(numbers,)h(in)g(particular)m (,)g(ha)n(v)o(e)g(tw)o(o)g(distinct)0 3960 y(representations.)0 4107 y(Some)20 b(e)o(xamples:)p 0 5549 3901 4 v 3808 5649 a Fo(51)p eop end %%Page: 52 60 TeXDict begin 52 59 bop 236 83 a Fk(>>>)45 b(s)f(=)h('Hello,)f(world.') 236 174 y(>>>)h(str\(s\))236 266 y('Hello,)f(world.')236 357 y(>>>)h(repr\(s\))236 448 y("'Hello,)f(world.'")236 540 y(>>>)h(str\(0.1\))236 631 y('0.1')236 722 y(>>>)g(repr\(0.1\))236 814 y('0.10000000000000001')236 905 y(>>>)g(x)f(=)h(10)729 918 y(*)819 905 y(3.25)236 996 y(>>>)g(y)f(=)h(200)774 1009 y(*)864 996 y(200)236 1088 y(>>>)g(s)f(=)h('The)f(value)g(of)h(x)f (is)h(')f(+)h(repr\(x\))f(+)g(',)h(and)f(y)h(is)f(')h(+)g(repr\(y\))e (+)i('...')236 1179 y(>>>)g(print)f(s)236 1270 y(The)h(value)f(of)g(x)h (is)f(32.5,)g(and)h(y)f(is)h(40000...)236 1362 y(>>>)g(#)f(The)h (repr\(\))e(of)i(a)g(string)e(adds)i(string)e(quotes)h(and)g (backslashes:)236 1453 y(...)h(hello)f(=)g('hello,)g(world\\n')236 1544 y(>>>)h(hellos)e(=)i(repr\(hello\))236 1636 y(>>>)g(print)f (hellos)236 1727 y('hello,)g(world\\n')236 1818 y(>>>)h(#)f(The)h (argument)e(to)i(repr\(\))e(may)i(be)f(any)g(Python)g(object:)236 1910 y(...)h(repr\(\(x,)e(y,)i(\('spam',)e('eggs'\)\)\))236 2001 y("\(32.5,)h(40000,)g(\('spam',)f('eggs'\)\)")236 2092 y(>>>)i(#)f(reverse)g(quotes)g(are)g(convenient)f(in)i (interactive)e(sessions:)236 2183 y(...)i(`x,)f(y,)g(\('spam',)g ('eggs'\)`)236 2275 y("\(32.5,)g(40000,)g(\('spam',)f('eggs'\)\)")0 2561 y Fs(Here)20 b(are)g(tw)o(o)h(w)o(ays)f(to)h(write)f(a)h(table)f (of)g(squares)f(and)h(cubes:)236 2708 y Fk(>>>)45 b(for)f(x)h(in)f (range\(1,)g(11\):)236 2799 y(...)224 b(print)44 b (repr\(x\).rjust\(2\),)e(repr\(x)1941 2812 y(*)1986 2799 y(x\).rjust\(3\),)236 2891 y(...)224 b(#)45 b(Note)f(trailing)f(comma)h (on)h(previous)e(line)236 2982 y(...)224 b(print)44 b(repr\(x)1134 2995 y(*)1179 2982 y(x)1224 2995 y(*)1269 2982 y(x\).rjust\(4\))236 3073 y(...)281 3164 y(1)134 b(1)180 b(1)281 3256 y(2)134 b(4)180 b(8)281 3347 y(3)134 b(9)h(27)281 3438 y(4)90 b(16)134 b(64)281 3530 y(5)90 b(25)f(125)281 3621 y(6)h(36)f(216)281 3712 y(7)h(49)f(343)281 3804 y(8)h(64)f(512)281 3895 y(9)h(81)f(729)236 3986 y(10)45 b(100)f(1000)236 4169 y(>>>)h(for)f(x)h(in)f(range\(1,11\):)236 4260 y(...)224 b(print)44 b('\0452d)g(\0453d)g(\0454d')h(\045)f(\(x,)h(x)1806 4273 y(*)1851 4260 y(x,)f(x)2030 4273 y(*)2075 4260 y(x)2120 4273 y(*)2165 4260 y(x\))236 4352 y(...)281 4443 y(1)134 b(1)180 b(1)281 4534 y(2)134 b(4)180 b(8)281 4626 y(3)134 b(9)h(27)281 4717 y(4)90 b(16)134 b(64)281 4808 y(5)90 b(25)f(125)281 4900 y(6)h(36)f(216)281 4991 y(7)h(49)f(343)281 5082 y(8)h(64)f(512)281 5174 y(9)h(81)f(729)236 5265 y(10)45 b(100)f(1000)p 0 5549 3901 4 v 0 5649 a Fo(52)2727 b(Chapter)24 b(7.)51 b(Input)24 b(and)g(Output)p eop end %%Page: 53 61 TeXDict begin 53 60 bop 0 83 a Fs(\(Note)22 b(that)g(in)g(the)g (\002rst)h(e)o(xample,)d(one)i(space)g(between)f(each)h(column)e(w)o (as)j(added)e(by)g(the)h(w)o(ay)h Fp(print)e Fs(w)o(orks:)29 b(it)22 b(al)o(w)o(ays)h(adds)0 183 y(spaces)d(between)g(its)h(ar)o (guments.\))0 330 y(This)i(e)o(xample)d(demonstrates)h(the)i Fp(rjust\(\))e Fs(method)g(of)h(string)g(objects,)h(which)f (right-justi\002es)f(a)i(string)f(in)h(a)f(\002eld)h(of)f(a)h(gi)n(v)o (en)0 429 y(width)h(by)h(padding)e(it)i(with)g(spaces)g(on)g(the)f (left.)39 b(There)24 b(are)h(similar)g(methods)f Fp(ljust\(\))g Fs(and)g Fp(center\(\))p Fs(.)38 b(These)25 b(methods)0 529 y(do)d(not)g(write)h(an)o(ything,)e(the)o(y)h(just)h(return)e(a)i (ne)n(w)f(string.)32 b(If)22 b(the)h(input)e(string)i(is)g(too)f(long,) g(the)o(y)g(don')o(t)f(truncate)g(it,)j(b)n(ut)f(return)e(it)0 628 y(unchanged;)e(this)k(will)f(mess)g(up)f(your)f(column)g(lay-out)h (b)n(ut)g(that')-5 b(s)22 b(usually)f(better)g(than)g(the)h(alternati)n (v)o(e,)e(which)h(w)o(ould)g(be)g(lying)0 728 y(about)e(a)i(v)n(alue.)j (\(If)c(you)f(really)h(w)o(ant)g(truncation)f(you)g(can)h(al)o(w)o(ays) h(add)e(a)i(slice)g(operation,)d(as)j(in)f(`)p Fp(x.ljust\(n\)[:n])p Fs('.\))0 875 y(There)e(is)i(another)e(method,)g Fp(zfill\(\))p Fs(,)g(which)h(pads)g(a)g(numeric)f(string)h(on)g(the)g(left)g(with)h (zeros.)k(It)19 b(understands)f(about)g(plus)h(and)0 975 y(minus)h(signs:)236 1121 y Fk(>>>)45 b('12'.zfill\(5\))236 1213 y('00012')236 1304 y(>>>)g('-3.14'.zfill\(7\))236 1395 y('-003.14')236 1487 y(>>>)g('3.14159265359'.zfill\(5\))236 1578 y('3.14159265359')0 1864 y Fs(Using)20 b(the)g Fp(\045)h Fs(operator)d(looks)i(lik)o(e)g(this:)236 2102 y Fk(>>>)45 b(import)e(math)236 2194 y(>>>)i(print)f('The)g(value)g(of)g(PI)h(is)f (approximately)f(\0455.3f.')g(\045)i(math.pi)236 2285 y(The)g(value)f(of)g(PI)h(is)f(approximately)f(3.142.)0 2571 y Fs(If)20 b(there)g(is)h(more)e(than)h(one)g(format)f(in)h(the)g (string,)g(you)f(need)h(to)g(pass)h(a)f(tuple)g(as)h(right)f(operand,)e (as)i(in)h(this)g(e)o(xample:)236 2809 y Fk(>>>)45 b(table)f(=)g ({'Sjoerd':)f(4127,)h('Jack':)g(4098,)g('Dcab':)g(7678})236 2901 y(>>>)h(for)f(name,)g(phone)g(in)g(table.items\(\):)236 2992 y(...)224 b(print)44 b('\045-10s)g(==>)g(\04510d')g(\045)h (\(name,)e(phone\))236 3083 y(...)236 3175 y(Jack)313 b(==>)h(4098)236 3266 y(Dcab)f(==>)h(7678)236 3357 y(Sjoerd)223 b(==>)314 b(4127)0 3644 y Fs(Most)32 b(formats)g(w)o(ork)f(e)o(xactly)g (as)i(in)g(C)g(and)e(require)g(that)h(you)g(pass)g(the)g(proper)f (type;)38 b(ho)n(we)n(v)o(er)m(,)32 b(if)h(you)e(don')o(t)f(you)h(get)i (an)0 3743 y(e)o(xception,)19 b(not)i(a)g(core)g(dump.)26 b(The)21 b Fp(\045s)g Fs(format)f(is)i(more)f(relax)o(ed:)k(if)d(the)f (corresponding)c(ar)o(gument)i(is)j(not)f(a)g(string)g(object,)g(it)h (is)0 3843 y(con)m(v)o(erted)17 b(to)i(string)h(using)e(the)i Fp(str\(\))f Fs(b)n(uilt-in)g(function.)k(Using)2031 3858 y Fp(*)2101 3843 y Fs(to)d(pass)g(the)f(width)g(or)g(precision)g (in)g(as)i(a)e(separate)g(\(inte)o(ger\))0 3942 y(ar)o(gument)f(is)j (supported.)i(The)d(C)h(formats)e Fp(\045n)i Fs(and)e Fp(\045p)i Fs(are)f(not)g(supported.)0 4089 y(If)27 b(you)g(ha)n(v)o(e) g(a)h(really)f(long)g(format)f(string)h(that)h(you)f(don')o(t)f(w)o (ant)h(to)h(split)g(up,)h(it)f(w)o(ould)f(be)g(nice)h(if)f(you)g(could) g(reference)f(the)0 4189 y(v)n(ariables)h(to)h(be)g(formatted)e(by)h (name)h(instead)f(of)h(by)f(position.)47 b(This)28 b(can)g(be)g(done)f (by)g(using)g(form)g Fp(\045\(name\)format)p Fs(,)h(as)0 4289 y(sho)n(wn)19 b(here:)236 4435 y Fk(>>>)45 b(table)f(=)g ({'Sjoerd':)f(4127,)h('Jack':)g(4098,)g('Dcab':)g(8637678})236 4527 y(>>>)h(print)f('Jack:)f(\045\(Jack\)d;)h(Sjoerd:)f (\045\(Sjoerd\)d;)g(Dcab:)h(\045\(Dcab\)d')g(\045)g(table)236 4618 y(Jack:)g(4098;)g(Sjoerd:)g(4127;)g(Dcab:)g(8637678)0 4904 y Fs(This)20 b(is)h(particularly)d(useful)i(in)g(combination)d (with)j(the)g(ne)n(w)g(b)n(uilt-in)g Fp(vars\(\))f Fs(function,)f (which)h(returns)g(a)i(dictionary)d(contain-)0 5004 y(ing)i(all)h (local)f(v)n(ariables.)p 0 5549 3901 4 v 0 5649 a Fo(7.1.)52 b(F)l(ancier)23 b(Output)g(F)n(or)r(matting)2654 b(53)p eop end %%Page: 54 62 TeXDict begin 54 61 bop 0 85 a Fl(7.2)121 b(Reading)35 b(and)g(Wr)r(iting)e(Files)0 318 y Fp(open\(\))20 b Fs(returns)f(a)i (\002le)f(object,)g(and)f(is)j(most)e(commonly)e(used)i(with)g(tw)o(o)h (ar)o(guments:)i(`)p Fp(open\()p Fq(\002lename)p Fp(,)46 b Fq(mode)p Fp(\))p Fs('.)236 556 y Fk(>>>)f(f=open\('/tmp/workfile',)c ('w'\))236 647 y(>>>)k(print)f(f)236 739 y()0 1025 y Fs(The)19 b(\002rst)i(ar)o(gument)c(is)k(a)f(string)f(containing)f (the)i(\002lename.)k(The)c(second)e(ar)o(gument)g(is)i(another)f (string)g(containing)f(a)i(fe)n(w)g(charac-)0 1125 y(ters)h(describing) e(the)i(w)o(ay)g(in)g(which)f(the)h(\002le)g(will)g(be)g(used.)26 b Fq(mode)20 b Fs(can)h(be)g Fp('r')f Fs(when)g(the)h(\002le)h(will)f (only)f(be)h(read,)f Fp('w')g Fs(for)g(only)0 1224 y(writing)j(\(an)h (e)o(xisting)f(\002le)h(with)g(the)g(same)g(name)f(will)i(be)f (erased\),)f(and)h Fp('a')f Fs(opens)g(the)h(\002le)h(for)e(appending;) g(an)o(y)g(data)h(written)0 1324 y(to)d(the)g(\002le)h(is)g (automatically)d(added)h(to)h(the)g(end.)27 b Fp('r+')20 b Fs(opens)h(the)g(\002le)g(for)f(both)h(reading)e(and)h(writing.)27 b(The)21 b Fq(mode)f Fs(ar)o(gument)f(is)0 1424 y(optional;)g Fp('r')h Fs(will)h(be)f(assumed)g(if)g(it')-5 b(s)22 b(omitted.)0 1570 y(On)i(W)m(indo)n(ws)g(and)g(the)g(Macintosh,)g Fp('b')h Fs(appended)d(to)i(the)h(mode)e(opens)h(the)g(\002le)h(in)g (binary)e(mode,)h(so)g(there)g(are)h(also)f(modes)0 1670 y(lik)o(e)c Fp('rb')p Fs(,)g Fp('wb')p Fs(,)g(and)f Fp('r+b')p Fs(.)25 b(W)m(indo)n(ws)19 b(mak)o(es)h(a)g(distinction)g(between)f(te) o(xt)h(and)f(binary)g(\002les;)i(the)f(end-of-line)e(characters)0 1770 y(in)j(te)o(xt)f(\002les)i(are)e(automatically)g(altered)g (slightly)g(when)g(data)h(is)g(read)f(or)h(written.)26 b(This)21 b(behind-the-scenes)c(modi\002cation)i(to)i(\002le)0 1869 y(data)i(is)h(\002ne)f(for)h Fm(A)t(S)t(C)t(I)t(I)j Fs(te)o(xt)c(\002les,)h(b)n(ut)f(it')o(ll)h(corrupt)d(binary)h(data)h (lik)o(e)g(that)h(in)f(`)p Ft(JPEG)p Fs(')g(or)g(`)p Ft(EXE)p Fs(')f(\002les.)34 b(Be)24 b(v)o(ery)e(careful)g(to)h(use)0 1969 y(binary)c(mode)g(when)h(reading)e(and)i(writing)g(such)g (\002les.)0 2254 y Fj(7.2.1)100 b(Methods)29 b(of)f(File)g(Objects)0 2457 y Fs(The)20 b(rest)g(of)g(the)h(e)o(xamples)e(in)h(this)h(section) f(will)h(assume)f(that)g(a)h(\002le)g(object)e(called)h Fp(f)h Fs(has)f(already)f(been)h(created.)0 2604 y(T)-7 b(o)23 b(read)g(a)g(\002le')-5 b(s)24 b(contents,)f(call)h Fp(f.read\()p Fq(size)p Fp(\))p Fs(,)f(which)f(reads)h(some)g(quantity) f(of)h(data)f(and)h(returns)f(it)i(as)g(a)f(string.)33 b Fq(size)25 b Fs(is)f(an)0 2703 y(optional)19 b(numeric)h(ar)o (gument.)k(When)d Fq(size)g Fs(is)h(omitted)e(or)h(ne)o(gati)n(v)o(e,)d (the)j(entire)f(contents)g(of)h(the)f(\002le)i(will)f(be)g(read)f(and)g (returned;)0 2803 y(it')-5 b(s)26 b(your)d(problem)g(if)i(the)g(\002le) g(is)h(twice)f(as)g(lar)o(ge)f(as)h(your)f(machine')-5 b(s)24 b(memory)-5 b(.)36 b(Otherwise,)25 b(at)g(most)g Fq(size)h Fs(bytes)e(are)h(read)f(and)0 2902 y(returned.)f(If)d(the)g (end)g(of)g(the)g(\002le)h(has)f(been)g(reached,)f Fp(f.read\(\))g Fs(will)i(return)e(an)h(empty)f(string)h(\()p Fp("")p Fs(\).)236 3141 y Fk(>>>)45 b(f.read\(\))236 3232 y('This)f(is)h(the)f (entire)g(file.\\n')236 3323 y(>>>)h(f.read\(\))236 3415 y('')0 3701 y Fp(f.readline\(\))19 b Fs(reads)h(a)i(single)e(line)h (from)f(the)g(\002le;)i(a)f(ne)n(wline)f(character)g(\()p Fp(\\n)p Fs(\))g(is)h(left)g(at)g(the)g(end)f(of)h(the)f(string,)h(and) f(is)h(only)0 3800 y(omitted)j(on)h(the)g(last)h(line)g(of)f(the)g (\002le)h(if)f(the)g(\002le)h(doesn')o(t)e(end)g(in)i(a)f(ne)n(wline.) 40 b(This)25 b(mak)o(es)g(the)g(return)f(v)n(alue)h(unambiguous;)f(if)0 3900 y Fp(f.readline\(\))f Fs(returns)h(an)g(empty)g(string,)h(the)g (end)f(of)g(the)h(\002le)g(has)g(been)f(reached,)g(while)g(a)h(blank)f (line)h(is)g(represented)e(by)0 4000 y Fp('\\n')p Fs(,)d(a)g(string)g (containing)f(only)g(a)i(single)f(ne)n(wline.)236 4147 y Fk(>>>)45 b(f.readline\(\))236 4238 y('This)f(is)h(the)f(first)g (line)g(of)h(the)f(file.\\n')236 4329 y(>>>)h(f.readline\(\))236 4421 y('Second)f(line)g(of)h(the)f(file\\n')236 4512 y(>>>)h(f.readline\(\))236 4603 y('')0 4889 y Fp(f.readlines\(\))24 b Fs(returns)h(a)i(list)g(containing)d(all)j(the)f(lines)h(of)e(data)h (in)h(the)f(\002le.)43 b(If)26 b(gi)n(v)o(en)f(an)h(optional)e (parameter)h Fq(sizehint)q Fs(,)0 4989 y(it)j(reads)e(that)h(man)o(y)f (bytes)g(from)g(the)h(\002le)g(and)g(enough)d(more)i(to)h(complete)f(a) h(line,)i(and)d(returns)g(the)h(lines)g(from)f(that.)45 b(This)27 b(is)0 5089 y(often)19 b(used)i(to)f(allo)n(w)g(ef)n (\002cient)g(reading)f(of)h(a)h(lar)o(ge)e(\002le)i(by)f(lines,)h(b)n (ut)f(without)g(ha)n(ving)f(to)h(load)g(the)g(entire)g(\002le)h(in)g (memory)-5 b(.)23 b(Only)0 5188 y(complete)c(lines)i(will)g(be)f (returned.)p 0 5549 3901 4 v 0 5649 a Fo(54)2727 b(Chapter)24 b(7.)51 b(Input)24 b(and)g(Output)p eop end %%Page: 55 63 TeXDict begin 55 62 bop 236 174 a Fk(>>>)45 b(f.readlines\(\))236 266 y(['This)f(is)h(the)f(first)g(line)g(of)h(the)f(file.\\n',)f ('Second)h(line)g(of)g(the)h(file\\n'])0 552 y Fs(An)17 b(alternate)g(approach)e(to)i(reading)f(lines)h(is)h(to)f(loop)g(o)o(v) o(er)f(the)h(\002le)g(object.)24 b(This)17 b(is)h(memory)e(ef)n (\002cient,)h(f)o(ast,)h(and)e(leads)i(to)f(simpler)0 651 y(code:)236 890 y Fk(>>>)45 b(for)f(line)g(in)h(f:)595 981 y(print)f(line,)236 1164 y(This)g(is)h(the)f(first)g(line)g(of)h (the)f(file.)236 1255 y(Second)g(line)g(of)h(the)f(file)0 1541 y Fs(The)25 b(alternati)n(v)o(e)f(approach)g(is)i(simpler)f(b)n (ut)h(does)f(not)g(pro)o(vide)f(as)i(\002ne-grained)d(control.)40 b(Since)25 b(the)h(tw)o(o)f(approaches)f(manage)0 1641 y(line)c(b)n(uf)n(fering)e(dif)n(ferently)-5 b(,)18 b(the)o(y)h(should) g(not)h(be)g(mix)o(ed.)0 1788 y Fp(f.write\()p Fq(string)p Fp(\))f Fs(writes)i(the)f(contents)f(of)h Fq(string)h Fs(to)f(the)g(\002le,)h(returning)d Fp(None)p Fs(.)236 1935 y Fk(>>>)45 b(f.write\('This)d(is)j(a)f(test\\n'\))0 2221 y Fs(T)-7 b(o)20 b(write)h(something)d(other)i(than)g(a)g(string,) g(it)h(needs)e(to)i(be)f(con)m(v)o(erted)d(to)k(a)f(string)g(\002rst:) 236 2368 y Fk(>>>)45 b(value)f(=)g(\('the)g(answer',)g(42\))236 2459 y(>>>)h(s)f(=)h(str\(value\))236 2550 y(>>>)g(f.write\(s\))0 2836 y Fp(f.tell\(\))17 b Fs(returns)h(an)g(inte)o(ger)f(gi)n(ving)g (the)i(\002le)g(object')-5 b(s)18 b(current)f(position)g(in)i(the)f (\002le,)h(measured)e(in)h(bytes)h(from)e(the)h(be)o(ginning)0 2936 y(of)k(the)h(\002le.)32 b(T)-7 b(o)23 b(change)e(the)h(\002le)h (object')-5 b(s)22 b(position,)g(use)h(`)p Fp(f.seek\()p Fq(of)o(fset)q Fp(,)48 b Fq(fr)l(om_what)q Fp(\))p Fs('.)31 b(The)22 b(position)g(is)h(computed)e(from)0 3036 y(adding)e Fq(of)o(fset)j Fs(to)f(a)g(reference)d(point;)i(the)h(reference)e (point)g(is)j(selected)e(by)g(the)h Fq(fr)l(om_what)g Fs(ar)o(gument.)j(A)d Fq(fr)l(om_what)g Fs(v)n(alue)f(of)g(0)0 3135 y(measures)h(from)f(the)h(be)o(ginning)e(of)i(the)g(\002le,)h(1)g (uses)f(the)h(current)e(\002le)h(position,)g(and)g(2)g(uses)h(the)f (end)g(of)g(the)g(\002le)h(as)g(the)f(reference)0 3235 y(point.)j Fq(fr)l(om_what)e Fs(can)d(be)i(omitted)e(and)h(def)o(aults) g(to)g(0,)g(using)g(the)g(be)o(ginning)d(of)j(the)g(\002le)h(as)g(the)f (reference)f(point.)236 3473 y Fk(>>>)45 b(f)f(=)h (open\('/tmp/workfile',)c('r+'\))236 3564 y(>>>)k (f.write\('0123456789abcdef'\))236 3656 y(>>>)g(f.seek\(5\))222 b(#)45 b(Go)f(to)h(the)f(6th)h(byte)f(in)g(the)g(file)236 3747 y(>>>)h(f.read\(1\))236 3838 y('5')236 3930 y(>>>)g(f.seek\(-3,)e (2\))h(#)h(Go)f(to)h(the)f(3rd)h(byte)f(before)f(the)i(end)236 4021 y(>>>)g(f.read\(1\))236 4112 y('d')0 4399 y Fs(When)19 b(you')l(re)f(done)g(with)h(a)h(\002le,)g(call)g Fp(f.close\(\))e Fs(to)h(close)h(it)g(and)f(free)g(up)f(an)o(y)h(system)g(resources)g (tak)o(en)g(up)g(by)g(the)g(open)f(\002le.)0 4498 y(After)i(calling)g Fp(f.close\(\))p Fs(,)e(attempts)j(to)f(use)g(the)h(\002le)f(object)g (will)h(automatically)e(f)o(ail.)236 4645 y Fk(>>>)45 b(f.close\(\))236 4736 y(>>>)g(f.read\(\))236 4828 y(Traceback)f (\(most)g(recent)f(call)i(last\):)326 4919 y(File)f("",)f(line)h (1,)h(in)f(?)236 5010 y(ValueError:)f(I/O)i(operation)e(on)h(closed)g (file)0 5297 y Fs(File)26 b(objects)f(ha)n(v)o(e)g(some)g(additional)f (methods,)h(such)g(as)h Fp(isatty\(\))f Fs(and)f Fp(truncate\(\))g Fs(which)h(are)g(less)i(frequently)c(used;)0 5396 y(consult)d(the)g (Library)f(Reference)g(for)g(a)i(complete)e(guide)g(to)i(\002le)g (objects.)p 0 5549 3901 4 v 0 5649 a Fo(7.2.)52 b(Reading)24 b(and)g(Wr)q(iting)h(Files)2666 b(55)p eop end %%Page: 56 64 TeXDict begin 56 63 bop 0 83 a Fj(7.2.2)100 b(The)29 b Fb(pickle)d Fj(Module)0 286 y Fs(Strings)c(can)g(easily)g(be)g (written)f(to)i(and)e(read)g(from)g(a)i(\002le.)31 b(Numbers)20 b(tak)o(e)i(a)h(bit)f(more)f(ef)n(fort,)g(since)h(the)g Fp(read\(\))g Fs(method)e(only)0 386 y(returns)j(strings,)h(which)f (will)h(ha)n(v)o(e)f(to)h(be)f(passed)h(to)f(a)h(function)e(lik)o(e)i Fp(int\(\))p Fs(,)f(which)g(tak)o(es)h(a)g(string)g(lik)o(e)f Fp('123')g Fs(and)g(returns)0 485 y(its)29 b(numeric)f(v)n(alue)f(123.) 49 b(Ho)n(we)n(v)o(er)m(,)29 b(when)e(you)h(w)o(ant)h(to)f(sa)n(v)o(e)h (more)e(comple)o(x)g(data)i(types)f(lik)o(e)h(lists,)i(dictionaries,)e (or)g(class)0 585 y(instances,)20 b(things)g(get)g(a)g(lot)h(more)e (complicated.)0 732 y(Rather)24 b(than)g(ha)n(v)o(e)f(users)h(be)g (constantly)f(writing)h(and)f(deb)n(ugging)f(code)h(to)i(sa)n(v)o(e)f (complicated)e(data)i(types,)h(Python)e(pro)o(vides)f(a)0 831 y(standard)16 b(module)h(called)g Fp(pickle)p Fs(.)24 b(This)18 b(is)g(an)g(amazing)e(module)h(that)g(can)h(tak)o(e)f(almost) h(an)o(y)f(Python)f(object)i(\(e)n(v)o(en)e(some)h(forms)0 931 y(of)25 b(Python)f(code!\),)g(and)g(con)m(v)o(ert)f(it)j(to)f(a)h (string)e(representation;)i(this)f(process)g(is)g(called)g Fq(pic)n(kling)p Fs(.)39 b(Reconstructing)23 b(the)i(object)0 1031 y(from)17 b(the)i(string)f(representation)e(is)j(called)g Fq(unpic)n(kling)p Fs(.)j(Between)d(pickling)e(and)h(unpickling,)e(the) i(string)g(representing)f(the)h(object)0 1130 y(may)i(ha)n(v)o(e)f (been)h(stored)f(in)i(a)f(\002le)h(or)f(data,)g(or)g(sent)g(o)o(v)o(er) f(a)i(netw)o(ork)e(connection)f(to)i(some)g(distant)g(machine.)0 1277 y(If)h(you)g(ha)n(v)o(e)g(an)g(object)g Fp(x)p Fs(,)h(and)f(a)h (\002le)g(object)f Fp(f)h Fs(that')-5 b(s)22 b(been)f(opened)e(for)i (writing,)g(the)h(simplest)g(w)o(ay)f(to)h(pickle)f(the)g(object)g(tak) o(es)0 1377 y(only)e(one)h(line)g(of)g(code:)236 1615 y Fk(pickle.dump\(x,)43 b(f\))0 1901 y Fs(T)-7 b(o)20 b(unpickle)f(the)h(object)g(again,)f(if)h Fp(f)h Fs(is)g(a)g(\002le)f (object)g(which)g(has)g(been)g(opened)e(for)i(reading:)236 2139 y Fk(x)45 b(=)g(pickle.load\(f\))0 2425 y Fs(\(There)18 b(are)i(other)e(v)n(ariants)h(of)h(this,)f(used)h(when)f(pickling)f (man)o(y)g(objects)h(or)h(when)f(you)f(don')o(t)g(w)o(ant)i(to)f(write) h(the)g(pickled)e(data)h(to)0 2525 y(a)i(\002le;)g(consult)e(the)h (complete)f(documentation)f(for)h Fp(pickle)h Fs(in)g(the)g Fq(Python)f(Libr)o(ary)i(Refer)m(ence)p Fs(.\))0 2672 y Fp(pickle)e Fs(is)h(the)g(standard)e(w)o(ay)i(to)f(mak)o(e)g(Python)g (objects)g(which)g(can)g(be)h(stored)f(and)f(reused)h(by)g(other)g (programs)f(or)h(by)g(a)h(future)0 2772 y(in)m(v)n(ocation)e(of)i(the)g (same)g(program;)e(the)i(technical)g(term)g(for)f(this)i(is)g(a)f Fq(per)o(sistent)h Fs(object.)j(Because)c Fp(pickle)g Fs(is)h(so)f(widely)g(used,)0 2871 y(man)o(y)j(authors)g(who)g(write)h (Python)f(e)o(xtensions)g(tak)o(e)h(care)f(to)i(ensure)e(that)h(ne)n(w) f(data)h(types)g(such)g(as)g(matrices)g(can)g(be)g(properly)0 2971 y(pickled)19 b(and)h(unpickled.)p 0 5549 3901 4 v 0 5649 a Fo(56)2727 b(Chapter)24 b(7.)51 b(Input)24 b(and)g(Output)p eop end %%Page: 57 65 TeXDict begin 57 64 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3535 427 y Fn(EIGHT)p 0 515 V 1811 978 a Fy(Errors)56 b(and)h(Exceptions)0 1465 y Fs(Until)18 b(no)n(w)f(error)g(messages)h(ha)n(v)o(en')o(t)e (been)h(more)g(than)g(mentioned,)f(b)n(ut)i(if)g(you)e(ha)n(v)o(e)i (tried)f(out)g(the)h(e)o(xamples)f(you)g(ha)n(v)o(e)g(probably)0 1565 y(seen)j(some.)25 b(There)19 b(are)i(\(at)f(least\))g(tw)o(o)h (distinguishable)d(kinds)i(of)g(errors:)k Fq(syntax)c(err)l(or)o(s)i Fs(and)e Fq(e)n(xceptions)p Fs(.)0 1892 y Fl(8.1)121 b(Syntax)33 b(Errors)0 2125 y Fs(Syntax)23 b(errors,)h(also)h(kno)n(wn) d(as)j(parsing)e(errors,)h(are)g(perhaps)f(the)h(most)h(common)d(kind)h (of)h(complaint)f(you)g(get)h(while)g(you)g(are)0 2225 y(still)d(learning)e(Python:)236 2463 y Fk(>>>)45 b(while)f(True)g (print)g('Hello)g(world')326 2554 y(File)g("",)f(line)h(1,)h(in) f(?)416 2645 y(while)g(True)g(print)g('Hello)g(world')1088 2737 y(^)236 2828 y(SyntaxError:)f(invalid)h(syntax)0 3114 y Fs(The)25 b(parser)f(repeats)h(the)g(of)n(fending)e(line)i(and)g (displays)g(a)g(little)h(`arro)n(w')e(pointing)f(at)j(the)f(earliest)h (point)e(in)i(the)f(line)g(where)g(the)0 3214 y(error)c(w)o(as)h (detected.)30 b(The)21 b(error)g(is)i(caused)e(by)h(\(or)f(at)h(least)h (detected)e(at\))h(the)g(tok)o(en)f Fq(pr)m(eceding)f Fs(the)i(arro)n(w:)28 b(in)22 b(the)g(e)o(xample,)f(the)0 3314 y(error)c(is)h(detected)f(at)i(the)f(k)o(e)o(yw)o(ord)e Fp(print)p Fs(,)h(since)h(a)h(colon)d(\(`)p Fp(:)p Fs('\))23 b(is)c(missing)f(before)e(it.)25 b(File)19 b(name)e(and)g(line)h (number)e(are)i(printed)0 3413 y(so)j(you)e(kno)n(w)g(where)h(to)g (look)f(in)i(case)f(the)g(input)g(came)g(from)f(a)i(script.)0 3741 y Fl(8.2)121 b(Exceptions)0 3973 y Fs(Ev)o(en)19 b(if)i(a)f(statement)g(or)g(e)o(xpression)f(is)i(syntactically)f (correct,)f(it)i(may)f(cause)g(an)g(error)f(when)h(an)g(attempt)g(is)h (made)f(to)g(e)o(x)o(ecute)f(it.)0 4073 y(Errors)k(detected)h(during)e (e)o(x)o(ecution)g(are)i(called)g Fq(e)n(xceptions)g Fs(and)g(are)g(not)g(unconditionally)d(f)o(atal:)33 b(you)23 b(will)i(soon)f(learn)g(ho)n(w)f(to)0 4173 y(handle)17 b(them)h(in)g(Python)f(programs.)22 b(Most)d(e)o(xceptions)d(are)i(not) g(handled)e(by)i(programs,)f(ho)n(we)n(v)o(er)m(,)f(and)h(result)h(in)g (error)f(messages)0 4272 y(as)k(sho)n(wn)e(here:)p 0 5549 3901 4 v 3808 5649 a Fo(57)p eop end %%Page: 58 66 TeXDict begin 58 65 bop 236 174 a Fk(>>>)45 b(10)550 187 y(*)640 174 y(\(1/0\))236 266 y(Traceback)f(\(most)g(recent)f(call) i(last\):)326 357 y(File)f("",)f(line)h(1,)h(in)f(?)236 448 y(ZeroDivisionError:)e(integer)i(division)f(or)i(modulo)e(by)i (zero)236 540 y(>>>)g(4)f(+)h(spam)775 553 y(*)820 540 y(3)236 631 y(Traceback)f(\(most)g(recent)f(call)i(last\):)326 722 y(File)f("",)f(line)h(1,)h(in)f(?)236 814 y(NameError:)f(name)i('spam')e(is)i(not)f(defined)236 905 y(>>>)h('2')f(+)h(2)236 996 y(Traceback)f(\(most)g(recent)f(call)i (last\):)326 1088 y(File)f("",)f(line)h(1,)h(in)f(?)236 1179 y(TypeError:)f(cannot)h(concatenate)f('str')h(and)g('int')g (objects)0 1465 y Fs(The)18 b(last)i(line)f(of)g(the)f(error)g(message) h(indicates)f(what)h(happened.)j(Exceptions)c(come)g(in)h(dif)n(ferent) e(types,)i(and)f(the)h(type)f(is)i(printed)0 1565 y(as)k(part)e(of)g (the)h(message:)31 b(the)22 b(types)h(in)g(the)g(e)o(xample)e(are)i Fp(ZeroDivisionError)p Fs(,)d Fp(NameError)i Fs(and)g Fp(TypeError)p Fs(.)32 b(The)0 1664 y(string)27 b(printed)f(as)i(the)g (e)o(xception)d(type)i(is)h(the)g(name)f(of)g(the)g(b)n(uilt-in)g(e)o (xception)e(that)j(occurred.)44 b(This)28 b(is)g(true)f(for)g(all)h(b)n (uilt-in)0 1764 y(e)o(xceptions,)23 b(b)n(ut)h(need)f(not)g(be)h(true)f (for)g(user)n(-de\002ned)f(e)o(xceptions)h(\(although)e(it)k(is)f(a)h (useful)e(con)m(v)o(ention\).)32 b(Standard)22 b(e)o(xception)0 1864 y(names)e(are)g(b)n(uilt-in)g(identi\002ers)f(\(not)h(reserv)o(ed) f(k)o(e)o(yw)o(ords\).)0 2010 y(The)h(rest)g(of)g(the)h(line)f(pro)o (vides)e(detail)j(based)e(on)h(the)g(type)g(of)g(e)o(xception)e(and)i (what)g(caused)g(it.)0 2157 y(The)30 b(preceding)d(part)j(of)g(the)g (error)f(message)g(sho)n(ws)h(the)g(conte)o(xt)f(where)g(the)h(e)o (xception)e(happened,)i(in)g(the)g(form)f(of)h(a)g(stack)0 2257 y(traceback.)43 b(In)27 b(general)e(it)j(contains)e(a)h(stack)g (traceback)e(listing)i(source)f(lines;)31 b(ho)n(we)n(v)o(er)m(,)26 b(it)h(will)h(not)e(display)g(lines)h(read)g(from)0 2357 y(standard)19 b(input.)0 2503 y(The)h Fq(Python)f(Libr)o(ary)h(Refer)m (ence)g Fs(lists)h(the)f(b)n(uilt-in)g(e)o(xceptions)f(and)g(their)h (meanings.)0 2831 y Fl(8.3)121 b(Handling)35 b(Exceptions)0 3064 y Fs(It)24 b(is)g(possible)f(to)g(write)h(programs)d(that)i (handle)g(selected)g(e)o(xceptions.)33 b(Look)22 b(at)h(the)h(follo)n (wing)d(e)o(xample,)i(which)g(asks)g(the)h(user)0 3163 y(for)30 b(input)g(until)h(a)h(v)n(alid)e(inte)o(ger)g(has)h(been)f (entered,)j(b)n(ut)d(allo)n(ws)i(the)f(user)f(to)h(interrupt)f(the)h (program)e(\(using)h Fp(Control-C)0 3263 y Fs(or)39 b(whate)n(v)o(er)e (the)i(operating)e(system)j(supports\);)47 b(note)38 b(that)h(a)h(user)n(-generated)c(interruption)h(is)j(signalled)e(by)h (raising)g(the)0 3363 y Fp(KeyboardInterrupt)18 b Fs(e)o(xception.)236 3601 y Fk(>>>)45 b(while)f(True:)236 3692 y(...)224 b(try:)236 3783 y(...)403 b(x)45 b(=)g(int\(raw_input\("Please)c(enter)j(a)g (number:)g("\)\))236 3875 y(...)403 b(break)236 3966 y(...)224 b(except)44 b(ValueError:)236 4057 y(...)403 b(print)44 b("Oops!)89 b(That)44 b(was)g(no)h(valid)f(number.)88 b(Try)45 b(again...")236 4149 y(...)0 4435 y Fs(The)20 b Fp(try)g Fs(statement)g(w)o(orks)g(as)h(follo)n(ws.)137 4665 y(\225)42 b(First,)20 b(the)h Fq(try)g(clause)e Fs(\(the)h(statement\(s\))g(between)f(the)h Fp(try)h Fs(and)e Fp(except)h Fs(k)o(e)o(yw)o(ords\))e(is)j(e)o(x)o(ecuted.)137 4831 y(\225)42 b(If)20 b(no)f(e)o(xception)g(occurs,)g(the)h Fq(e)n(xcept)g(clause)g Fs(is)h(skipped)e(and)h(e)o(x)o(ecution)e(of)i (the)g Fp(try)g Fs(statement)g(is)h(\002nished.)137 4997 y(\225)42 b(If)28 b(an)h(e)o(xception)e(occurs)i(during)e(e)o(x)o (ecution)g(of)i(the)g(try)f(clause,)j(the)e(rest)h(of)e(the)h(clause)g (is)h(skipped.)51 b(Then)28 b(if)h(its)h(type)208 5097 y(matches)e(the)g(e)o(xception)f(named)g(after)h(the)g Fp(except)g Fs(k)o(e)o(yw)o(ord,)h(the)f(e)o(xcept)g(clause)g(is)h(e)o (x)o(ecuted,)g(and)f(then)g(e)o(x)o(ecution)208 5196 y(continues)19 b(after)g(the)i Fp(try)f Fs(statement.)137 5362 y(\225)42 b(If)22 b(an)g(e)o(xception)f(occurs)h(which)g(does)g (not)g(match)g(the)g(e)o(xception)f(named)g(in)i(the)f(e)o(xcept)g (clause,)h(it)g(is)g(passed)g(on)f(to)g(outer)p 0 5549 3901 4 v 0 5649 a Fo(58)2529 b(Chapter)23 b(8.)52 b(Errors)22 b(and)i(Exceptions)p eop end %%Page: 59 67 TeXDict begin 59 66 bop 208 83 a Fp(try)28 b Fs(statements;)k(if)d(no)e (handler)g(is)i(found,)g(it)g(is)g(an)f Fq(unhandled)d(e)n(xception)j Fs(and)f(e)o(x)o(ecution)f(stops)j(with)f(a)h(message)f(as)208 183 y(sho)n(wn)19 b(abo)o(v)o(e.)0 413 y(A)26 b Fp(try)g Fs(statement)g(may)f(ha)n(v)o(e)h(more)f(than)g(one)g(e)o(xcept)g (clause,)i(to)f(specify)f(handlers)g(for)g(dif)n(ferent)f(e)o (xceptions.)41 b(At)26 b(most)g(one)0 512 y(handler)g(will)i(be)f(e)o (x)o(ecuted.)45 b(Handlers)27 b(only)f(handle)g(e)o(xceptions)g(that)i (occur)e(in)h(the)h(corresponding)c(try)j(clause,)i(not)e(in)g(other)0 612 y(handlers)e(of)g(the)h(same)g Fp(try)g Fs(statement.)41 b(An)26 b(e)o(xcept)f(clause)g(may)h(name)f(multiple)g(e)o(xceptions)f (as)j(a)f(parenthesized)e(tuple,)i(for)0 711 y(e)o(xample:)236 950 y Fk(...)45 b(except)e(\(RuntimeError,)g(TypeError,)g(NameError\):) 236 1041 y(...)224 b(pass)0 1327 y Fs(The)20 b(last)h(e)o(xcept)f (clause)g(may)g(omit)h(the)f(e)o(xception)f(name\(s\),)g(to)h(serv)o(e) g(as)i(a)e(wildcard.)25 b(Use)c(this)g(with)g(e)o(xtreme)e(caution,)g (since)i(it)0 1427 y(is)j(easy)f(to)g(mask)f(a)i(real)e(programming)e (error)i(in)h(this)g(w)o(ay!)33 b(It)23 b(can)g(also)g(be)f(used)h(to)g (print)f(an)h(error)f(message)g(and)h(then)f(re-raise)0 1526 y(the)e(e)o(xception)f(\(allo)n(wing)f(a)j(caller)f(to)h(handle)e (the)h(e)o(xception)e(as)j(well\):)236 1765 y Fk(import)44 b(sys)236 1947 y(try:)416 2039 y(f)g(=)h(open\('myfile.txt'\))416 2130 y(s)f(=)h(f.readline\(\))416 2221 y(i)f(=)h(int\(s.strip\(\)\))236 2313 y(except)f(IOError,)g(\(errno,)f(strerror\):)416 2404 y(print)h("I/O)g(error\(\045s\):)f(\045s")h(\045)h(\(errno,)e (strerror\))236 2495 y(except)h(ValueError:)416 2586 y(print)g("Could)f(not)i(convert)e(data)i(to)f(an)g(integer.")236 2678 y(except:)416 2769 y(print)g("Unexpected)f(error:",)g (sys.exc_info\(\)[0])416 2860 y(raise)0 3147 y Fs(The)22 b Fp(try)h Fs(.)12 b(.)g(.)36 b Fp(except)22 b Fs(statement)h(has)g(an) g(optional)e Fq(else)i(clause)p Fs(,)g(which,)g(when)f(present,)g(must) h(follo)n(w)f(all)i(e)o(xcept)d(clauses.)33 b(It)0 3246 y(is)21 b(useful)f(for)f(code)h(that)g(must)g(be)g(e)o(x)o(ecuted)f(if) h(the)h(try)e(clause)i(does)f(not)g(raise)g(an)g(e)o(xception.)j(F)o (or)d(e)o(xample:)236 3484 y Fk(for)45 b(arg)f(in)g(sys.argv[1:]:)416 3576 y(try:)595 3667 y(f)h(=)f(open\(arg,)g('r'\))416 3758 y(except)f(IOError:)595 3850 y(print)h('cannot)g(open',)f(arg)416 3941 y(else:)595 4032 y(print)h(arg,)g('has',)g (len\(f.readlines\(\)\),)d('lines')595 4124 y(f.close\(\))0 4410 y Fs(The)27 b(use)h(of)f(the)h Fp(else)f Fs(clause)h(is)g(better)f (than)h(adding)e(additional)g(code)h(to)g(the)h Fp(try)g Fs(clause)f(because)g(it)h(a)n(v)n(oids)g(accidentally)0 4510 y(catching)19 b(an)h(e)o(xception)e(that)j(w)o(asn')o(t)f(raised)g (by)f(the)i(code)e(being)g(protected)g(by)h(the)g Fp(try)g Fs(.)12 b(.)g(.)33 b Fp(except)20 b Fs(statement.)0 4656 y(When)k(an)h(e)o(xception)e(occurs,)h(it)h(may)f(ha)n(v)o(e)g(an)h (associated)f(v)n(alue,)h(also)g(kno)n(wn)e(as)i(the)g(e)o(xception')-5 b(s)23 b Fq(ar)m(gument)p Fs(.)36 b(The)25 b(presence)0 4756 y(and)20 b(type)f(of)h(the)g(ar)o(gument)e(depend)h(on)h(the)g(e)o (xception)e(type.)0 4903 y(The)28 b(e)o(xcept)g(clause)h(may)f(specify) g(a)h(v)n(ariable)e(after)i(the)f(e)o(xception)f(name)h(\(or)g (tuple\).)49 b(The)29 b(v)n(ariable)e(is)j(bound)d(to)h(an)h(e)o(xcep-) 0 5003 y(tion)23 b(instance)g(with)g(the)g(ar)o(guments)f(stored)g(in)i Fp(instance.args)p Fs(.)32 b(F)o(or)23 b(con)m(v)o(enience,)d(the)j(e)o (xception)f(instance)h(de\002nes)g Fp(__-)0 5102 y(getitem__)17 b Fs(and)h Fp(__str__)f Fs(so)i(the)f(ar)o(guments)e(can)i(be)g (accessed)g(or)g(printed)f(directly)h(without)f(ha)n(ving)g(to)i (reference)d Fp(.args)p Fs(.)0 5249 y(But)23 b(use)g(of)g Fp(.args)f Fs(is)i(discouraged.)30 b(Instead,)22 b(the)h(preferred)e (use)i(is)g(to)g(pass)g(a)h(single)e(ar)o(gument)f(to)h(an)h(e)o (xception)e(\(which)h(can)0 5349 y(be)e(a)g(tuple)f(if)h(multiple)f(ar) o(guments)f(are)h(needed\))f(and)h(ha)n(v)o(e)g(it)i(bound)d(to)h(the)h Fp(message)f Fs(attrib)n(ute.)24 b(One)c(may)f(also)h(instantiate)g(an) p 0 5549 3901 4 v 0 5649 a Fo(8.3.)52 b(Handling)25 b(Exceptions)2861 b(59)p eop end %%Page: 60 68 TeXDict begin 60 67 bop 0 83 a Fs(e)o(xception)18 b(\002rst)j(before)e (raising)h(it)h(and)e(add)h(an)o(y)f(attrib)n(utes)h(to)h(it)g(as)g (desired.)236 321 y Fk(>>>)45 b(try:)236 413 y(...)179 b(raise)44 b(Exception\('spam',)e('eggs'\))236 504 y(...)j(except)e (Exception,)h(inst:)236 595 y(...)179 b(print)44 b(type\(inst\))223 b(#)44 b(the)h(exception)e(instance)236 687 y(...)179 b(print)44 b(inst.args)268 b(#)44 b(arguments)f(stored)h(in)h(.args)236 778 y(...)179 b(print)44 b(inst)493 b(#)44 b(__str__)g(allows)g(args)g (to)g(printed)g(directly)236 869 y(...)179 b(x,)45 b(y)f(=)h(inst)448 b(#)44 b(__getitem__)f(allows)h(args)g(to)h(be)f(unpacked)g(directly) 236 960 y(...)179 b(print)44 b('x)h(=',)f(x)236 1052 y(...)179 b(print)44 b('y)h(=',)f(y)236 1143 y(...)236 1234 y()236 1326 y(\('spam',)g('eggs'\)) 236 1417 y(\('spam',)g('eggs'\))236 1508 y(x)h(=)g(spam)236 1600 y(y)g(=)g(eggs)0 1886 y Fs(If)20 b(an)g(e)o(xception)e(has)j(an)f (ar)o(gument,)e(it)j(is)g(printed)e(as)i(the)f(last)h(part)f (\(`detail'\))e(of)i(the)h(message)f(for)f(unhandled)f(e)o(xceptions.)0 2033 y(Exception)26 b(handlers)h(don')o(t)g(just)i(handle)e(e)o (xceptions)f(if)j(the)o(y)e(occur)h(immediately)e(in)j(the)f(try)g (clause,)i(b)n(ut)e(also)h(if)f(the)o(y)g(occur)0 2132 y(inside)20 b(functions)f(that)h(are)g(called)g(\(e)n(v)o(en)f (indirectly\))g(in)h(the)g(try)g(clause.)25 b(F)o(or)20 b(e)o(xample:)236 2371 y Fk(>>>)45 b(def)f(this_fails\(\):)236 2462 y(...)224 b(x)45 b(=)f(1/0)236 2553 y(...)236 2645 y(>>>)h(try:)236 2736 y(...)224 b(this_fails\(\))236 2827 y(...)45 b(except)e(ZeroDivisionError,)f(detail:)236 2919 y(...)224 b(print)44 b('Handling)f(run-time)h(error:',)f(detail) 236 3010 y(...)236 3101 y(Handling)h(run-time)f(error:)h(integer)g (division)f(or)i(modulo)e(by)i(zero)0 3547 y Fl(8.4)121 b(Raising)34 b(Exceptions)0 3780 y Fs(The)20 b Fp(raise)g Fs(statement)g(allo)n(ws)g(the)g(programmer)d(to)k(force)e(a)i (speci\002ed)f(e)o(xception)e(to)i(occur)-5 b(.)25 b(F)o(or)19 b(e)o(xample:)236 4018 y Fk(>>>)45 b(raise)f(NameError,)f('HiThere')236 4109 y(Traceback)h(\(most)g(recent)f(call)i(last\):)326 4201 y(File)f("",)f(line)h(1,)h(in)f(?)236 4292 y(NameError:)f(HiThere)0 4578 y Fs(The)17 b(\002rst)h(ar)o(gument)d(to) i Fp(raise)g Fs(names)g(the)h(e)o(xception)d(to)j(be)f(raised.)24 b(The)17 b(optional)f(second)g(ar)o(gument)f(speci\002es)j(the)f(e)o (xception')-5 b(s)0 4678 y(ar)o(gument.)21 b(Alternati)n(v)o(ely)-5 b(,)16 b(the)h(abo)o(v)o(e)e(could)h(be)h(written)g(as)g Fp(raise)49 b(NameError\('HiThere'\))p Fs(.)21 b(Either)16 b(form)g(w)o(orks)h(\002ne,)0 4777 y(b)n(ut)j(there)g(seems)h(to)f(be)g (a)h(gro)n(wing)d(stylistic)j(preference)d(for)i(the)g(latter)-5 b(.)0 4924 y(If)22 b(you)e(need)h(to)h(determine)e(whether)h(an)h(e)o (xception)d(w)o(as)k(raised)e(b)n(ut)h(don')o(t)e(intend)h(to)h(handle) e(it,)j(a)f(simpler)f(form)g(of)g(the)h Fp(raise)0 5024 y Fs(statement)e(allo)n(ws)g(you)g(to)g(re-raise)g(the)g(e)o(xception:) p 0 5549 3901 4 v 0 5649 a Fo(60)2529 b(Chapter)23 b(8.)52 b(Errors)22 b(and)i(Exceptions)p eop end %%Page: 61 69 TeXDict begin 61 68 bop 236 174 a Fk(>>>)45 b(try:)236 266 y(...)224 b(raise)44 b(NameError,)f('HiThere')236 357 y(...)i(except)e(NameError:)236 448 y(...)224 b(print)44 b('An)g(exception)g(flew)g(by!')236 540 y(...)224 b(raise)236 631 y(...)236 722 y(An)45 b(exception)e(flew)h(by!)236 814 y(Traceback)g(\(most)g(recent)f(call)i(last\):)326 905 y(File)f("",)f(line)h(2,)h(in)f(?)236 996 y(NameError:)f(HiThere)0 1445 y Fl(8.5)121 b(User-de\002ned)35 b(Exceptions)0 1678 y Fs(Programs)21 b(may)h(name)g(their)h(o)n(wn)f(e) o(xceptions)f(by)h(creating)f(a)i(ne)n(w)g(e)o(xception)d(class.)33 b(Exceptions)22 b(should)f(typically)h(be)g(deri)n(v)o(ed)0 1777 y(from)d(the)h Fp(Exception)f Fs(class,)i(either)f(directly)g(or)f (indirectly)-5 b(.)24 b(F)o(or)c(e)o(xample:)236 2015 y Fk(>>>)45 b(class)f(MyError\(Exception\):)236 2107 y(...)224 b(def)44 b(__init__\(self,)f(value\):)236 2198 y(...)403 b(self.value)43 b(=)i(value)236 2289 y(...)224 b(def)44 b(__str__\(self\):)236 2381 y(...)403 b(return)44 b(repr\(self.value\))236 2472 y(...)236 2563 y(>>>)h(try:)236 2655 y(...)224 b(raise)44 b(MyError\(2)1269 2668 y(*)1314 2655 y(2\))236 2746 y(...)h(except)e(MyError,)h(e:)236 2837 y(...)224 b(print)44 b('My)g(exception)g(occurred,)f(value:',)g (e.value)236 2929 y(...)236 3020 y(My)i(exception)e(occurred,)g(value:) h(4)236 3111 y(>>>)h(raise)f(MyError,)f('oops!')236 3203 y(Traceback)h(\(most)g(recent)f(call)i(last\):)326 3294 y(File)f("",)f(line)h(1,)h(in)f(?)236 3385 y(__main__.MyError:)e ('oops!')0 3672 y Fs(In)24 b(this)g(e)o(xample,)f(the)h(def)o(ault)g Fp(__init__)f Fs(of)g Fp(Exception)g Fs(has)h(been)f(o)o(v)o(erridden.) 33 b(The)24 b(ne)n(w)g(beha)n(vior)e(simply)i(creates)g(the)0 3771 y Fq(value)c Fs(attrib)n(ute.)k(This)c(replaces)g(the)g(def)o (ault)g(beha)n(vior)e(of)i(creating)g(the)g Fq(ar)m(gs)g Fs(attrib)n(ute.)0 3918 y(Exception)k(classes)j(can)e(be)h(de\002ned)f (which)g(do)g(an)o(ything)f(an)o(y)g(other)h(class)i(can)e(do,)i(b)n (ut)f(are)f(usually)g(k)o(ept)h(simple,)h(often)d(only)0 4018 y(of)n(fering)f(a)j(number)e(of)i(attrib)n(utes)f(that)h(allo)n(w) f(information)e(about)i(the)h(error)e(to)i(be)f(e)o(xtracted)g(by)g (handlers)f(for)h(the)h(e)o(xception.)0 4117 y(When)17 b(creating)f(a)i(module)e(that)h(can)g(raise)h(se)n(v)o(eral)e (distinct)i(errors,)e(a)i(common)d(practice)i(is)h(to)f(create)g(a)h (base)f(class)i(for)d(e)o(xceptions)0 4217 y(de\002ned)j(by)h(that)g (module,)f(and)g(subclass)i(that)f(to)h(create)e(speci\002c)i(e)o (xception)d(classes)j(for)f(dif)n(ferent)f(error)g(conditions:)p 0 5549 3901 4 v 0 5649 a Fo(8.5.)52 b(User-de\002ned)22 b(Exceptions)2714 b(61)p eop end %%Page: 62 70 TeXDict begin 62 69 bop 236 174 a Fk(class)44 b(Error\(Exception\):)416 266 y("""Base)f(class)h(for)h(exceptions)e(in)h(this)g(module.""")416 357 y(pass)236 540 y(class)g(InputError\(Error\):)416 631 y("""Exception)e(raised)i(for)h(errors)e(in)i(the)f(input.)416 814 y(Attributes:)595 905 y(expression)f(--)i(input)f(expression)f(in)h (which)g(the)g(error)g(occurred)595 996 y(message)g(--)g(explanation)f (of)i(the)f(error)416 1088 y(""")416 1270 y(def)g(__init__\(self,)e (expression,)h(message\):)595 1362 y(self.expression)f(=)j(expression) 595 1453 y(self.message)e(=)h(message)236 1636 y(class)g (TransitionError\(Error\):)416 1727 y("""Raised)f(when)h(an)h (operation)e(attempts)g(a)i(state)f(transition)f(that's)h(not)416 1818 y(allowed.)416 2001 y(Attributes:)595 2092 y(previous)f(--)i (state)f(at)g(beginning)g(of)g(transition)595 2183 y(next)g(--)h (attempted)e(new)h(state)595 2275 y(message)g(--)g(explanation)f(of)i (why)f(the)g(specific)g(transition)f(is)h(not)h(allowed)416 2366 y(""")416 2549 y(def)f(__init__\(self,)e(previous,)i(next,)g (message\):)595 2640 y(self.previous)f(=)h(previous)595 2731 y(self.next)f(=)i(next)595 2823 y(self.message)e(=)h(message)0 3109 y Fs(Most)20 b(e)o(xceptions)f(are)h(de\002ned)f(with)i(names)f (that)g(end)f(in)i(\223Error)m(,)-6 b(\224)18 b(similar)j(to)f(the)g (naming)f(of)h(the)g(standard)f(e)o(xceptions.)0 3256 y(Man)o(y)25 b(standard)g(modules)g(de\002ne)g(their)h(o)n(wn)f(e)o (xceptions)g(to)h(report)f(errors)g(that)h(may)f(occur)g(in)i (functions)d(the)o(y)h(de\002ne.)42 b(More)0 3355 y(information)18 b(on)i(classes)h(is)g(presented)e(in)h(chapter)f(9,)h(\223Classes.)-6 b(\224)0 3683 y Fl(8.6)121 b(De\002ning)34 b(Clean-up)i(Actions)0 3916 y Fs(The)27 b Fp(try)g Fs(statement)g(has)h(another)e(optional)g (clause)h(which)g(is)h(intended)e(to)i(de\002ne)f(clean-up)e(actions)i (that)h(must)f(be)g(e)o(x)o(ecuted)0 4015 y(under)19 b(all)i(circumstances.)i(F)o(or)d(e)o(xample:)236 4253 y Fk(>>>)45 b(try:)236 4345 y(...)224 b(raise)44 b(KeyboardInterrupt) 236 4436 y(...)h(finally:)236 4527 y(...)224 b(print)44 b('Goodbye,)f(world!')236 4619 y(...)236 4710 y(Goodbye,)h(world!)236 4801 y(Traceback)g(\(most)g(recent)f(call)i(last\):)326 4893 y(File)f("",)f(line)h(2,)h(in)f(?)236 4984 y(KeyboardInterrupt)0 5270 y Fs(A)18 b Fq(\002nally)f(clause)g Fs(is)i(al)o(w)o(ays)f(e)o(x)o(ecuted)e(before)g(lea)n(ving)i(the)f Fp(try)h Fs(statement,)g(whether)e(an)i(e)o(xception)e(has)i(occurred)e (or)h(not.)24 b(When)0 5370 y(an)e(e)o(xception)f(has)i(occurred)d(in)j (the)f Fp(try)h Fs(clause)f(and)g(has)h(not)f(been)g(handled)f(by)h(an) g Fp(except)g Fs(clause)g(\(or)g(it)h(has)g(occurred)e(in)h(a)p 0 5549 3901 4 v 0 5649 a Fo(62)2529 b(Chapter)23 b(8.)52 b(Errors)22 b(and)i(Exceptions)p eop end %%Page: 63 71 TeXDict begin 63 70 bop 0 83 a Fp(except)22 b Fs(or)g Fp(else)g Fs(clause\),)g(it)h(is)g(re-raised)e(after)h(the)g Fp(finally)g Fs(clause)g(has)h(been)e(e)o(x)o(ecuted.)29 b(The)22 b Fp(finally)g Fs(clause)g(is)h(also)0 183 y(e)o(x)o(ecuted)d (\223on)h(the)h(w)o(ay)g(out\224)g(when)f(an)o(y)g(other)g(clause)h(of) g(the)g Fp(try)g Fs(statement)f(is)i(left)f(via)g(a)h Fp(break)p Fs(,)e Fp(continue)g Fs(or)h Fp(return)0 282 y Fs(statement.)36 b(A)24 b(more)f(complicated)f(e)o(xample)h(\(ha)n (ving)f Fp(except)i Fs(and)f Fp(finally)g Fs(clauses)h(in)g(the)g(same) g Fp(try)g Fs(statement)g(w)o(orks)0 382 y(as)d(of)f(Python)f(2.5\):) 236 620 y Fk(>>>)45 b(def)f(divide\(x,)f(y\):)236 711 y(...)224 b(try:)236 803 y(...)403 b(result)44 b(=)h(x)f(/)h(y)236 894 y(...)224 b(except)44 b(ZeroDivisionError:)236 985 y(...)403 b(print)44 b("division)g(by)g(zero!")236 1077 y(...)224 b(else:)236 1168 y(...)403 b(print)44 b("result)g(is",)g (result)236 1259 y(...)224 b(finally:)236 1351 y(...)403 b(print)44 b("executing)f(finally)h(clause")236 1442 y(...)236 1533 y(>>>)h(divide\(2,)e(1\))236 1625 y(result)h(is)h(2)236 1716 y(executing)f(finally)f(clause)236 1807 y(>>>)i(divide\(2,)e(0\)) 236 1899 y(division)h(by)g(zero!)236 1990 y(executing)g(finally)f (clause)236 2081 y(>>>)i(divide\("2",)e("1"\))236 2173 y(executing)h(finally)f(clause)236 2264 y(Traceback)h(\(most)g(recent)f (call)i(last\):)326 2355 y(File)f("",)f(line)h(1,)h(in)f(?)326 2447 y(File)g("",)f(line)h(3,)h(in)f(divide)236 2538 y(TypeError:)f(unsupported)g(operand)h(type\(s\))g(for)g(/:)g ('str')g(and)h('str')0 2824 y Fs(As)21 b(you)e(can)g(see,)i(the)f Fp(finally)f Fs(clause)h(is)h(e)o(x)o(ecuted)d(in)i(an)o(y)f(e)n(v)o (ent.)24 b(The)c Fp(TypeError)e Fs(raised)i(by)g(di)n(viding)e(tw)o(o)i (strings)g(is)h(not)0 2924 y(handled)e(by)g(the)i Fp(except)e Fs(clause)h(and)g(therefore)e(re-raised)i(after)f(the)i Fp(finally)e Fs(clauses)i(has)f(been)g(e)o(x)o(ecuted.)0 3071 y(In)27 b(real)g(w)o(orld)f(applications,)i(the)f Fp(finally)f Fs(clause)h(is)h(useful)f(for)f(releasing)h(e)o(xternal)f (resources)g(\(such)g(as)i(\002les)g(or)f(netw)o(ork)0 3170 y(connections\),)18 b(re)o(gardless)g(of)i(whether)f(the)i(use)f (of)g(the)g(resource)f(w)o(as)i(successful.)0 3498 y Fl(8.7)121 b(Prede\002ned)35 b(Clean-up)g(Actions)0 3730 y Fs(Some)26 b(objects)h(de\002ne)f(standard)f(clean-up)h(actions)g(to) h(be)f(undertak)o(en)f(when)h(the)h(object)f(is)h(no)g(longer)e (needed,)i(re)o(gardless)e(of)0 3830 y(whether)20 b(or)g(not)g(the)h (operation)e(using)h(the)g(object)h(succeeded)e(or)h(f)o(ailed.)26 b(Look)20 b(at)h(the)f(follo)n(wing)f(e)o(xample,)h(which)g(tries)h(to) g(open)0 3930 y(a)g(\002le)f(and)g(print)g(its)h(contents)e(to)i(the)f (screen.)236 4168 y Fk(for)45 b(line)f(in)g(open\("myfile.txt"\):)416 4259 y(print)g(line)0 4545 y Fs(The)17 b(problem)e(with)i(this)h(code)f (is)h(that)f(it)h(lea)n(v)o(es)f(the)g(\002le)h(open)e(for)g(an)h (indeterminate)f(amount)f(of)i(time)g(after)g(the)g(code)g(has)g (\002nished)0 4645 y(e)o(x)o(ecuting.)37 b(This)25 b(is)h(not)e(an)h (issue)g(in)g(simple)g(scripts,)h(b)n(ut)f(can)g(be)g(a)g(problem)e (for)h(lar)o(ger)g(applications.)38 b(The)24 b Fp(with)h Fs(statement)0 4745 y(allo)n(ws)20 b(objects)g(lik)o(e)h(\002les)g(to)f (be)g(used)g(in)h(a)f(w)o(ay)h(that)f(ensures)g(the)o(y)f(are)h(al)o(w) o(ays)h(cleaned)e(up)h(promptly)e(and)i(correctly)-5 b(.)236 4983 y Fk(with)44 b(open\("myfile.txt"\))e(as)j(f:)416 5074 y(for)f(line)g(in)h(f:)595 5165 y(print)f(line)p 0 5549 3901 4 v 0 5649 a Fo(8.7.)52 b(Prede\002ned)24 b(Clean-up)g(Actions)2561 b(63)p eop end %%Page: 64 72 TeXDict begin 64 71 bop 0 83 a Fs(After)25 b(the)f(statement)h(is)h(e)o (x)o(ecuted,)d(the)i(\002le)h Fq(f)37 b Fs(is)26 b(al)o(w)o(ays)f (closed,)h(e)n(v)o(en)d(if)i(a)h(problem)d(w)o(as)i(encountered)d (while)j(processing)f(the)0 183 y(lines.)h(Other)20 b(objects)g(which)g (pro)o(vide)e(prede\002ned)g(clean-up)h(actions)h(will)h(indicate)e (this)i(in)f(their)g(documentation.)0 330 y Fr(Note:)44 b Fs(Since)30 b Fp(with)f Fs(is)i(a)f(ne)n(w)g(language)e(k)o(e)o(yw)o (ord,)i(it)h(must)f(be)f(enabled)g(by)g(e)o(x)o(ecuting)f Fp(from)49 b(__future__)f(import)0 429 y(with_statement)18 b Fs(in)i(Python)g(2.5.)k(From)19 b(2.6)h(on,)g(it)g(will)h(al)o(w)o (ays)g(be)f(enabled.)p 0 5549 3901 4 v 0 5649 a Fo(64)2529 b(Chapter)23 b(8.)52 b(Errors)22 b(and)i(Exceptions)p eop end %%Page: 65 73 TeXDict begin 65 72 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3614 427 y Fn(NINE)p 0 515 V 3108 978 a Fy(Classes)0 1457 y Fs(Python')-5 b(s)21 b(class)h(mechanism)e(adds)h(classes)h(to)g(the) f(language)f(with)h(a)h(minimum)e(of)h(ne)n(w)g(syntax)f(and)h (semantics.)28 b(It)22 b(is)g(a)g(mixture)0 1556 y(of)d(the)h(class)g (mechanisms)e(found)g(in)i(C)1176 1549 y(++)1290 1556 y(and)f(Modula-3.)j(As)f(is)f(true)f(for)g(modules,)f(classes)j(in)e (Python)g(do)g(not)g(put)g(an)g(absolute)0 1656 y(barrier)28 b(between)g(de\002nition)g(and)g(user)m(,)j(b)n(ut)e(rather)f(rely)g (on)h(the)g(politeness)f(of)h(the)g(user)g(not)f(to)h(\223break)f(into) h(the)g(de\002nition.)-6 b(\224)0 1756 y(The)20 b(most)h(important)d (features)i(of)g(classes)i(are)e(retained)g(with)g(full)h(po)n(wer)m(,) e(ho)n(we)n(v)o(er:)k(the)e(class)g(inheritance)e(mechanism)g(allo)n (ws)0 1855 y(multiple)i(base)h(classes,)h(a)g(deri)n(v)o(ed)d(class)i (can)g(o)o(v)o(erride)e(an)o(y)h(methods)g(of)g(its)i(base)f(class)h (or)e(classes,)j(and)d(a)h(method)f(can)g(call)i(the)0 1955 y(method)c(of)h(a)g(base)h(class)g(with)f(the)g(same)h(name.)j (Objects)c(can)g(contain)g(an)g(arbitrary)e(amount)h(of)h(pri)n(v)n (ate)f(data.)0 2102 y(In)g(C)144 2095 y(++)258 2102 y(terminology)-5 b(,)17 b(all)j(class)g(members)f(\(including)e(the)j(data)f(members\))f (are)i Fq(public)p Fs(,)e(and)h(all)h(member)e(functions)g(are)i Fq(virtual)p Fs(.)0 2201 y(There)h(are)g(no)g(special)h(constructors)d (or)j(destructors.)27 b(As)22 b(in)g(Modula-3,)d(there)i(are)h(no)f (shorthands)e(for)i(referencing)e(the)j(object')-5 b(s)0 2301 y(members)25 b(from)g(its)i(methods:)36 b(the)26 b(method)f(function)f(is)j(declared)e(with)h(an)g(e)o(xplicit)g (\002rst)h(ar)o(gument)c(representing)h(the)i(object,)0 2401 y(which)c(is)i(pro)o(vided)d(implicitly)h(by)h(the)g(call.)34 b(As)24 b(in)f(Smalltalk,)g(classes)h(themselv)o(es)e(are)h(objects,)h (albeit)f(in)g(the)g(wider)f(sense)i(of)0 2500 y(the)j(w)o(ord:)38 b(in)27 b(Python,)h(all)f(data)g(types)g(are)g(objects.)45 b(This)27 b(pro)o(vides)e(semantics)i(for)f(importing)f(and)i (renaming.)43 b(Unlik)o(e)27 b(C)3806 2493 y(++)0 2600 y(and)e(Modula-3,)h(b)n(uilt-in)f(types)h(can)g(be)g(used)f(as)i(base)f (classes)h(for)f(e)o(xtension)e(by)i(the)g(user)-5 b(.)42 b(Also,)28 b(lik)o(e)e(in)h(C)3351 2593 y(++)3471 2600 y(b)n(ut)f(unlik)o(e)f(in)0 2699 y(Modula-3,)h(most)g(b)n(uilt-in)g (operators)f(with)i(special)f(syntax)g(\(arithmetic)g(operators,)g (subscripting)f(etc.\))44 b(can)26 b(be)g(rede\002ned)f(for)0 2799 y(class)c(instances.)0 3126 y Fl(9.1)121 b(A)33 b(W)l(ord)h(About)h(T)-14 b(er)s(minology)0 3359 y Fs(Lacking)28 b(uni)n(v)o(ersally)g(accepted)h(terminology)e(to)j(talk)g(about)f (classes,)k(I)d(will)h(mak)o(e)e(occasional)g(use)h(of)f(Smalltalk)h (and)f(C)3806 3352 y(++)0 3459 y(terms.)f(\(I)21 b(w)o(ould)g(use)g (Modula-3)e(terms,)j(since)f(its)h(object-oriented)d(semantics)i(are)g (closer)g(to)g(those)h(of)e(Python)h(than)f(C)3609 3452 y(++)3703 3459 y(,)i(b)n(ut)f(I)0 3559 y(e)o(xpect)e(that)h(fe)n(w)h (readers)e(ha)n(v)o(e)h(heard)f(of)h(it.\))0 3705 y(Objects)j(ha)n(v)o (e)g(indi)n(viduality)-5 b(,)21 b(and)i(multiple)f(names)h(\(in)g (multiple)g(scopes\))g(can)g(be)g(bound)e(to)j(the)f(same)g(object.)34 b(This)23 b(is)h(kno)n(wn)0 3805 y(as)h(aliasing)f(in)g(other)f (languages.)35 b(This)24 b(is)h(usually)f(not)g(appreciated)e(on)h(a)i (\002rst)g(glance)e(at)i(Python,)e(and)h(can)g(be)g(safely)f(ignored)0 3905 y(when)29 b(dealing)g(with)i(immutable)d(basic)i(types)g (\(numbers,)h(strings,)h(tuples\).)54 b(Ho)n(we)n(v)o(er)m(,)30 b(aliasing)g(has)g(an)g(\(intended!\))52 b(ef)n(fect)0 4004 y(on)28 b(the)f(semantics)h(of)g(Python)f(code)g(in)m(v)n(olving)f (mutable)h(objects)h(such)f(as)i(lists,)i(dictionaries,)e(and)e(most)h (types)g(representing)0 4104 y(entities)f(outside)f(the)h(program)e (\(\002les,)j(windo)n(ws,)g(etc.\).)44 b(This)27 b(is)h(usually)e(used) h(to)f(the)h(bene\002t)g(of)f(the)h(program,)f(since)h(aliases)0 4204 y(beha)n(v)o(e)c(lik)o(e)h(pointers)f(in)h(some)g(respects.)36 b(F)o(or)23 b(e)o(xample,)g(passing)h(an)g(object)f(is)i(cheap)e(since) h(only)f(a)i(pointer)d(is)j(passed)f(by)g(the)0 4303 y(implementation;)29 b(and)e(if)h(a)g(function)e(modi\002es)i(an)f (object)g(passed)h(as)g(an)g(ar)o(gument,)f(the)g(caller)h(will)h(see)f (the)f(change)g(\227)h(this)0 4403 y(eliminates)20 b(the)g(need)g(for)f (tw)o(o)i(dif)n(ferent)d(ar)o(gument)g(passing)i(mechanisms)f(as)i(in)f (P)o(ascal.)0 4730 y Fl(9.2)121 b(Python)34 b(Scopes)g(and)h(Name)f (Spaces)0 4963 y Fs(Before)21 b(introducing)f(classes,)j(I)f(\002rst)h (ha)n(v)o(e)f(to)g(tell)h(you)e(something)f(about)h(Python')-5 b(s)22 b(scope)f(rules.)31 b(Class)23 b(de\002nitions)e(play)h(some)0 5063 y(neat)17 b(tricks)h(with)f(namespaces,)g(and)g(you)g(need)f(to)i (kno)n(w)e(ho)n(w)h(scopes)g(and)g(namespaces)g(w)o(ork)f(to)i(fully)f (understand)e(what')-5 b(s)18 b(going)0 5162 y(on.)25 b(Incidentally)-5 b(,)17 b(kno)n(wledge)h(about)h(this)i(subject)f(is)h (useful)f(for)f(an)o(y)h(adv)n(anced)e(Python)h(programmer)-5 b(.)0 5309 y(Let')g(s)21 b(be)o(gin)e(with)h(some)g(de\002nitions.)p 0 5549 3901 4 v 3808 5649 a Fo(65)p eop end %%Page: 66 74 TeXDict begin 66 73 bop 0 83 a Fs(A)17 b Fq(namespace)e Fs(is)i(a)g(mapping)e(from)g(names)h(to)h(objects.)23 b(Most)17 b(namespaces)f(are)g(currently)f(implemented)f(as)k(Python)d (dictionaries,)0 183 y(b)n(ut)21 b(that')-5 b(s)21 b(normally)f(not)g (noticeable)g(in)h(an)o(y)f(w)o(ay)h(\(e)o(xcept)e(for)i (performance\),)c(and)k(it)g(may)g(change)e(in)i(the)g(future.)26 b(Examples)20 b(of)0 282 y(namespaces)g(are:)26 b(the)20 b(set)i(of)e(b)n(uilt-in)g(names)g(\(functions)f(such)h(as)i Fp(abs\(\))p Fs(,)e(and)g(b)n(uilt-in)g(e)o(xception)e(names\);)j(the)f (global)g(names)0 382 y(in)27 b(a)g(module;)i(and)d(the)h(local)f (names)h(in)g(a)g(function)e(in)m(v)n(ocation.)42 b(In)27 b(a)g(sense)g(the)g(set)g(of)g(attrib)n(utes)f(of)h(an)f(object)g(also) h(form)f(a)0 482 y(namespace.)35 b(The)24 b(important)f(thing)g(to)h (kno)n(w)f(about)g(namespaces)h(is)h(that)f(there)f(is)i(absolutely)e (no)h(relation)f(between)h(names)f(in)0 581 y(dif)n(ferent)e (namespaces;)i(for)g(instance,)f(tw)o(o)h(dif)n(ferent)e(modules)h(may) h(both)e(de\002ne)i(a)g(function)e(\223maximize\224)h(without)g (confusion)0 681 y(\227)f(users)f(of)g(the)g(modules)f(must)i(pre\002x) e(it)i(with)f(the)h(module)d(name.)0 828 y(By)h(the)g(w)o(ay)-5 b(,)18 b(I)h(use)f(the)h(w)o(ord)f Fq(attrib)n(ute)g Fs(for)g(an)o(y)g(name)g(follo)n(wing)f(a)i(dot)f(\227)h(for)f(e)o (xample,)f(in)i(the)f(e)o(xpression)f Fp(z.real)p Fs(,)h Fp(real)h Fs(is)0 927 y(an)d(attrib)n(ute)g(of)g(the)g(object)g Fp(z)p Fs(.)24 b(Strictly)16 b(speaking,)g(references)f(to)h(names)g (in)g(modules)g(are)g(attrib)n(ute)g(references:)21 b(in)c(the)f(e)o (xpression)0 1027 y Fp(modname.funcname)p Fs(,)h Fp(modname)j Fs(is)h(a)f(module)f(object)h(and)f Fp(funcname)g Fs(is)i(an)f(attrib)n (ute)g(of)g(it.)25 b(In)20 b(this)h(case)f(there)g(happens)0 1127 y(to)26 b(be)g(a)h(straightforw)o(ard)c(mapping)i(between)g(the)h (module')-5 b(s)25 b(attrib)n(utes)h(and)f(the)h(global)g(names)f (de\002ned)g(in)i(the)f(module:)35 b(the)o(y)0 1226 y(share)20 b(the)g(same)g(namespace!)927 1196 y Fi(1)0 1373 y Fs(Attrib)n(utes)i (may)g(be)g(read-only)d(or)j(writable.)30 b(In)22 b(the)g(latter)g (case,)h(assignment)e(to)i(attrib)n(utes)f(is)h(possible.)30 b(Module)21 b(attrib)n(utes)h(are)0 1473 y(writable:)38 b(you)25 b(can)i(write)f(`)p Fp(modname.the_answer)46 b(=)k(42)p Fs('.)44 b(Writable)26 b(attrib)n(utes)h(may)f(also)h(be)f (deleted)g(with)h(the)g Fp(del)0 1572 y Fs(statement.)35 b(F)o(or)23 b(e)o(xample,)g(`)p Fp(del)49 b(modname.the_answer)p Fs(')21 b(will)j(remo)o(v)o(e)e(the)h(attrib)n(ute)h Fp(the_answer)e Fs(from)g(the)i(object)0 1672 y(named)19 b(by)h Fp(modname)p Fs(.)0 1819 y(Name)27 b(spaces)h(are)f(created)g (at)h(dif)n(ferent)e(moments)g(and)h(ha)n(v)o(e)g(dif)n(ferent)e (lifetimes.)47 b(The)27 b(namespace)f(containing)g(the)h(b)n(uilt-in)0 1918 y(names)h(is)i(created)e(when)g(the)h(Python)e(interpreter)g (starts)j(up,)g(and)e(is)i(ne)n(v)o(er)d(deleted.)50 b(The)28 b(global)g(namespace)g(for)g(a)h(module)0 2018 y(is)f(created)f(when)f(the)i(module)e(de\002nition)g(is)i(read)f(in;)k (normally)-5 b(,)27 b(module)f(namespaces)g(also)i(last)g(until)f(the)g (interpreter)f(quits.)0 2118 y(The)g(statements)g(e)o(x)o(ecuted)e(by)i (the)g(top-le)n(v)o(el)f(in)m(v)n(ocation)f(of)i(the)g(interpreter)m(,) f(either)h(read)g(from)f(a)h(script)g(\002le)h(or)f(interacti)n(v)o (ely)-5 b(,)0 2217 y(are)26 b(considered)e(part)i(of)g(a)g(module)f (called)h Fp(__main__)p Fs(,)g(so)h(the)o(y)e(ha)n(v)o(e)g(their)h(o)n (wn)g(global)f(namespace.)41 b(\(The)25 b(b)n(uilt-in)h(names)0 2317 y(actually)20 b(also)g(li)n(v)o(e)g(in)g(a)h(module;)e(this)i(is)g (called)f Fp(__builtin__)p Fs(.\))0 2464 y(The)25 b(local)g(namespace)f (for)g(a)i(function)d(is)k(created)d(when)h(the)g(function)e(is)j (called,)g(and)f(deleted)f(when)h(the)g(function)f(returns)g(or)0 2563 y(raises)j(an)g(e)o(xception)d(that)j(is)g(not)f(handled)f(within) i(the)f(function.)42 b(\(Actually)-5 b(,)27 b(for)o(getting)d(w)o(ould) i(be)g(a)h(better)f(w)o(ay)h(to)f(describe)0 2663 y(what)20 b(actually)g(happens.\))j(Of)d(course,)f(recursi)n(v)o(e)g(in)m(v)n (ocations)g(each)h(ha)n(v)o(e)f(their)h(o)n(wn)g(local)g(namespace.)0 2810 y(A)i Fq(scope)f Fs(is)i(a)f(te)o(xtual)e(re)o(gion)g(of)i(a)g (Python)e(program)f(where)i(a)h(namespace)e(is)j(directly)e (accessible.)29 b(\223Directly)21 b(accessible\224)g(here)0 2910 y(means)f(that)g(an)g(unquali\002ed)f(reference)f(to)i(a)h(name)f (attempts)g(to)g(\002nd)g(the)g(name)g(in)g(the)g(namespace.)0 3056 y(Although)f(scopes)h(are)h(determined)e(statically)-5 b(,)20 b(the)o(y)g(are)h(used)f(dynamically)-5 b(.)24 b(At)e(an)o(y)e(time)g(during)f(e)o(x)o(ecution,)g(there)h(are)h(at)g (least)0 3156 y(three)h(nested)h(scopes)f(whose)h(namespaces)f(are)g (directly)g(accessible:)31 b(the)23 b(innermost)e(scope,)i(which)f(is)i (searched)d(\002rst,)j(contains)0 3256 y(the)i(local)f(names;)j(the)e (namespaces)e(of)i(an)o(y)e(enclosing)h(functions,)g(which)g(are)g (searched)g(starting)g(with)h(the)g(nearest)f(enclosing)0 3355 y(scope;)17 b(the)f(middle)g(scope,)g(searched)f(ne)o(xt,)h (contains)g(the)g(current)f(module')-5 b(s)15 b(global)g(names;)j(and)d (the)h(outermost)f(scope)h(\(searched)0 3455 y(last\))21 b(is)g(the)f(namespace)f(containing)f(b)n(uilt-in)i(names.)0 3602 y(If)32 b(a)h(name)f(is)h(declared)e(global,)k(then)c(all)i (references)e(and)h(assignments)g(go)g(directly)g(to)g(the)h(middle)e (scope)h(containing)f(the)0 3701 y(module')-5 b(s)25 b(global)h(names.)43 b(Otherwise,)27 b(all)g(v)n(ariables)e(found)g (outside)g(of)h(the)h(innermost)e(scope)g(are)i(read-only)d(\(an)i (attempt)f(to)0 3801 y(write)e(to)f(such)g(a)h(v)n(ariable)e(will)j (simply)e(create)g(a)h Fq(ne)o(w)f Fs(local)g(v)n(ariable)g(in)g(the)h (innermost)e(scope,)h(lea)n(ving)g(the)g(identically)g(named)0 3901 y(outer)d(v)n(ariable)g(unchanged\).)0 4048 y(Usually)-5 b(,)28 b(the)f(local)f(scope)h(references)e(the)i(local)g(names)f(of)h (the)g(\(te)o(xtually\))e(current)g(function.)43 b(Outside)27 b(functions,)g(the)g(local)0 4147 y(scope)17 b(references)e(the)j(same) f(namespace)f(as)i(the)f(global)f(scope:)23 b(the)17 b(module')-5 b(s)17 b(namespace.)22 b(Class)d(de\002nitions)d(place)h (yet)g(another)0 4247 y(namespace)i(in)h(the)h(local)f(scope.)0 4394 y(It)29 b(is)h(important)e(to)h(realize)g(that)g(scopes)g(are)g (determined)e(te)o(xtually:)42 b(the)29 b(global)f(scope)h(of)f(a)i (function)d(de\002ned)h(in)h(a)h(module)0 4493 y(is)f(that)f(module')-5 b(s)27 b(namespace,)i(no)e(matter)h(from)f(where)g(or)h(by)g(what)f (alias)i(the)f(function)f(is)h(called.)49 b(On)28 b(the)g(other)f (hand,)i(the)0 4593 y(actual)18 b(search)g(for)g(names)g(is)h(done)f (dynamically)-5 b(,)16 b(at)j(run)f(time)g(\227)h(ho)n(we)n(v)o(er)m(,) d(the)j(language)d(de\002nition)i(is)h(e)n(v)n(olving)e(to)n(w)o(ards)h (static)0 4693 y(name)25 b(resolution,)h(at)g(\223compile\224)e(time,)j (so)f(don')o(t)e(rely)i(on)f(dynamic)f(name)h(resolution!)40 b(\(In)25 b(f)o(act,)i(local)f(v)n(ariables)f(are)h(already)0 4792 y(determined)18 b(statically)-5 b(.\))0 4939 y(A)24 b(special)f(quirk)f(of)g(Python)g(is)i(that)f(assignments)g(al)o(w)o (ays)g(go)g(into)g(the)g(innermost)f(scope.)33 b(Assignments)22 b(do)h(not)g(cop)o(y)f(data)h(\227)0 5039 y(the)o(y)c(just)h(bind)f (names)g(to)h(objects.)25 b(The)19 b(same)h(is)h(true)e(for)g (deletions:)25 b(the)19 b(statement)h(`)p Fp(del)49 b(x)p Fs(')19 b(remo)o(v)o(es)f(the)i(binding)e(of)i Fp(x)g Fs(from)p 0 5110 1560 4 v 90 5165 a Fh(1)120 5189 y Fm(Except)k(for)g (one)g(thing.)42 b(Module)25 b(objects)h(ha)o(v)o(e)e(a)g(secret)h (read-only)h(attrib)o(ute)g(called)g Fa(__dict__)c Fm(which)j(returns)g (the)f(dictionary)j(used)d(to)g(implement)0 5267 y(the)c(module')l(s)h (namespace;)j(the)c(name)g Fa(__dict__)e Fm(is)i(an)g(attrib)o(ute)i(b) o(ut)e(not)g(a)g(global)h(name.)29 b(Ob)o(viously)l(,)21 b(using)g(this)f(violates)i(the)e(abstraction)j(of)d(namespace)0 5346 y(implementation,)g(and)e(should)f(be)h(restricted)h(to)f(things)g (lik)o(e)g(post-mortem)g(deb)o(uggers.)p 0 5549 3901 4 v 0 5649 a Fo(66)3050 b(Chapter)24 b(9.)52 b(Classes)p eop end %%Page: 67 75 TeXDict begin 67 74 bop 0 83 a Fs(the)27 b(namespace)f(referenced)e(by) j(the)g(local)f(scope.)45 b(In)27 b(f)o(act,)h(all)g(operations)d(that) i(introduce)e(ne)n(w)h(names)h(use)g(the)g(local)g(scope:)0 183 y(in)f(particular)m(,)f(import)g(statements)g(and)g(function)f (de\002nitions)h(bind)g(the)h(module)e(or)h(function)f(name)h(in)h(the) g(local)f(scope.)41 b(\(The)0 282 y Fp(global)20 b Fs(statement)g(can)g (be)g(used)g(to)g(indicate)g(that)g(particular)f(v)n(ariables)g(li)n(v) o(e)h(in)g(the)h(global)e(scope.\))0 610 y Fl(9.3)121 b(A)33 b(First)f(Look)j(at)f(Classes)0 843 y Fs(Classes)22 b(introduce)c(a)j(little)g(bit)f(of)g(ne)n(w)g(syntax,)f(three)h(ne)n (w)g(object)g(types,)g(and)f(some)h(ne)n(w)g(semantics.)0 1127 y Fj(9.3.1)100 b(Class)28 b(De\002nition)h(Syntax)0 1330 y Fs(The)20 b(simplest)g(form)g(of)f(class)j(de\002nition)d(looks) g(lik)o(e)i(this:)236 1569 y Fk(class)44 b(ClassName:)416 1660 y()416 1751 y(.)416 1842 y(.)416 1934 y(.)416 2025 y()0 2311 y Fs(Class)21 b(de\002nitions,)e (lik)o(e)h(function)e(de\002nitions)h(\()p Fp(def)g Fs(statements\))h (must)g(be)f(e)o(x)o(ecuted)f(before)g(the)o(y)h(ha)n(v)o(e)h(an)o(y)f (ef)n(fect.)24 b(\(Y)-9 b(ou)19 b(could)0 2411 y(concei)n(v)n(ably)f (place)i(a)g(class)h(de\002nition)f(in)g(a)g(branch)f(of)h(an)g Fp(if)h Fs(statement,)e(or)h(inside)g(a)h(function.\))0 2558 y(In)15 b(practice,)h(the)g(statements)f(inside)h(a)g(class)g (de\002nition)f(will)h(usually)f(be)h(function)d(de\002nitions,)j(b)n (ut)g(other)e(statements)i(are)f(allo)n(wed,)0 2657 y(and)j(sometimes)h (useful)f(\227)h(we')o(ll)g(come)g(back)f(to)h(this)g(later)-5 b(.)25 b(The)18 b(function)f(de\002nitions)h(inside)h(a)g(class)h (normally)d(ha)n(v)o(e)h(a)i(peculiar)0 2757 y(form)f(of)h(ar)o(gument) e(list,)j(dictated)f(by)f(the)i(calling)e(con)m(v)o(entions)f(for)h (methods)g(\227)i(again,)e(this)i(is)g(e)o(xplained)d(later)-5 b(.)0 2904 y(When)19 b(a)h(class)h(de\002nition)d(is)j(entered,)d(a)i (ne)n(w)f(namespace)g(is)h(created,)f(and)g(used)g(as)i(the)e(local)h (scope)f(\227)h(thus,)f(all)h(assignments)f(to)0 3004 y(local)h(v)n(ariables)g(go)f(into)h(this)h(ne)n(w)f(namespace.)k(In)c (particular)m(,)e(function)g(de\002nitions)i(bind)f(the)h(name)g(of)g (the)g(ne)n(w)g(function)e(here.)0 3150 y(When)j(a)g(class)h (de\002nition)e(is)i(left)f(normally)f(\(via)g(the)h(end\),)f(a)i Fq(class)g(object)e Fs(is)i(created.)27 b(This)21 b(is)h(basically)e(a) i(wrapper)d(around)h(the)0 3250 y(contents)e(of)h(the)h(namespace)e (created)g(by)h(the)g(class)h(de\002nition;)f(we')o(ll)g(learn)g(more)f (about)g(class)j(objects)e(in)g(the)g(ne)o(xt)g(section.)24 b(The)0 3350 y(original)c(local)h(scope)g(\(the)g(one)g(in)g(ef)n(fect) g(just)h(before)e(the)h(class)h(de\002nition)f(w)o(as)h(entered\))e(is) i(reinstated,)f(and)f(the)i(class)g(object)f(is)0 3449 y(bound)d(here)i(to)g(the)g(class)i(name)d(gi)n(v)o(en)g(in)h(the)h (class)g(de\002nition)e(header)g(\()p Fp(ClassName)g Fs(in)h(the)g(e)o(xample\).)0 3734 y Fj(9.3.2)100 b(Class)28 b(Objects)0 3937 y Fs(Class)22 b(objects)e(support)e(tw)o(o)j(kinds)f (of)f(operations:)24 b(attrib)n(ute)c(references)f(and)g (instantiation.)0 4084 y Fq(Attrib)n(ute)26 b(r)m(efer)m(ences)f Fs(use)h(the)f(standard)g(syntax)f(used)i(for)e(all)j(attrib)n(ute)e (references)f(in)h(Python:)35 b Fp(obj.name)p Fs(.)40 b(V)-9 b(alid)25 b(attrib)n(ute)0 4184 y(names)17 b(are)g(all)h(the)f (names)g(that)h(were)f(in)g(the)h(class')-5 b(s)18 b(namespace)e(when)h (the)g(class)i(object)d(w)o(as)i(created.)24 b(So,)18 b(if)f(the)g(class)i(de\002nition)0 4283 y(look)o(ed)g(lik)o(e)h(this:) 236 4430 y Fk(class)44 b(MyClass:)416 4521 y("A)g(simple)g(example)g (class")416 4613 y(i)g(=)h(12345)416 4704 y(def)f(f\(self\):)595 4795 y(return)g('hello)g(world')0 5082 y Fs(then)30 b Fp(MyClass.i)g Fs(and)h Fp(MyClass.f)e Fs(are)i(v)n(alid)f(attrib)n (ute)h(references,)h(returning)d(an)i(inte)o(ger)e(and)i(a)g(function)e (object,)k(re-)0 5181 y(specti)n(v)o(ely)-5 b(.)52 b(Class)32 b(attrib)n(utes)d(can)h(also)g(be)g(assigned)f(to,)j(so)e(you)f(can)h (change)e(the)i(v)n(alue)f(of)h Fp(MyClass.i)e Fs(by)i(assignment.)0 5281 y Fp(__doc__)18 b Fs(is)h(also)f(a)h(v)n(alid)f(attrib)n(ute,)g (returning)e(the)j(docstring)e(belonging)f(to)i(the)g(class:)26 b Fp("A)49 b(simple)g(example)f(class")p Fs(.)p 0 5549 3901 4 v 0 5649 a Fo(9.3.)k(A)24 b(First)e(Look)h(at)g(Classes)2764 b(67)p eop end %%Page: 68 76 TeXDict begin 68 75 bop 0 83 a Fs(Class)24 b Fq(instantiation)d Fs(uses)i(function)e(notation.)30 b(Just)24 b(pretend)d(that)h(the)h (class)g(object)f(is)i(a)f(parameterless)e(function)g(that)i(returns)e (a)0 183 y(ne)n(w)f(instance)g(of)g(the)g(class.)26 b(F)o(or)20 b(e)o(xample)e(\(assuming)h(the)i(abo)o(v)o(e)d(class\):)236 330 y Fk(x)45 b(=)g(MyClass\(\))0 616 y Fs(creates)20 b(a)h(ne)n(w)f Fq(instance)f Fs(of)h(the)g(class)i(and)d(assigns)i (this)g(object)e(to)i(the)f(local)g(v)n(ariable)f Fp(x)p Fs(.)0 763 y(The)h(instantiation)f(operation)g(\(\223calling\224)g(a)h (class)i(object\))d(creates)h(an)g(empty)g(object.)k(Man)o(y)19 b(classes)j(lik)o(e)e(to)g(create)g(objects)g(with)0 862 y(instances)i(customized)e(to)i(a)h(speci\002c)f(initial)g(state.) 31 b(Therefore)20 b(a)i(class)h(may)e(de\002ne)g(a)i(special)f(method)e (named)h Fp(__init__\(\))p Fs(,)0 962 y(lik)o(e)f(this:)416 1109 y Fk(def)44 b(__init__\(self\):)595 1200 y(self.data)f(=)i([])0 1486 y Fs(When)34 b(a)g(class)g(de\002nes)g(an)g Fp(__init__\(\))e Fs(method,)j(class)g(instantiation)e(automatically)f(in)m(v)n(ok)o(es)h Fp(__init__\(\))f Fs(for)i(the)0 1586 y(ne)n(wly-created)18 b(class)j(instance.)k(So)20 b(in)g(this)h(e)o(xample,)e(a)h(ne)n(w)-5 b(,)20 b(initialized)f(instance)h(can)g(be)g(obtained)f(by:)236 1824 y Fk(x)45 b(=)g(MyClass\(\))0 2110 y Fs(Of)21 b(course,)f(the)h Fp(__init__\(\))f Fs(method)f(may)i(ha)n(v)o(e)f(ar)o(guments)f(for)h (greater)g(\003e)o(xibility)-5 b(.)26 b(In)21 b(that)g(case,)g(ar)o (guments)e(gi)n(v)o(en)g(to)j(the)0 2210 y(class)f(instantiation)e (operator)g(are)h(passed)g(on)g(to)g Fp(__init__\(\))p Fs(.)k(F)o(or)c(e)o(xample,)236 2357 y Fk(>>>)45 b(class)f(Complex:)236 2448 y(...)224 b(def)44 b(__init__\(self,)f(realpart,)g(imagpart\):)236 2539 y(...)403 b(self.r)44 b(=)h(realpart)236 2631 y(...)403 b(self.i)44 b(=)h(imagpart)236 2722 y(...)236 2813 y(>>>)g(x)f(=)h (Complex\(3.0,)e(-4.5\))236 2905 y(>>>)i(x.r,)f(x.i)236 2996 y(\(3.0,)g(-4.5\))0 3421 y Fj(9.3.3)100 b(Instance)28 b(Objects)0 3623 y Fs(No)n(w)d(what)g(can)g(we)h(do)e(with)i(instance)e (objects?)40 b(The)25 b(only)f(operations)g(understood)e(by)j(instance) g(objects)g(are)g(attrib)n(ute)f(refer)n(-)0 3723 y(ences.)h(There)19 b(are)h(tw)o(o)h(kinds)e(of)h(v)n(alid)g(attrib)n(ute)g(names,)g(data)g (attrib)n(utes)g(and)f(methods.)0 3870 y Fq(data)k(attrib)n(utes)h Fs(correspond)d(to)i(\223instance)g(v)n(ariables\224)g(in)h(Smalltalk,) g(and)f(to)h(\223data)f(members\224)f(in)i(C)3080 3863 y(++)3174 3870 y(.)36 b(Data)24 b(attrib)n(utes)f(need)0 3970 y(not)c(be)g(declared;)g(lik)o(e)h(local)f(v)n(ariables,)g(the)o (y)f(spring)h(into)g(e)o(xistence)g(when)g(the)o(y)f(are)i(\002rst)g (assigned)f(to.)25 b(F)o(or)19 b(e)o(xample,)f(if)h Fp(x)h Fs(is)h(the)0 4069 y(instance)f(of)g Fp(MyClass)f Fs(created)h(abo)o(v) o(e,)e(the)i(follo)n(wing)f(piece)h(of)f(code)h(will)h(print)f(the)g(v) n(alue)f Fp(16)p Fs(,)h(without)g(lea)n(ving)f(a)i(trace:)236 4216 y Fk(x.counter)44 b(=)g(1)236 4307 y(while)g(x.counter)g(<)g(10:) 416 4399 y(x.counter)f(=)i(x.counter)1402 4412 y(*)1492 4399 y(2)236 4490 y(print)f(x.counter)236 4581 y(del)h(x.counter)0 4868 y Fs(The)26 b(other)f(kind)h(of)g(instance)g(attrib)n(ute)g (reference)f(is)i(a)g Fq(method)p Fs(.)42 b(A)27 b(method)e(is)i(a)g (function)d(that)j(\223belongs)e(to\224)h(an)g(object.)43 b(\(In)0 4967 y(Python,)22 b(the)g(term)g(method)f(is)i(not)g(unique)d (to)j(class)g(instances:)30 b(other)22 b(object)f(types)i(can)f(ha)n(v) o(e)g(methods)f(as)i(well.)32 b(F)o(or)22 b(e)o(xample,)0 5067 y(list)i(objects)f(ha)n(v)o(e)f(methods)g(called)h(append,)e (insert,)j(remo)o(v)o(e,)d(sort,)j(and)e(so)i(on.)32 b(Ho)n(we)n(v)o(er)m(,)22 b(in)h(the)g(follo)n(wing)e(discussion,)i (we')o(ll)0 5166 y(use)d(the)h(term)f(method)e(e)o(xclusi)n(v)o(ely)h (to)h(mean)g(methods)f(of)h(class)h(instance)f(objects,)f(unless)i(e)o (xplicitly)e(stated)h(otherwise.\))0 5313 y(V)-9 b(alid)22 b(method)f(names)g(of)h(an)g(instance)g(object)f(depend)g(on)g(its)i (class.)32 b(By)22 b(de\002nition,)f(all)i(attrib)n(utes)f(of)g(a)g (class)h(that)f(are)g(function)p 0 5549 3901 4 v 0 5649 a Fo(68)3050 b(Chapter)24 b(9.)52 b(Classes)p eop end %%Page: 69 77 TeXDict begin 69 76 bop 0 83 a Fs(objects)28 b(de\002ne)g (corresponding)c(methods)k(of)f(its)j(instances.)49 b(So)28 b(in)g(our)g(e)o(xample,)g Fp(x.f)h Fs(is)g(a)g(v)n(alid)e(method)g (reference,)h(since)0 183 y Fp(MyClass.f)20 b Fs(is)h(a)h(function,)d (b)n(ut)h Fp(x.i)h Fs(is)h(not,)e(since)h Fp(MyClass.i)f Fs(is)i(not.)k(But)21 b Fp(x.f)g Fs(is)h(not)e(the)h(same)g(thing)f(as) i Fp(MyClass.f)0 282 y Fs(\227)f(it)g(is)g(a)f Fq(method)g(object)p Fs(,)f(not)h(a)g(function)f(object.)0 567 y Fj(9.3.4)100 b(Method)29 b(Objects)0 770 y Fs(Usually)-5 b(,)19 b(a)i(method)e(is)i (called)f(right)g(after)f(it)i(is)g(bound:)236 1008 y Fk(x.f\(\))0 1294 y Fs(In)e(the)g Fp(MyClass)f Fs(e)o(xample,)f(this)j (will)f(return)f(the)h(string)f Fp('hello)49 b(world')p Fs(.)24 b(Ho)n(we)n(v)o(er)m(,)17 b(it)j(is)g(not)e(necessary)g(to)h (call)h(a)f(method)0 1394 y(right)h(a)o(w)o(ay:)k Fp(x.f)d Fs(is)g(a)f(method)f(object,)h(and)f(can)h(be)g(stored)g(a)o(w)o(ay)g (and)g(called)g(at)g(a)h(later)f(time.)25 b(F)o(or)20 b(e)o(xample:)236 1632 y Fk(xf)45 b(=)f(x.f)236 1724 y(while)g(True:)416 1815 y(print)g(xf\(\))0 2101 y Fs(will)21 b(continue)e(to)h(print)g(`)p Fp(hello)48 b(world)p Fs(')20 b(until)g(the)g(end)f(of)h(time.)0 2248 y(What)j(e)o(xactly)f(happens)g (when)g(a)i(method)d(is)j(called?)33 b(Y)-9 b(ou)23 b(may)f(ha)n(v)o(e) h(noticed)f(that)h Fp(x.f\(\))f Fs(w)o(as)i(called)f(without)f(an)h(ar) o(gument)0 2348 y(abo)o(v)o(e,)29 b(e)n(v)o(en)f(though)f(the)i (function)f(de\002nition)g(for)g Fp(f)h Fs(speci\002ed)g(an)g(ar)o (gument.)49 b(What)29 b(happened)e(to)i(the)g(ar)o(gument?)49 b(Surely)0 2447 y(Python)23 b(raises)i(an)f(e)o(xception)f(when)h(a)g (function)f(that)h(requires)g(an)g(ar)o(gument)e(is)j(called)f(without) g(an)o(y)g(\227)h(e)n(v)o(en)e(if)i(the)f(ar)o(gument)0 2547 y(isn')o(t)c(actually)f(used...)0 2694 y(Actually)-5 b(,)25 b(you)g(may)f(ha)n(v)o(e)h(guessed)f(the)h(answer:)35 b(the)26 b(special)f(thing)f(about)g(methods)g(is)j(that)e(the)g (object)g(is)h(passed)f(as)h(the)f(\002rst)0 2793 y(ar)o(gument)h(of)j (the)g(function.)49 b(In)28 b(our)g(e)o(xample,)h(the)g(call)g Fp(x.f\(\))g Fs(is)h(e)o(xactly)d(equi)n(v)n(alent)g(to)i Fp(MyClass.f\(x\))p Fs(.)49 b(In)29 b(general,)0 2893 y(calling)24 b(a)g(method)f(with)h(a)g(list)h(of)f Fq(n)g Fs(ar)o(guments)e(is)j(equi)n(v)n(alent)d(to)i(calling)g(the)g (corresponding)c(function)j(with)h(an)g(ar)o(gument)d(list)0 2993 y(that)f(is)h(created)f(by)g(inserting)f(the)h(method')-5 b(s)19 b(object)h(before)f(the)h(\002rst)h(ar)o(gument.)0 3139 y(If)i(you)g(still)i(don')o(t)c(understand)h(ho)n(w)h(methods)f(w) o(ork,)i(a)g(look)e(at)i(the)g(implementation)d(can)i(perhaps)g (clarify)f(matters.)35 b(When)24 b(an)0 3239 y(instance)17 b(attrib)n(ute)h(is)g(referenced)e(that)h(isn')o(t)h(a)g(data)f(attrib) n(ute,)h(its)h(class)f(is)h(searched.)k(If)17 b(the)h(name)f(denotes)g (a)h(v)n(alid)f(class)i(attrib)n(ute)0 3339 y(that)g(is)h(a)f(function) e(object,)h(a)i(method)d(object)h(is)i(created)e(by)h(packing)e (\(pointers)h(to\))g(the)h(instance)g(object)f(and)g(the)h(function)e (object)0 3438 y(just)i(found)e(together)g(in)i(an)f(abstract)h (object:)k(this)d(is)f(the)g(method)e(object.)24 b(When)18 b(the)h(method)e(object)h(is)h(called)g(with)f(an)h(ar)o(gument)0 3538 y(list,)27 b(it)e(is)h(unpack)o(ed)c(again,)j(a)g(ne)n(w)f(ar)o (gument)f(list)j(is)f(constructed)e(from)h(the)h(instance)f(object)g (and)g(the)h(original)e(ar)o(gument)g(list,)0 3638 y(and)d(the)g (function)e(object)i(is)h(called)f(with)g(this)h(ne)n(w)f(ar)o(gument)e (list.)0 3965 y Fl(9.4)121 b(Random)35 b(Remar)r(ks)0 4198 y Fs(Data)24 b(attrib)n(utes)f(o)o(v)o(erride)f(method)g(attrib)n (utes)h(with)h(the)f(same)h(name;)h(to)f(a)n(v)n(oid)f(accidental)g (name)f(con\003icts,)j(which)e(may)g(cause)0 4297 y(hard-to-\002nd)c(b) n(ugs)j(in)g(lar)o(ge)e(programs,)h(it)h(is)h(wise)f(to)g(use)g(some)g (kind)f(of)h(con)m(v)o(ention)c(that)k(minimizes)g(the)f(chance)g(of)h (con\003icts.)0 4397 y(Possible)29 b(con)m(v)o(entions)c(include)j (capitalizing)f(method)g(names,)j(pre\002xing)d(data)h(attrib)n(ute)g (names)h(with)f(a)h(small)g(unique)e(string)0 4497 y(\(perhaps)19 b(just)h(an)g(underscore\),)e(or)i(using)f(v)o(erbs)h(for)f(methods)g (and)h(nouns)f(for)h(data)g(attrib)n(utes.)0 4644 y(Data)25 b(attrib)n(utes)h(may)e(be)h(referenced)e(by)i(methods)f(as)i(well)g (as)g(by)f(ordinary)e(users)i(\(\223clients\224\))g(of)f(an)i(object.) 39 b(In)25 b(other)f(w)o(ords,)0 4743 y(classes)h(are)f(not)g(usable)f (to)h(implement)f(pure)g(abstract)h(data)g(types.)36 b(In)24 b(f)o(act,)h(nothing)d(in)i(Python)f(mak)o(es)h(it)g(possible)g (to)g(enforce)0 4843 y(data)29 b(hiding)f(\227)h(it)h(is)g(all)f(based) g(upon)f(con)m(v)o(ention.)48 b(\(On)29 b(the)g(other)f(hand,)i(the)f (Python)e(implementation,)i(written)g(in)g(C,)h(can)0 4942 y(completely)19 b(hide)h(implementation)e(details)j(and)f(control) f(access)i(to)g(an)f(object)g(if)h(necessary;)e(this)i(can)g(be)f(used) g(by)g(e)o(xtensions)f(to)0 5042 y(Python)g(written)h(in)g(C.\))0 5189 y(Clients)h(should)d(use)i(data)g(attrib)n(utes)f(with)h(care)g (\227)g(clients)g(may)f(mess)i(up)e(in)m(v)n(ariants)f(maintained)h(by) g(the)h(methods)e(by)i(stamping)0 5289 y(on)j(their)g(data)f(attrib)n (utes.)34 b(Note)23 b(that)g(clients)h(may)e(add)h(data)g(attrib)n (utes)g(of)g(their)g(o)n(wn)f(to)h(an)g(instance)g(object)g(without)f (af)n(fecting)0 5388 y(the)27 b(v)n(alidity)f(of)h(the)f(methods,)i(as) f(long)f(as)i(name)e(con\003icts)h(are)g(a)n(v)n(oided)f(\227)h(again,) g(a)g(naming)f(con)m(v)o(ention)e(can)i(sa)n(v)o(e)h(a)h(lot)f(of)p 0 5549 3901 4 v 0 5649 a Fo(9.4.)52 b(Random)24 b(Remar)q(ks)2947 b(69)p eop end %%Page: 70 78 TeXDict begin 70 77 bop 0 83 a Fs(headaches)19 b(here.)0 230 y(There)g(is)i(no)e(shorthand)f(for)h(referencing)e(data)j(attrib)n (utes)f(\(or)h(other)e(methods!\))24 b(from)18 b(within)i(methods.)k(I) c(\002nd)f(that)h(this)g(actually)0 330 y(increases)30 b(the)g(readability)f(of)h(methods:)44 b(there)29 b(is)i(no)f(chance)f (of)h(confusing)e(local)i(v)n(ariables)g(and)f(instance)h(v)n(ariables) f(when)0 429 y(glancing)19 b(through)f(a)i(method.)0 576 y(Often,)25 b(the)g(\002rst)h(ar)o(gument)c(of)j(a)g(method)f(is)h (called)g Fp(self)p Fs(.)39 b(This)25 b(is)g(nothing)f(more)g(than)g(a) h(con)m(v)o(ention:)32 b(the)24 b(name)h Fp(self)f Fs(has)0 676 y(absolutely)19 b(no)h(special)g(meaning)e(to)i(Python.)k(\(Note,) 19 b(ho)n(we)n(v)o(er)m(,)f(that)i(by)g(not)f(follo)n(wing)g(the)h(con) m(v)o(ention)d(your)i(code)g(may)h(be)f(less)0 775 y(readable)j(to)h (other)g(Python)f(programmers,)f(and)h(it)i(is)g(also)g(concei)n(v)n (able)d(that)i(a)h Fq(class)g(br)l(owser)g Fs(program)d(might)h(be)i (written)e(that)0 875 y(relies)f(upon)d(such)i(a)h(con)m(v)o(ention.\)) 0 1022 y(An)o(y)k(function)f(object)h(that)h(is)g(a)g(class)h(attrib)n (ute)e(de\002nes)h(a)g(method)e(for)h(instances)h(of)f(that)h(class.)42 b(It)26 b(is)h(not)e(necessary)g(that)h(the)0 1121 y(function)e (de\002nition)g(is)i(te)o(xtually)e(enclosed)h(in)g(the)h(class)g (de\002nition:)34 b(assigning)25 b(a)h(function)d(object)i(to)h(a)f (local)h(v)n(ariable)e(in)i(the)0 1221 y(class)21 b(is)g(also)g(ok.)j (F)o(or)c(e)o(xample:)236 1459 y Fk(#)45 b(Function)e(defined)h (outside)g(the)g(class)236 1551 y(def)h(f1\(self,)e(x,)i(y\):)416 1642 y(return)e(min\(x,)h(x+y\))236 1825 y(class)g(C:)416 1916 y(f)g(=)h(f1)416 2007 y(def)f(g\(self\):)595 2098 y(return)g('hello)g(world')416 2190 y(h)g(=)h(g)0 2476 y Fs(No)n(w)25 b Fp(f)p Fs(,)h Fp(g)g Fs(and)e Fp(h)i Fs(are)f(all)g(attrib)n(utes)g(of)g(class)h Fp(C)f Fs(that)g(refer)g (to)g(function)e(objects,)j(and)e(consequently)f(the)o(y)h(are)h(all)h (methods)e(of)0 2576 y(instances)19 b(of)g Fp(C)h Fs(\227)g Fp(h)g Fs(being)e(e)o(xactly)h(equi)n(v)n(alent)e(to)j Fp(g)p Fs(.)25 b(Note)19 b(that)h(this)g(practice)e(usually)h(only)g (serv)o(es)g(to)g(confuse)f(the)i(reader)e(of)h(a)0 2675 y(program.)0 2822 y(Methods)g(may)h(call)h(other)e(methods)g(by)h (using)g(method)e(attrib)n(utes)j(of)e(the)i Fp(self)f Fs(ar)o(gument:)236 3060 y Fk(class)44 b(Bag:)416 3152 y(def)g(__init__\(self\):)595 3243 y(self.data)f(=)i([])416 3334 y(def)f(add\(self,)f(x\):)595 3426 y(self.data.append\(x\))416 3517 y(def)h(addtwice\(self,)e(x\):)595 3608 y(self.add\(x\))595 3700 y(self.add\(x\))0 3986 y Fs(Methods)29 b(may)f(reference)g(global) h(names)g(in)g(the)h(same)f(w)o(ay)h(as)g(ordinary)d(functions.)51 b(The)29 b(global)f(scope)h(associated)h(with)f(a)0 4085 y(method)c(is)i(the)f(module)e(containing)h(the)h(class)h (de\002nition.)41 b(\(The)25 b(class)i(itself)g(is)g(ne)n(v)o(er)e (used)h(as)g(a)h(global)e(scope!\))42 b(While)26 b(one)0 4185 y(rarely)20 b(encounters)e(a)j(good)e(reason)h(for)f(using)h (global)g(data)g(in)g(a)h(method,)e(there)h(are)g(man)o(y)f(le)o (gitimate)h(uses)h(of)f(the)g(global)g(scope:)0 4285 y(for)f(one)h(thing,)f(functions)g(and)g(modules)g(imported)g(into)h (the)g(global)f(scope)h(can)g(be)g(used)g(by)f(methods,)g(as)i(well)g (as)f(functions)f(and)0 4384 y(classes)28 b(de\002ned)d(in)i(it.)44 b(Usually)-5 b(,)27 b(the)g(class)g(containing)e(the)h(method)f(is)i (itself)g(de\002ned)f(in)g(this)h(global)f(scope,)h(and)f(in)h(the)f (ne)o(xt)0 4484 y(section)20 b(we')o(ll)g(\002nd)g(some)g(good)f (reasons)h(why)f(a)i(method)e(w)o(ould)g(w)o(ant)i(to)f(reference)e (its)k(o)n(wn)d(class!)0 4811 y Fl(9.5)121 b(Inher)r(itance)0 5044 y Fs(Of)19 b(course,)f(a)h(language)f(feature)g(w)o(ould)g(not)g (be)h(w)o(orthy)e(of)i(the)g(name)f(\223class\224)i(without)e (supporting)f(inheritance.)22 b(The)d(syntax)f(for)0 5144 y(a)j(deri)n(v)o(ed)d(class)j(de\002nition)e(looks)h(lik)o(e)g (this:)p 0 5549 3901 4 v 0 5649 a Fo(70)3050 b(Chapter)24 b(9.)52 b(Classes)p eop end %%Page: 71 79 TeXDict begin 71 78 bop 236 174 a Fk(class)44 b (DerivedClassName\(BaseClassName\):)416 266 y()416 357 y(.)416 448 y(.)416 540 y(.)416 631 y()0 917 y Fs(The)25 b(name)f Fp(BaseClassName)f Fs(must)i(be)g(de\002ned)f (in)h(a)g(scope)f(containing)f(the)i(deri)n(v)o(ed)e(class)j (de\002nition.)38 b(In)25 b(place)g(of)f(a)h(base)0 1017 y(class)30 b(name,)h(other)e(arbitrary)f(e)o(xpressions)g(are)h(also)h (allo)n(wed.)52 b(This)30 b(can)f(be)g(useful,)i(for)e(e)o(xample,)h (when)f(the)h(base)f(class)i(is)0 1116 y(de\002ned)19 b(in)i(another)d(module:)236 1355 y Fk(class)44 b (DerivedClassName\(modname.BaseClassName\):)0 1641 y Fs(Ex)o(ecution)18 b(of)h(a)h(deri)n(v)o(ed)e(class)i(de\002nition)f (proceeds)f(the)i(same)g(as)g(for)f(a)h(base)g(class.)25 b(When)20 b(the)g(class)g(object)f(is)i(constructed,)d(the)0 1740 y(base)25 b(class)g(is)g(remembered.)36 b(This)24 b(is)i(used)e(for)g(resolving)f(attrib)n(ute)h(references:)32 b(if)25 b(a)g(requested)e(attrib)n(ute)h(is)h(not)f(found)f(in)i(the)0 1840 y(class,)h(the)e(search)g(proceeds)e(to)i(look)g(in)g(the)g(base)g (class.)38 b(This)24 b(rule)g(is)h(applied)e(recursi)n(v)o(ely)f(if)i (the)g(base)h(class)g(itself)f(is)h(deri)n(v)o(ed)0 1940 y(from)19 b(some)h(other)g(class.)0 2087 y(There')-5 b(s)26 b(nothing)f(special)h(about)g(instantiation)f(of)h(deri)n(v)o (ed)f(classes:)39 b Fp(DerivedClassName\(\))23 b Fs(creates)k(a)g(ne)n (w)f(instance)g(of)0 2186 y(the)c(class.)33 b(Method)21 b(references)g(are)h(resolv)o(ed)f(as)i(follo)n(ws:)30 b(the)22 b(corresponding)d(class)k(attrib)n(ute)f(is)i(searched,)d (descending)g(do)n(wn)0 2286 y(the)f(chain)g(of)g(base)g(classes)h(if)g (necessary)-5 b(,)19 b(and)g(the)h(method)f(reference)g(is)i(v)n(alid)f (if)g(this)h(yields)f(a)h(function)d(object.)0 2433 y(Deri)n(v)o(ed)g (classes)j(may)f(o)o(v)o(erride)d(methods)i(of)h(their)f(base)h (classes.)26 b(Because)20 b(methods)f(ha)n(v)o(e)g(no)g(special)h(pri)n (vile)o(ges)f(when)g(calling)0 2532 y(other)k(methods)g(of)h(the)g (same)g(object,)g(a)h(method)e(of)g(a)i(base)f(class)h(that)f(calls)h (another)d(method)h(de\002ned)g(in)h(the)g(same)g(base)h(class)0 2632 y(may)e(end)h(up)f(calling)g(a)i(method)d(of)i(a)g(deri)n(v)o(ed)e (class)j(that)f(o)o(v)o(errides)e(it.)36 b(\(F)o(or)24 b(C)2420 2625 y(++)2538 2632 y(programmers:)30 b(all)24 b(methods)f(in)h(Python)f(are)0 2732 y(ef)n(fecti)n(v)o(ely)18 b Fp(virtual)p Fs(.\))0 2878 y(An)48 b(o)o(v)o(erriding)c(method)j(in)h (a)g(deri)n(v)o(ed)e(class)j(may)e(in)h(f)o(act)g(w)o(ant)g(to)g(e)o (xtend)e(rather)h(than)g(simply)h(replace)f(the)g(base)0 2978 y(class)f(method)d(of)h(the)h(same)g(name.)97 b(There)44 b(is)i(a)f(simple)g(w)o(ay)f(to)h(call)g(the)g(base)g(class)g(method)f (directly:)73 b(just)45 b(call)0 3078 y(`)p Fp (BaseClassName.methodname\(self,)f(arguments\))p Fs('.)37 b(This)25 b(is)h(occasionally)d(useful)i(to)g(clients)g(as)h(well.)39 b(\(Note)0 3177 y(that)20 b(this)h(only)e(w)o(orks)h(if)h(the)f(base)g (class)h(is)g(de\002ned)f(or)g(imported)e(directly)h(in)i(the)f(global) f(scope.\))0 3462 y Fj(9.5.1)100 b(Multiple)29 b(Inher)q(itance)0 3665 y Fs(Python)18 b(supports)g(a)h(limited)g(form)f(of)g(multiple)g (inheritance)g(as)h(well.)25 b(A)20 b(class)g(de\002nition)e(with)h (multiple)f(base)h(classes)h(looks)e(lik)o(e)0 3765 y(this:)236 4003 y Fk(class)44 b(DerivedClassName\(Base1,)d(Base2,)j(Base3\):)416 4094 y()416 4186 y(.)416 4277 y(.)416 4368 y(.)416 4460 y()0 4746 y Fs(F)o(or)57 b(old-style)f (classes,)67 b(the)57 b(only)g(rule)f(is)j(depth-\002rst,)65 b(left-to-right.)133 b(Thus,)66 b(if)57 b(an)g(attrib)n(ute)g(is)h(not) f(found)e(in)0 4845 y Fp(DerivedClassName)p Fs(,)31 b(it)h(is)g (searched)e(in)h Fp(Base1)p Fs(,)i(then)d(\(recursi)n(v)o(ely\))f(in)i (the)g(base)g(classes)h(of)f Fp(Base1)p Fs(,)i(and)d(only)h(if)g(it)0 4945 y(is)21 b(not)f(found)e(there,)i(it)h(is)g(searched)e(in)h Fp(Base2)p Fs(,)g(and)g(so)g(on.)0 5092 y(\(T)-7 b(o)30 b(some)g(people)e(breadth)h(\002rst)i(\227)f(searching)f Fp(Base2)g Fs(and)h Fp(Base3)f Fs(before)g(the)h(base)g(classes)h(of)f Fp(Base1)f Fs(\227)i(looks)e(more)0 5191 y(natural.)55 b(Ho)n(we)n(v)o(er)m(,)31 b(this)g(w)o(ould)f(require)f(you)g(to)i(kno) n(w)f(whether)f(a)i(particular)e(attrib)n(ute)h(of)g Fp(Base1)h Fs(is)g(actually)f(de\002ned)f(in)0 5291 y Fp(Base1)20 b Fs(or)g(in)g(one)g(of)g(its)h(base)f(classes)i(before)c (you)i(can)g(\002gure)f(out)h(the)g(consequences)e(of)i(a)h(name)f (con\003ict)f(with)i(an)f(attrib)n(ute)g(of)0 5391 y Fp(Base2)p Fs(.)25 b(The)19 b(depth-\002rst)g(rule)h(mak)o(es)g(no)g (dif)n(ferences)f(between)g(direct)h(and)f(inherited)g(attrib)n(utes)h (of)g Fp(Base1)p Fs(.\))p 0 5549 3901 4 v 0 5649 a Fo(9.5.)52 b(Inher)q(itance)3207 b(71)p eop end %%Page: 72 80 TeXDict begin 72 79 bop 0 83 a Fs(F)o(or)27 b(ne)n(w-style)g(classes,)j (the)e(method)e(resolution)g(order)g(changes)g(dynamically)g(to)i (support)e(cooperati)n(v)o(e)f(calls)j(to)g Fp(super\(\))p Fs(.)0 183 y(This)22 b(approach)d(is)k(kno)n(wn)d(in)h(some)h(other)e (multiple-inheritance)f(languages)h(as)i(call-ne)o(xt-method)c(and)j (is)i(more)d(po)n(werful)g(than)0 282 y(the)g(super)g(call)g(found)f (in)h(single-inheritance)e(languages.)0 429 y(W)m(ith)24 b(ne)n(w-style)g(classes,)i(dynamic)c(ordering)g(is)j(necessary)e (because)h(all)h(cases)f(of)g(multiple)g(inheritance)e(e)o(xhibit)h (one)h(or)f(more)0 529 y(diamond)f(relationships)g(\(where)h(one)g(at)i (least)f(one)f(of)h(the)f(parent)g(classes)i(can)e(be)h(accessed)g (through)d(multiple)i(paths)h(from)f(the)0 628 y(bottommost)29 b(class\).)56 b(F)o(or)30 b(e)o(xample,)i(all)f(ne)n(w-style)f(classes) h(inherit)f(from)f Fp(object)p Fs(,)k(so)d(an)o(y)g(case)h(of)f (multiple)g(inheritance)0 728 y(pro)o(vides)24 b(more)i(than)f(one)h (path)f(to)i(reach)e Fp(object)p Fs(.)42 b(T)-7 b(o)27 b(k)o(eep)e(the)h(base)g(classes)i(from)d(being)g(accessed)h(more)f (than)h(once,)h(the)0 828 y(dynamic)18 b(algorithm)g(linearizes)i(the)f (search)h(order)e(in)i(a)g(w)o(ay)g(that)g(preserv)o(es)e(the)i (left-to-right)e(ordering)f(speci\002ed)j(in)f(each)h(class,)0 927 y(that)j(calls)h(each)f(parent)f(only)g(once,)h(and)g(that)g(is)h (monotonic)d(\(meaning)g(that)i(a)h(class)g(can)f(be)g(subclassed)g (without)f(af)n(fecting)g(the)0 1027 y(precedence)i(order)g(of)i(its)h (parents\).)41 b(T)-7 b(ak)o(en)25 b(together)m(,)h(these)g(properties) e(mak)o(e)h(it)i(possible)f(to)g(design)f(reliable)g(and)h(e)o (xtensible)0 1127 y(classes)21 b(with)g(multiple)e(inheritance.)24 b(F)o(or)19 b(more)h(detail,)g(see)h Ft(http://www)l(.p)n(ython.org)o (/do)n(wnl)o(oa)o(d/r)o(ele)o(ases/2)o(.3/)o(mro/)-6 b Fs(.)0 1454 y Fl(9.6)121 b(Pr)r(iv)m(ate)33 b(V)-8 b(ar)r(iab)n(les)0 1687 y Fs(There)22 b(is)i(limited)e(support)g(for)g (class-pri)n(v)n(ate)f(identi\002ers.)33 b(An)o(y)22 b(identi\002er)g(of)g(the)h(form)f Fp(__spam)g Fs(\(at)h(least)g(tw)o (o)g(leading)f(under)n(-)0 1786 y(scores,)f(at)g(most)g(one)f(trailing) g(underscore\))f(is)i(te)o(xtually)f(replaced)g(with)h Fp(_classname__spam)p Fs(,)d(where)i Fp(classname)g Fs(is)i(the)0 1886 y(current)c(class)i(name)e(with)i(leading)e(underscore\(s\))e (stripped.)24 b(This)19 b(mangling)f(is)i(done)e(without)g(re)o(gard)f (to)i(the)h(syntactic)e(position)0 1986 y(of)28 b(the)g(identi\002er)m (,)h(so)g(it)g(can)f(be)g(used)g(to)g(de\002ne)g(class-pri)n(v)n(ate)f (instance)h(and)g(class)h(v)n(ariables,)g(methods,)g(v)n(ariables)f (stored)f(in)0 2085 y(globals,)h(and)f(e)n(v)o(en)g(v)n(ariables)f (stored)h(in)h(instances.)46 b(pri)n(v)n(ate)26 b(to)i(this)g(class)g (on)f(instances)h(of)f Fq(other)g Fs(classes.)48 b(T)m(runcation)25 b(may)0 2185 y(occur)19 b(when)h(the)h(mangled)d(name)i(w)o(ould)g(be)g (longer)f(than)h(255)g(characters.)k(Outside)c(classes,)i(or)e(when)g (the)g(class)h(name)f(consists)0 2285 y(of)g(only)f(underscores,)f(no)i (mangling)e(occurs.)0 2431 y(Name)25 b(mangling)e(is)j(intended)d(to)j (gi)n(v)o(e)e(classes)i(an)f(easy)g(w)o(ay)g(to)g(de\002ne)g(\223pri)n (v)n(ate\224)e(instance)i(v)n(ariables)f(and)g(methods,)h(without)0 2531 y(ha)n(ving)15 b(to)h(w)o(orry)e(about)h(instance)h(v)n(ariables)e (de\002ned)h(by)h(deri)n(v)o(ed)e(classes,)j(or)f(mucking)d(with)j (instance)g(v)n(ariables)f(by)g(code)g(outside)0 2631 y(the)20 b(class.)25 b(Note)20 b(that)g(the)f(mangling)f(rules)i(are)f (designed)g(mostly)g(to)h(a)n(v)n(oid)f(accidents;)h(it)g(still)h(is)f (possible)g(for)f(a)h(determined)e(soul)0 2730 y(to)k(access)g(or)g (modify)e(a)i(v)n(ariable)f(that)h(is)h(considered)d(pri)n(v)n(ate.)28 b(This)22 b(can)g(e)n(v)o(en)f(be)g(useful)h(in)g(special)g (circumstances,)e(such)i(as)g(in)0 2830 y(the)e(deb)n(ugger)m(,)d(and)i (that')-5 b(s)21 b(one)e(reason)g(why)g(this)h(loophole)e(is)j(not)f (closed.)k(\(Buglet:)h(deri)n(v)n(ation)18 b(of)h(a)i(class)f(with)g (the)g(same)g(name)0 2930 y(as)h(the)f(base)g(class)i(mak)o(es)e(use)g (of)g(pri)n(v)n(ate)f(v)n(ariables)g(of)h(the)h(base)f(class)h (possible.\))0 3076 y(Notice)e(that)g(code)g(passed)g(to)g Fp(exec)p Fs(,)g Fp(eval\(\))g Fs(or)g Fp(execfile\(\))f Fs(does)h(not)g(consider)f(the)h(classname)g(of)g(the)g(in)m(v)n(oking) e(class)j(to)0 3176 y(be)i(the)f(current)g(class;)j(this)e(is)h (similar)e(to)h(the)g(ef)n(fect)f(of)h(the)g Fp(global)f Fs(statement,)h(the)f(ef)n(fect)h(of)f(which)g(is)i(lik)o(e)n(wise)f (restricted)f(to)0 3276 y(code)e(that)g(is)h(byte-compiled)d(together) -5 b(.)23 b(The)c(same)h(restriction)e(applies)h(to)h Fp(getattr\(\))p Fs(,)e Fp(setattr\(\))g Fs(and)h Fp(delattr\(\))p Fs(,)f(as)0 3375 y(well)j(as)g(when)e(referencing)f Fp(__dict__)h Fs(directly)-5 b(.)0 3703 y Fl(9.7)121 b(Odds)34 b(and)g(Ends)0 3936 y Fs(Sometimes)20 b(it)i(is)f(useful)g(to)f(ha)n(v)o(e)h(a)g(data) f(type)h(similar)f(to)h(the)g(P)o(ascal)g(\223record\224)e(or)i(C)g (\223struct\224,)g(b)n(undling)e(together)g(a)i(fe)n(w)g(named)0 4035 y(data)f(items.)25 b(An)c(empty)e(class)i(de\002nition)e(will)i (do)f(nicely:)236 4273 y Fk(class)44 b(Employee:)416 4365 y(pass)236 4547 y(john)g(=)h(Employee\(\))e(#)i(Create)f(an)g (empty)g(employee)g(record)236 4730 y(#)h(Fill)f(the)g(fields)g(of)h (the)f(record)236 4821 y(john.name)g(=)g('John)g(Doe')236 4913 y(john.dept)g(=)g('computer)g(lab')236 5004 y(john.salary)f(=)i (1000)0 5290 y Fs(A)32 b(piece)e(of)h(Python)f(code)g(that)h(e)o (xpects)g(a)g(particular)f(abstract)h(data)g(type)f(can)h(often)f(be)h (passed)g(a)h(class)g(that)f(emulates)g(the)0 5390 y(methods)21 b(of)i(that)f(data)g(type)g(instead.)32 b(F)o(or)22 b(instance,)g(if)h (you)f(ha)n(v)o(e)f(a)i(function)e(that)i(formats)e(some)i(data)f(from) f(a)i(\002le)g(object,)f(you)p 0 5549 3901 4 v 0 5649 a Fo(72)3050 b(Chapter)24 b(9.)52 b(Classes)p eop end %%Page: 73 81 TeXDict begin 73 80 bop 0 83 a Fs(can)20 b(de\002ne)g(a)h(class)g(with) g(methods)f Fp(read\(\))f Fs(and)h Fp(readline\(\))f Fs(that)i(get)g(the)f(data)g(from)g(a)h(string)f(b)n(uf)n(fer)f (instead,)h(and)g(pass)h(it)0 183 y(as)g(an)f(ar)o(gument.)0 330 y(Instance)e(method)f(objects)h(ha)n(v)o(e)g(attrib)n(utes,)g(too:) 24 b Fp(m.im_self)18 b Fs(is)h(the)g(instance)f(object)g(with)g(the)h (method)e Fp(m)p Fs(,)i(and)f Fp(m.im_func)0 429 y Fs(is)j(the)f (function)f(object)g(corresponding)e(to)k(the)f(method.)0 757 y Fl(9.8)121 b(Exceptions)34 b(Are)f(Classes)g(T)-14 b(oo)0 989 y Fs(User)n(-de\002ned)27 b(e)o(xceptions)g(are)i (identi\002ed)f(by)g(classes)i(as)f(well.)51 b(Using)29 b(this)g(mechanism)e(it)i(is)h(possible)e(to)h(create)g(e)o(xtensible)0 1089 y(hierarchies)19 b(of)h(e)o(xceptions.)0 1236 y(There)f(are)h(tw)o (o)h(ne)n(w)f(v)n(alid)g(\(semantic\))f(forms)g(for)h(the)g(raise)h (statement:)236 1474 y Fk(raise)44 b(Class,)g(instance)236 1657 y(raise)g(instance)0 1943 y Fs(In)29 b(the)h(\002rst)g(form,)h Fp(instance)d Fs(must)i(be)f(an)g(instance)h(of)f Fp(Class)g Fs(or)g(of)g(a)h(class)h(deri)n(v)o(ed)c(from)i(it.)53 b(The)30 b(second)e(form)h(is)h(a)0 2043 y(shorthand)18 b(for:)236 2281 y Fk(raise)44 b(instance.__class__,)e(instance)0 2567 y Fs(A)24 b(class)f(in)h(an)f(e)o(xcept)f(clause)h(is)h (compatible)d(with)i(an)g(e)o(xception)e(if)j(it)f(is)h(the)f(same)g (class)h(or)f(a)h(base)f(class)h(thereof)d(\(b)n(ut)i(not)g(the)0 2667 y(other)i(w)o(ay)g(around)f(\227)i(an)f(e)o(xcept)g(clause)g (listing)h(a)g(deri)n(v)o(ed)d(class)k(is)f(not)f(compatible)f(with)i (a)g(base)f(class\).)41 b(F)o(or)25 b(e)o(xample,)h(the)0 2766 y(follo)n(wing)19 b(code)g(will)i(print)f(B,)g(C,)h(D)g(in)f(that) h(order:)236 3004 y Fk(class)44 b(B:)416 3096 y(pass)236 3187 y(class)g(C\(B\):)416 3278 y(pass)236 3370 y(class)g(D\(C\):)416 3461 y(pass)236 3644 y(for)h(c)f(in)h([B,)f(C,)h(D]:)416 3735 y(try:)595 3826 y(raise)f(c\(\))416 3918 y(except)f(D:)595 4009 y(print)h("D")416 4100 y(except)f(C:)595 4192 y(print)h("C")416 4283 y(except)f(B:)595 4374 y(print)h("B")0 4660 y Fs(Note)29 b(that)g(if)h(the)f(e)o(xcept)f(clauses)i(were)f(re)n(v)o(ersed)f (\(with)h(`)p Fp(except)48 b(B)p Fs(')29 b(\002rst\),)j(it)e(w)o(ould)e (ha)n(v)o(e)h(printed)f(B,)i(B,)g(B)g(\227)g(the)f(\002rst)0 4760 y(matching)19 b(e)o(xcept)g(clause)h(is)h(triggered.)0 4907 y(When)g(an)g(error)f(message)h(is)h(printed)e(for)g(an)h (unhandled)e(e)o(xception,)g(the)i(e)o(xception')-5 b(s)20 b(class)i(name)e(is)i(printed,)e(then)g(a)i(colon)e(and)0 5007 y(a)h(space,)f(and)f(\002nally)h(the)g(instance)g(con)m(v)o(erted) e(to)i(a)h(string)f(using)f(the)h(b)n(uilt-in)g(function)e Fp(str\(\))p Fs(.)p 0 5549 3901 4 v 0 5649 a Fo(9.8.)52 b(Exceptions)23 b(Are)h(Classes)e(T)-10 b(oo)2585 b(73)p eop end %%Page: 74 82 TeXDict begin 74 81 bop 0 85 a Fl(9.9)121 b(Iter)o(ators)0 318 y Fs(By)21 b(no)n(w)e(you)h(ha)n(v)o(e)f(probably)f(noticed)h(that) i(most)f(container)e(objects)i(can)g(be)g(looped)f(o)o(v)o(er)g(using)h (a)g Fp(for)h Fs(statement:)236 556 y Fk(for)45 b(element)e(in)i([1,)f (2,)h(3]:)416 647 y(print)f(element)236 739 y(for)h(element)e(in)i (\(1,)f(2,)h(3\):)416 830 y(print)f(element)236 921 y(for)h(key)f(in)g ({'one':1,)g('two':2}:)416 1013 y(print)g(key)236 1104 y(for)h(char)f(in)g("123":)416 1195 y(print)g(char)236 1287 y(for)h(line)f(in)g(open\("myfile.txt"\):)416 1378 y(print)g(line)0 1664 y Fs(This)25 b(style)h(of)f(access)h(is)g(clear)m (,)g(concise,)g(and)e(con)m(v)o(enient.)38 b(The)25 b(use)g(of)g (iterators)g(perv)n(ades)f(and)h(uni\002es)g(Python.)39 b(Behind)25 b(the)0 1764 y(scenes,)e(the)g Fp(for)g Fs(statement)g (calls)g Fp(iter\(\))f Fs(on)h(the)g(container)e(object.)32 b(The)23 b(function)e(returns)h(an)g(iterator)h(object)f(that)h (de\002nes)0 1864 y(the)30 b(method)f Fp(next\(\))g Fs(which)h (accesses)h(elements)e(in)i(the)f(container)e(one)i(at)g(a)h(time.)55 b(When)30 b(there)f(are)h(no)g(more)f(elements,)0 1963 y Fp(next\(\))20 b Fs(raises)h(a)g Fp(StopIteration)e Fs(e)o(xception)f(which)i(tells)i(the)e Fp(for)h Fs(loop)f(to)g (terminate.)25 b(This)c(e)o(xample)e(sho)n(ws)i(ho)n(w)f(it)h(all)0 2063 y(w)o(orks:)236 2301 y Fk(>>>)45 b(s)f(=)h('abc')236 2392 y(>>>)g(it)f(=)h(iter\(s\))236 2484 y(>>>)g(it)236 2575 y()236 2666 y(>>>)g(it.next\(\))236 2758 y('a')236 2849 y(>>>)g(it.next\(\))236 2940 y('b')236 3032 y(>>>)g(it.next\(\))236 3123 y('c')236 3214 y(>>>)g(it.next\(\))236 3397 y(Traceback)f(\(most)g(recent)f(call) i(last\):)326 3488 y(File)f("",)f(line)h(1,)h(in)f(?)416 3580 y(it.next\(\))236 3671 y(StopIteration)0 3957 y Fs(Ha)n(ving)28 b(seen)g(the)g(mechanics)f(behind)g(the)h(iterator)g (protocol,)g(it)h(is)g(easy)g(to)f(add)g(iterator)f(beha)n(vior)g(to)h (your)f(classes.)51 b(De\002ne)0 4057 y(a)27 b Fp(__iter__\(\))e Fs(method)h(which)g(returns)g(an)g(object)g(with)h(a)g Fp(next\(\))g Fs(method.)43 b(If)26 b(the)h(class)g(de\002nes)g Fp(next\(\))p Fs(,)g(then)g Fp(__-)0 4156 y(iter__\(\))19 b Fs(can)h(just)h(return)e Fp(self)p Fs(:)p 0 5549 3901 4 v 0 5649 a Fo(74)3050 b(Chapter)24 b(9.)52 b(Classes)p eop end %%Page: 75 83 TeXDict begin 75 82 bop 236 174 a Fk(class)44 b(Reverse:)416 266 y("Iterator)f(for)h(looping)g(over)g(a)h(sequence)e(backwards")416 357 y(def)h(__init__\(self,)e(data\):)595 448 y(self.data)h(=)i(data) 595 540 y(self.index)e(=)i(len\(data\))416 631 y(def)f (__iter__\(self\):)595 722 y(return)g(self)416 814 y(def)g (next\(self\):)595 905 y(if)g(self.index)g(==)g(0:)774 996 y(raise)g(StopIteration)595 1088 y(self.index)f(=)i(self.index)e(-) i(1)595 1179 y(return)f(self.data[self.index])236 1362 y(>>>)h(for)f(char)g(in)h(Reverse\('spam'\):)236 1453 y(...)224 b(print)44 b(char)236 1544 y(...)236 1636 y(m)236 1727 y(a)236 1818 y(p)236 1910 y(s)0 2358 y Fl(9.10)121 b(Gener)o(ators)0 2591 y Fs(Generators)24 b(are)i(a)f(simple)h(and)f (po)n(werful)e(tool)j(for)e(creating)h(iterators.)40 b(The)o(y)24 b(are)i(written)f(lik)o(e)h(re)o(gular)d(functions)h(b)n (ut)i(use)g(the)0 2691 y Fp(yield)f Fs(statement)h(whene)n(v)o(er)e (the)o(y)h(w)o(ant)h(to)g(return)f(data.)41 b(Each)26 b(time)g Fp(next\(\))f Fs(is)i(called,)f(the)g(generator)e(resumes)i (where)f(it)0 2790 y(left-of)n(f)c(\(it)j(remembers)d(all)i(the)g(data) g(v)n(alues)g(and)f(which)g(statement)h(w)o(as)h(last)f(e)o(x)o (ecuted\).)31 b(An)23 b(e)o(xample)e(sho)n(ws)i(that)g(generators)0 2890 y(can)d(be)g(tri)n(vially)g(easy)g(to)g(create:)236 3128 y Fk(def)45 b(reverse\(data\):)416 3219 y(for)f(index)g(in)g (range\(len\(data\)-1,)e(-1,)i(-1\):)595 3311 y(yield)g(data[index])236 3493 y(>>>)h(for)f(char)g(in)h(reverse\('golf'\):)236 3585 y(...)224 b(print)44 b(char)236 3676 y(...)236 3767 y(f)236 3859 y(l)236 3950 y(o)236 4041 y(g)0 4327 y Fs(An)o(ything)25 b(that)i(can)f(be)h(done)e(with)i(generators)e(can)i(also)g(be)g(done)e (with)i(class)h(based)e(iterators)h(as)g(described)f(in)g(the)h(pre)n (vious)0 4427 y(section.)c(What)17 b(mak)o(es)g(generators)e(so)i (compact)e(is)i(that)g(the)g Fp(__iter__\(\))e Fs(and)h Fp(next\(\))g Fs(methods)f(are)i(created)f(automatically)-5 b(.)0 4574 y(Another)23 b(k)o(e)o(y)g(feature)g(is)i(that)f(the)g (local)g(v)n(ariables)f(and)h(e)o(x)o(ecution)e(state)i(are)g (automatically)f(sa)n(v)o(ed)h(between)f(calls.)37 b(This)24 b(made)0 4674 y(the)h(function)f(easier)h(to)h(write)f(and)g(much)f (more)h(clear)g(than)g(an)g(approach)e(using)i(instance)g(v)n(ariables) g(lik)o(e)g Fp(self.index)f Fs(and)0 4773 y Fp(self.data)p Fs(.)0 4920 y(In)k(addition)f(to)h(automatic)g(method)e(creation)i(and) f(sa)n(ving)h(program)e(state,)31 b(when)d(generators)e(terminate,)k (the)o(y)d(automatically)0 5020 y(raise)j Fp(StopIteration)p Fs(.)50 b(In)29 b(combination,)g(these)g(features)g(mak)o(e)g(it)h (easy)f(to)g(create)g(iterators)g(with)h(no)e(more)h(ef)n(fort)f(than)0 5119 y(writing)20 b(a)g(re)o(gular)f(function.)p 0 5549 3901 4 v 0 5649 a Fo(9.10.)52 b(Gener)o(ators)3157 b(75)p eop end %%Page: 76 84 TeXDict begin 76 83 bop 0 88 a Fl(9.11)121 b(Gener)o(ator)35 b(Expressions)0 321 y Fs(Some)19 b(simple)g(generators)f(can)h(be)g (coded)f(succinctly)h(as)h(e)o(xpressions)e(using)h(a)h(syntax)e (similar)i(to)f(list)i(comprehensions)16 b(b)n(ut)j(with)0 420 y(parentheses)h(instead)i(of)f(brack)o(ets.)28 b(These)21 b(e)o(xpressions)g(are)g(designed)f(for)h(situations)g(where)g(the)h (generator)d(is)k(used)e(right)g(a)o(w)o(ay)0 520 y(by)c(an)h (enclosing)f(function.)22 b(Generator)16 b(e)o(xpressions)h(are)h(more) f(compact)f(b)n(ut)i(less)h(v)o(ersatile)e(than)h(full)f(generator)f (de\002nitions)h(and)0 620 y(tend)j(to)g(be)g(more)g(memory)e(friendly) h(than)g(equi)n(v)n(alent)g(list)i(comprehensions.)0 766 y(Examples:)236 913 y Fk(>>>)45 b(sum\(i)641 926 y(*)686 913 y(i)e(for)i(i)f(in)h(range\(10\)\))760 b(#)45 b(sum)f(of)h(squares)236 1005 y(285)236 1187 y(>>>)g(xvec)f(=)g([10,)h (20,)f(30])236 1279 y(>>>)h(yvec)f(=)g([7,)h(5,)f(3])236 1370 y(>>>)h(sum\(x)641 1383 y(*)686 1370 y(y)e(for)i(x,y)f(in)g (zip\(xvec,)g(yvec\)\))402 b(#)45 b(dot)f(product)236 1461 y(260)236 1644 y(>>>)h(from)f(math)g(import)g(pi,)g(sin)236 1735 y(>>>)h(sine_table)e(=)h(dict\(\(x,)g(sin\(x)1627 1748 y(*)1672 1735 y(pi/180\)\))e(for)j(x)f(in)h(range\(0,)e(91\)\))236 1918 y(>>>)i(unique_words)d(=)j(set\(word)88 b(for)45 b(line)f(in)g(page)89 b(for)45 b(word)f(in)g(line.split\(\)\))236 2101 y(>>>)h(valedictorian)d(=)j(max\(\(student.gpa,)d(student.name\))g (for)j(student)e(in)i(graduates\))236 2283 y(>>>)g(data)f(=)g('golf') 236 2375 y(>>>)h(list\(data[i])d(for)j(i)f(in)h (range\(len\(data\)-1,-1,-1\)\))236 2466 y(['f',)f('l',)g('o',)h('g'])p 0 5549 3901 4 v 0 5649 a Fo(76)3050 b(Chapter)24 b(9.)52 b(Classes)p eop end %%Page: 77 85 TeXDict begin 77 84 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3661 427 y Fn(TEN)p 0 515 V 795 978 a Fy(Br)s(ief)57 b(T)-25 b(our)57 b(of)h(the)g(Standard)e(Libr)n(ar)6 b(y)0 1506 y Fl(10.1)121 b(Oper)o(ating)35 b(System)e(Interf)l(ace)0 1739 y Fs(The)20 b Fp(os)g Fs(module)f(pro)o(vides)f(dozens)i(of)g (functions)e(for)i(interacting)f(with)h(the)g(operating)f(system:)236 1977 y Fk(>>>)45 b(import)e(os)236 2069 y(>>>)i(os.system\('time)d (0:02'\))236 2160 y(0)236 2251 y(>>>)j(os.getcwd\(\))267 b(#)44 b(Return)g(the)h(current)e(working)h(directory)236 2343 y('C:\\\\Python25')236 2434 y(>>>)h (os.chdir\('/server/accesslogs'\))0 2720 y Fs(Be)22 b(sure)f(to)g(use)h (the)f(`)p Fp(import)48 b(os)p Fs(')21 b(style)h(instead)f(of)f(`)p Fp(from)49 b(os)h(import)2442 2735 y(*)2492 2720 y Fs('.)28 b(This)22 b(will)f(k)o(eep)g Fp(os.open\(\))f Fs(from)g(shad-)0 2820 y(o)n(wing)f(the)h(b)n(uiltin)g Fp(open\(\))g Fs(function)e(which) i(operates)g(much)f(dif)n(ferently)-5 b(.)0 2967 y(The)20 b(b)n(uiltin)g Fp(dir\(\))g Fs(and)f Fp(help\(\))h Fs(functions)f(are)h (useful)f(as)i(interacti)n(v)o(e)e(aids)i(for)e(w)o(orking)g(with)h (lar)o(ge)f(modules)h(lik)o(e)g Fp(os)p Fs(:)236 3205 y Fk(>>>)45 b(import)e(os)236 3296 y(>>>)i(dir\(os\))236 3388 y()236 3479 y(>>>)h(help\(os\))236 3570 y()0 3856 y Fs(F)o(or)22 b(daily)g(\002le)h(and)f(directory)f(management)f (tasks,)j(the)g Fp(shutil)f Fs(module)f(pro)o(vides)f(a)j(higher)e(le)n (v)o(el)h(interf)o(ace)g(that)g(is)i(easier)e(to)0 3956 y(use:)236 4194 y Fk(>>>)45 b(import)e(shutil)236 4286 y(>>>)i(shutil.copyfile\('data.db',)40 b('archive.db'\))236 4377 y(>>>)45 b(shutil.move\('/build/executables',)39 b('installdir'\))0 4823 y Fl(10.2)121 b(File)33 b(Wildcards)0 5056 y Fs(The)20 b Fp(glob)g Fs(module)f(pro)o(vides)f(a)j(function)d (for)i(making)f(\002le)h(lists)i(from)d(directory)g(wildcard)g (searches:)p 0 5549 3901 4 v 3808 5649 a Fo(77)p eop end %%Page: 78 86 TeXDict begin 78 85 bop 236 174 a Fk(>>>)45 b(import)e(glob)236 266 y(>>>)i(glob.glob\(')911 279 y(*)956 266 y(.py'\))236 357 y(['primes.py',)e('random.py',)g('quote.py'])0 806 y Fl(10.3)121 b(Command)35 b(Line)f(Arguments)0 1038 y Fs(Common)d(utility)h(scripts)h(often)e(need)h(to)h(process)e (command)g(line)h(ar)o(guments.)59 b(These)32 b(ar)o(guments)f(are)h (stored)g(in)g(the)h Fp(sys)0 1138 y Fs(module')-5 b(s)23 b Fq(ar)m(gv)h Fs(attrib)n(ute)g(as)h(a)g(list.)39 b(F)o(or)24 b(instance)f(the)i(follo)n(wing)d(output)i(results)g(from)g(running)e (`)p Fp(python)48 b(demo.py)h(one)0 1238 y(two)g(three)p Fs(')20 b(at)g(the)h(command)d(line:)236 1476 y Fk(>>>)45 b(import)e(sys)236 1567 y(>>>)i(print)f(sys.argv)236 1658 y(['demo.py',)f('one',)h('two',)g('three'])0 1945 y Fs(The)19 b Fp(getopt)f Fs(module)f(processes)i Fq(sys.ar)m(gv)g Fs(using)f(the)h(con)m(v)o(entions)d(of)i(the)j(U)t Fm(N)t(I)t(X)h Fp(getopt\(\))c Fs(function.)23 b(More)18 b(po)n(werful)f(and)0 2044 y(\003e)o(xible)j(command)e(line)i(processing)f(is)i(pro)o(vided)d (by)h(the)i Fp(optparse)e Fs(module.)0 2372 y Fl(10.4)121 b(Error)33 b(Output)i(Redirection)g(and)g(Prog)o(r)o(am)e(T)-14 b(er)s(mination)0 2605 y Fs(The)23 b Fp(sys)g Fs(module)e(also)j(has)f (attrib)n(utes)g(for)f Fq(stdin)p Fs(,)h Fq(stdout)q Fs(,)h(and)e Fq(stderr)r Fs(.)34 b(The)23 b(latter)g(is)h(useful)f(for) f(emitting)g(w)o(arnings)g(and)h(error)0 2704 y(messages)d(to)h(mak)o (e)e(them)h(visible)g(e)n(v)o(en)g(when)f Fq(stdout)i Fs(has)g(been)e(redirected:)236 2942 y Fk(>>>)45 b (sys.stderr.write\('Warning,)40 b(log)k(file)g(not)h(found)f(starting)f (a)i(new)f(one\\n'\))236 3034 y(Warning,)g(log)g(file)g(not)h(found)f (starting)f(a)i(new)f(one)0 3320 y Fs(The)20 b(most)g(direct)g(w)o(ay)g (to)g(terminate)g(a)g(script)h(is)g(to)f(use)h(`)p Fp(sys.exit\(\))p Fs('.)0 3647 y Fl(10.5)121 b(Str)r(ing)33 b(P)-5 b(atter)s(n)35 b(Matching)0 3880 y Fs(The)19 b Fp(re)h Fs(module)f(pro)o(vides)f(re)o (gular)g(e)o(xpression)g(tools)i(for)f(adv)n(anced)f(string)i (processing.)j(F)o(or)d(comple)o(x)e(matching)g(and)h(manipu-)0 3980 y(lation,)h(re)o(gular)e(e)o(xpressions)h(of)n(fer)g(succinct,)h (optimized)f(solutions:)236 4218 y Fk(>>>)45 b(import)e(re)236 4309 y(>>>)i(re.findall\(r'\\bf[a-z])1361 4322 y(*)1406 4309 y(',)40 b('which)k(foot)g(or)h(hand)f(fell)g(fastest'\))236 4401 y(['foot',)g('fell',)f('fastest'])236 4492 y(>>>)i (re.sub\(r'\(\\b[a-z]+\))c(\\1',)j(r'\\1',)g('cat)g(in)h(the)f(the)g (hat'\))236 4583 y('cat)g(in)h(the)f(hat')0 4869 y Fs(When)20 b(only)f(simple)i(capabilities)e(are)i(needed,)d(string)i(methods)f (are)h(preferred)e(because)i(the)o(y)f(are)h(easier)h(to)f(read)g(and)f (deb)n(ug:)236 5108 y Fk(>>>)45 b('tea)f(for)g(too'.replace\('too',)e ('two'\))236 5199 y('tea)i(for)h(two')p 0 5549 3901 4 v 0 5649 a Fo(78)2075 b(Chapter)23 b(10.)52 b(Br)q(ief)24 b(T)-10 b(our)23 b(of)g(the)h(Standard)g(Libr)o(ar)r(y)p eop end %%Page: 79 87 TeXDict begin 79 86 bop 0 85 a Fl(10.6)121 b(Mathematics)0 318 y Fs(The)20 b Fp(math)g Fs(module)f(gi)n(v)o(es)g(access)i(to)g (the)f(underlying)d(C)k(library)e(functions)g(for)h(\003oating)f(point) h(math:)236 556 y Fk(>>>)45 b(import)e(math)236 647 y(>>>)i (math.cos\(math.pi)d(/)i(4.0\))236 739 y(0.70710678118654757)236 830 y(>>>)h(math.log\(1024,)d(2\))236 921 y(10.0)0 1208 y Fs(The)20 b Fp(random)f Fs(module)g(pro)o(vides)g(tools)h(for)g (making)e(random)h(selections:)236 1446 y Fk(>>>)45 b(import)e(random) 236 1537 y(>>>)i(random.choice\(['apple',)c('pear',)i('banana']\))236 1628 y('apple')236 1720 y(>>>)i(random.sample\(xrange\(100\),)40 b(10\))134 b(#)45 b(sampling)e(without)h(replacement)236 1811 y([30,)g(83,)h(16,)f(4,)h(8,)f(81,)g(41,)h(50,)f(18,)g(33])236 1902 y(>>>)h(random.random\(\))176 b(#)45 b(random)f(float)236 1994 y(0.17970987693706186)236 2085 y(>>>)h(random.randrange\(6\))176 b(#)44 b(random)g(integer)g(chosen)g(from)g(range\(6\))236 2176 y(4)0 2622 y Fl(10.7)121 b(Inter)s(net)36 b(Access)0 2855 y Fs(There)23 b(are)g(a)h(number)d(of)i(modules)g(for)f(accessing) h(the)h(internet)e(and)h(processing)f(internet)h(protocols.)33 b(T)-7 b(w)o(o)23 b(of)h(the)f(simplest)h(are)0 2955 y Fp(urllib2)19 b Fs(for)h(retrie)n(ving)e(data)j(from)e(urls)h(and)g Fp(smtplib)f Fs(for)h(sending)f(mail:)236 3193 y Fk(>>>)45 b(import)e(urllib2)236 3284 y(>>>)i(for)f(line)g(in)h (urllib2.urlopen\('http://tycho.usno.n)o(avy.mi)o(l/cgi-)o(bin/ti)o (mer.pl)o('\):)236 3376 y(...)224 b(if)44 b('EST')g(in)h(line)f(or)h ('EDT')f(in)g(line:)89 b(#)44 b(look)h(for)f(Eastern)g(Time)236 3467 y(...)403 b(print)44 b(line)236 3650 y(
Nov.)g(25,)g(09:43:32)g (PM)g(EST)236 3832 y(>>>)h(import)e(smtplib)236 3923 y(>>>)i(server)e(=)i(smtplib.SMTP\('localhost'\))236 4015 y(>>>)g(server.sendmail\('soothsayer@example.)o(org',)38 b('jcaesar@example.org',)236 4106 y("""To:)44 b(jcaesar@example.org)236 4197 y(From:)g(soothsayer@example.org)236 4380 y(Beware)g(the)g(Ides)h (of)f(March.)236 4471 y("""\))236 4563 y(>>>)h(server.quit\(\))0 5009 y Fl(10.8)121 b(Dates)35 b(and)f(Times)0 5241 y Fs(The)22 b Fp(datetime)g Fs(module)g(supplies)g(classes)i(for)e (manipulating)f(dates)i(and)f(times)h(in)g(both)f(simple)h(and)f (comple)o(x)f(w)o(ays.)33 b(While)0 5341 y(date)26 b(and)g(time)h (arithmetic)e(is)j(supported,)e(the)g(focus)g(of)g(the)h (implementation)d(is)j(on)f(ef)n(\002cient)g(member)f(e)o(xtraction)g (for)h(output)p 0 5549 3901 4 v 0 5649 a Fo(10.6.)52 b(Mathematics)3102 b(79)p eop end %%Page: 80 88 TeXDict begin 80 87 bop 0 83 a Fs(formatting)18 b(and)i(manipulation.)j (The)c(module)g(also)i(supports)e(objects)h(that)g(are)g(timezone)f(a)o (w)o(are.)236 321 y Fk(#)45 b(dates)f(are)g(easily)g(constructed)f(and) h(formatted)236 413 y(>>>)h(from)f(datetime)f(import)h(date)236 504 y(>>>)h(now)f(=)h(date.today\(\))236 595 y(>>>)g(now)236 687 y(datetime.date\(2003,)d(12,)i(2\))236 778 y(>>>)h (now.strftime\("\045m-\045d-\045y.)c(\045d)j(\045b)h(\045Y)f(is)g(a)h (\045A)g(on)f(the)g(\045d)h(day)f(of)h(\045B."\))236 869 y('12-02-03.)e(02)i(Dec)f(2003)g(is)h(a)g(Tuesday)e(on)i(the)f(02)g (day)h(of)f(December.')236 1052 y(#)h(dates)f(support)g(calendar)f (arithmetic)236 1143 y(>>>)i(birthday)e(=)i(date\(1964,)e(7,)h(31\))236 1234 y(>>>)h(age)f(=)h(now)f(-)h(birthday)236 1326 y(>>>)g(age.days)236 1417 y(14368)0 1866 y Fl(10.9)121 b(Data)35 b(Compression)0 2098 y Fs(Common)23 b(data)g(archi)n(ving)g(and)g(compression)f (formats)h(are)h(directly)f(supported)f(by)i(modules)f(including:)31 b Fp(zlib)p Fs(,)24 b Fp(gzip)p Fs(,)h Fp(bz2)p Fs(,)0 2198 y Fp(zipfile)p Fs(,)19 b(and)h Fp(tarfile)p Fs(.)236 2345 y Fk(>>>)45 b(import)e(zlib)236 2436 y(>>>)i(s)f(=)h('witch)f (which)g(has)g(which)g(witches)g(wrist)g(watch')236 2528 y(>>>)h(len\(s\))236 2619 y(41)236 2710 y(>>>)g(t)f(=)h (zlib.compress\(s\))236 2802 y(>>>)g(len\(t\))236 2893 y(37)236 2984 y(>>>)g(zlib.decompress\(t\))236 3076 y('witch)f(which)g (has)g(which)g(witches)g(wrist)g(watch')236 3167 y(>>>)h (zlib.crc32\(s\))236 3258 y(226805979)0 3707 y Fl(10.10)122 b(P)-6 b(erf)l(or)s(mance)35 b(Measurement)0 3940 y Fs(Some)24 b(Python)f(users)i(de)n(v)o(elop)d(a)j(deep)f(interest)g(in)h(kno)n (wing)e(the)h(relati)n(v)o(e)g(performance)d(of)j(dif)n(ferent)f (approaches)f(to)j(the)f(same)0 4039 y(problem.)f(Python)c(pro)o(vides) g(a)h(measurement)f(tool)h(that)g(answers)g(those)g(questions)g (immediately)-5 b(.)0 4186 y(F)o(or)20 b(e)o(xample,)g(it)h(may)f(be)h (tempting)e(to)i(use)g(the)g(tuple)f(packing)f(and)i(unpacking)d (feature)i(instead)g(of)h(the)f(traditional)g(approach)e(to)0 4286 y(sw)o(apping)h(ar)o(guments.)k(The)d Fp(timeit)f Fs(module)g(quickly)g(demonstrates)g(a)i(modest)e(performance)e(adv)n (antage:)236 4524 y Fk(>>>)45 b(from)f(timeit)g(import)f(Timer)236 4615 y(>>>)i(Timer\('t=a;)e(a=b;)h(b=t',)g('a=1;)g(b=2'\).timeit\(\)) 236 4707 y(0.57535828626024577)236 4798 y(>>>)h(Timer\('a,b)e(=)h (b,a',)g('a=1;)g(b=2'\).timeit\(\))236 4889 y(0.54962537085770791)0 5175 y Fs(In)24 b(contrast)f(to)h Fp(timeit)p Fs(')-5 b(s)24 b(\002ne)g(le)n(v)o(el)f(of)h(granularity)-5 b(,)22 b(the)i Fp(profile)f Fs(and)g Fp(pstats)h Fs(modules)f(pro)o(vide)e (tools)j(for)g(identifying)0 5275 y(time)c(critical)h(sections)f(in)g (lar)o(ger)f(blocks)h(of)g(code.)p 0 5549 3901 4 v 0 5649 a Fo(80)2075 b(Chapter)23 b(10.)52 b(Br)q(ief)24 b(T)-10 b(our)23 b(of)g(the)h(Standard)g(Libr)o(ar)r(y)p eop end %%Page: 81 89 TeXDict begin 81 88 bop 0 88 a Fl(10.11)122 b(Quality)33 b(Control)0 321 y Fs(One)18 b(approach)e(for)h(de)n(v)o(eloping)e(high) i(quality)g(softw)o(are)h(is)h(to)f(write)g(tests)h(for)f(each)f (function)f(as)j(it)g(is)g(de)n(v)o(eloped)c(and)i(to)i(run)e(those)0 420 y(tests)k(frequently)d(during)h(the)h(de)n(v)o(elopment)d(process.) 0 567 y(The)g Fp(doctest)g Fs(module)f(pro)o(vides)f(a)j(tool)f(for)g (scanning)f(a)i(module)e(and)h(v)n(alidating)f(tests)j(embedded)c(in)i (a)h(program')-5 b(s)16 b(docstrings.)0 667 y(T)-6 b(est)32 b(construction)e(is)i(as)h(simple)e(as)h(cutting-and-pasting)c(a)k (typical)f(call)h(along)f(with)h(its)g(results)g(into)f(the)h (docstring.)57 b(This)0 766 y(impro)o(v)o(es)18 b(the)j(documentation)c (by)j(pro)o(viding)e(the)i(user)h(with)f(an)h(e)o(xample)e(and)g(it)i (allo)n(ws)g(the)g(doctest)f(module)f(to)h(mak)o(e)g(sure)h(the)0 866 y(code)f(remains)f(true)h(to)g(the)h(documentation:)236 1104 y Fk(def)45 b(average\(values\):)416 1196 y("""Computes)e(the)h (arithmetic)f(mean)h(of)h(a)f(list)g(of)h(numbers.)416 1378 y(>>>)f(print)g(average\([20,)f(30,)h(70]\))416 1470 y(40.0)416 1561 y(""")416 1652 y(return)f(sum\(values,)g(0.0\))i (/)f(len\(values\))236 1835 y(import)g(doctest)236 1926 y(doctest.testmod\(\))132 b(#)45 b(automatically)d(validate)i(the)g (embedded)f(tests)0 2212 y Fs(The)21 b Fp(unittest)f Fs(module)g(is)i(not)f(as)h(ef)n(fortless)f(as)h(the)g Fp(doctest)e Fs(module,)g(b)n(ut)h(it)h(allo)n(ws)g(a)g(more)e (comprehensi)n(v)o(e)e(set)k(of)f(tests)0 2312 y(to)f(be)g(maintained)f (in)i(a)f(separate)g(\002le:)236 2550 y Fk(import)44 b(unittest)236 2733 y(class)g (TestStatisticalFunctions\(unittest.TestCa)o(se\):)416 2916 y(def)g(test_average\(self\):)595 3007 y (self.assertEqual\(average\([20,)c(30,)k(70]\),)g(40.0\))595 3098 y(self.assertEqual\(round\(average\([1,)39 b(5,)44 b(7]\),)h(1\),)f(4.3\))595 3189 y (self.assertRaises\(ZeroDivisionError,)39 b(average,)k([]\))595 3281 y(self.assertRaises\(TypeError,)d(average,)j(20,)i(30,)f(70\))236 3463 y(unittest.main\(\))f(#)h(Calling)g(from)g(the)g(command)g(line)g (invokes)g(all)g(tests)0 3909 y Fl(10.12)122 b(Batter)r(ies)33 b(Included)0 4142 y Fs(Python)19 b(has)i(a)g(\223batteries)f (included\224)f(philosophy)-5 b(.)22 b(This)f(is)h(best)e(seen)h (through)d(the)j(sophisticated)e(and)h(rob)n(ust)g(capabilities)g(of)h (its)0 4242 y(lar)o(ger)e(packages.)24 b(F)o(or)19 b(e)o(xample:)137 4471 y(\225)42 b(The)22 b Fp(xmlrpclib)f Fs(and)h Fp (SimpleXMLRPCServer)e Fs(modules)i(mak)o(e)g(implementing)e(remote)i (procedure)e(calls)j(into)g(an)208 4570 y(almost)d(tri)n(vial)g(task.) 25 b(Despite)20 b(the)h(modules)e(names,)g(no)h(direct)g(kno)n(wledge)e (or)i(handling)e(of)i(XML)g(is)h(needed.)137 4736 y(\225)42 b(The)26 b Fp(email)h Fs(package)f(is)i(a)f(library)f(for)g(managing)f (email)j(messages,)g(including)d(MIME)i(and)g(other)f(RFC)i(2822-based) 208 4836 y(message)j(documents.)56 b(Unlik)o(e)31 b Fp(smtplib)g Fs(and)f Fp(poplib)h Fs(which)g(actually)f(send)h(and)g(recei)n(v)o(e)f (messages,)k(the)e(email)208 4935 y(package)24 b(has)j(a)f(complete)f (toolset)h(for)g(b)n(uilding)f(or)h(decoding)e(comple)o(x)g(message)i (structures)g(\(including)e(attachments\))208 5035 y(and)19 b(for)h(implementing)e(internet)h(encoding)f(and)i(header)f(protocols.) 137 5201 y(\225)42 b(The)17 b Fp(xml.dom)f Fs(and)h Fp(xml.sax)g Fs(packages)f(pro)o(vide)g(rob)n(ust)g(support)h(for)f(parsing)h(this)h (popular)d(data)j(interchange)d(format.)208 5300 y(Lik)o(e)n(wise,)33 b(the)d Fp(csv)h Fs(module)f(supports)f(direct)i(reads)f(and)g(writes)h (in)g(a)g(common)e(database)h(format.)56 b(T)-7 b(ogether)m(,)31 b(these)208 5400 y(modules)19 b(and)g(packages)g(greatly)h(simplify)f (data)h(interchange)e(between)i(p)o(ython)e(applications)h(and)h(other) f(tools.)p 0 5549 3901 4 v 0 5649 a Fo(10.11.)52 b(Quality)24 b(Control)2977 b(81)p eop end %%Page: 82 90 TeXDict begin 82 89 bop 137 83 a Fs(\225)42 b(Internationalization)27 b(is)32 b(supported)d(by)h(a)h(number)e(of)h(modules)g(including)f Fp(gettext)p Fs(,)k Fp(locale)p Fs(,)f(and)e(the)h Fp(codecs)208 183 y Fs(package.)p 0 5549 3901 4 v 0 5649 a Fo(82)2075 b(Chapter)23 b(10.)52 b(Br)q(ief)24 b(T)-10 b(our)23 b(of)g(the)h(Standard)g(Libr)o(ar)r(y)p eop end %%Page: 83 91 TeXDict begin 83 90 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3422 427 y Fn(ELEVEN)p 0 515 V 71 978 a Fy(Br)s(ief)58 b(T)-25 b(our)57 b(of)h(the)f(Standard)g(Libr)n(ar)6 b(y)57 b(\226)g(P)-8 b(ar)8 b(t)57 b(II)0 1465 y Fs(This)20 b(second)g(tour)f(co)o(v)o(ers)g (more)h(adv)n(anced)e(modules)h(that)h(support)f(professional)g (programming)e(needs.)24 b(These)c(modules)f(rarely)0 1565 y(occur)g(in)h(small)h(scripts.)0 1892 y Fl(11.1)121 b(Output)35 b(F)l(or)s(matting)0 2125 y Fs(The)d Fp(repr)g Fs(module)e(pro)o(vides)g(a)j(v)o(ersion)e(of)g Fp(repr\(\))h Fs(customized)f(for)g(abbre)n(viated)f(displays)h(of)h(lar)o(ge)f(or)h (deeply)f(nested)0 2225 y(containers:)416 2463 y Fk(>>>)44 b(import)g(repr)416 2554 y(>>>)g (repr.repr\(set\('supercalifragilisticexp)o(ialido)o(cious')o(\)\))416 2645 y("set\(['a',)f('c',)h('d',)g('e',)g('f',)g('g',)g(...]\)")0 2932 y Fs(The)24 b Fp(pprint)f Fs(module)g(of)n(fers)g(more)g (sophisticated)g(control)g(o)o(v)o(er)f(printing)h(both)g(b)n(uilt-in)g (and)h(user)f(de\002ned)g(objects)h(in)g(a)h(w)o(ay)0 3031 y(that)g(is)h(readable)e(by)h(the)g(interpreter)-5 b(.)38 b(When)25 b(the)g(result)g(is)h(longer)e(than)h(one)f(line,)j (the)e(\223pretty)f(printer\224)g(adds)h(line)g(breaks)f(and)0 3131 y(indentation)18 b(to)j(more)e(clearly)h(re)n(v)o(eal)f(data)h (structure:)416 3369 y Fk(>>>)44 b(import)g(pprint)416 3460 y(>>>)g(t)h(=)f([[[['black',)f('cyan'],)h('white',)f(['green',)g ('red']],)h([['magenta',)416 3552 y(...)223 b('yellow'],)43 b('blue']]])416 3643 y(...)416 3734 y(>>>)h(pprint.pprint\(t,)e (width=30\))416 3826 y([[[['black',)g('cyan'],)550 3917 y('white',)550 4008 y(['green',)h('red']],)505 4100 y([['magenta',)g ('yellow'],)550 4191 y('blue']]])0 4477 y Fs(The)20 b Fp(textwrap)f Fs(module)g(formats)g(paragraphs)f(of)i(te)o(xt)g(to)h (\002t)g(a)f(gi)n(v)o(en)f(screen)h(width:)p 0 5549 3901 4 v 3808 5649 a Fo(83)p eop end %%Page: 84 92 TeXDict begin 84 91 bop 416 174 a Fk(>>>)44 b(import)g(textwrap)416 266 y(>>>)g(doc)g(=)h("""The)f(wrap\(\))g(method)f(is)i(just)f(like)g (fill\(\))g(except)g(that)g(it)g(returns)416 357 y(...)g(a)h(list)f(of) g(strings)g(instead)g(of)g(one)g(big)h(string)e(with)i(newlines)e(to)i (separate)416 448 y(...)f(the)g(wrapped)g(lines.""")416 540 y(...)416 631 y(>>>)g(print)g(textwrap.fill\(doc,)e(width=40\))416 722 y(The)i(wrap\(\))g(method)g(is)g(just)g(like)g(fill\(\))416 814 y(except)f(that)i(it)f(returns)g(a)g(list)h(of)f(strings)416 905 y(instead)f(of)i(one)f(big)g(string)g(with)g(newlines)416 996 y(to)g(separate)g(the)g(wrapped)g(lines.)0 1282 y Fs(The)24 b Fp(locale)f Fs(module)g(accesses)h(a)h(database)e(of)h (culture)f(speci\002c)h(data)g(formats.)35 b(The)24 b(grouping)d (attrib)n(ute)j(of)g(locale')-5 b(s)24 b(format)0 1382 y(function)18 b(pro)o(vides)h(a)i(direct)e(w)o(ay)i(of)e(formatting)g (numbers)f(with)j(group)d(separators:)416 1620 y Fk(>>>)44 b(import)g(locale)416 1712 y(>>>)g(locale.setlocale\(locale.LC_ALL,)39 b('English_United)k(States.1252'\))416 1803 y('English_United)f (States.1252')416 1894 y(>>>)i(conv)g(=)h(locale.localeconv\(\))445 b(#)44 b(get)h(a)f(mapping)g(of)h(conventions)416 1986 y(>>>)f(x)h(=)f(1234567.8)416 2077 y(>>>)g(locale.format\("\045d",)e (x,)i(grouping=True\))416 2168 y('1,234,567')416 2260 y(>>>)g(locale.format\("\045s\045.)1450 2273 y(*)1495 2260 y(f",)d(\(conv['currency_symbol'],)416 2351 y(...)313 b(conv['frac_digits'],)41 b(x\),)k(grouping=True\))416 2442 y('$1,234,567.80')0 2888 y Fl(11.2)121 b(T)-14 b(emplating)0 3121 y Fs(The)20 b Fp(string)f Fs(module)g(includes)g(a)h(v)o(ersatile) g Fp(Template)f Fs(class)i(with)f(a)g(simpli\002ed)g(syntax)f(suitable) h(for)f(editing)h(by)f(end-users.)0 3220 y(This)h(allo)n(ws)h(users)f (to)h(customize)e(their)h(applications)f(without)g(ha)n(ving)g(to)i (alter)f(the)g(application.)0 3367 y(The)29 b(format)g(uses)h (placeholder)e(names)h(formed)f(by)h(`)p Fp($)p Fs(')h(with)g(v)n(alid) f(Python)f(identi\002ers)i(\(alphanumeric)c(characters)j(and)g(un-)0 3467 y(derscores\).)41 b(Surrounding)22 b(the)k(placeholder)e(with)i (braces)f(allo)n(ws)h(it)h(to)f(be)f(follo)n(wed)g(by)g(more)g (alphanumeric)f(letters)i(with)g(no)0 3567 y(interv)o(ening)18 b(spaces.)25 b(Writing)20 b(`)p Fp($$)p Fs(')g(creates)g(a)g(single)g (escaped)g(`)p Fp($)p Fs(':)236 3805 y Fk(>>>)45 b(from)f(string)g (import)f(Template)236 3896 y(>>>)i(t)f(=)h(Template\('${village}folk)c (send)j($$10)g(to)g($cause.'\))236 3987 y(>>>)h (t.substitute\(village='Nottingham',)39 b(cause='the)k(ditch)h(fund'\)) 236 4079 y('Nottinghamfolk)f(send)h($10)g(to)h(the)f(ditch)g(fund.')0 4365 y Fs(The)30 b Fp(substitute)e Fs(method)h(raises)i(a)f Fp(KeyError)g Fs(when)f(a)h(placeholder)e(is)j(not)f(supplied)f(in)h(a) h(dictionary)d(or)i(a)g(k)o(e)o(yw)o(ord)0 4465 y(ar)o(gument.)43 b(F)o(or)26 b(mail-mer)o(ge)f(style)i(applications,)g(user)g(supplied)e (data)i(may)g(be)f(incomplete)g(and)g(the)h Fp(safe_substitute)0 4564 y Fs(method)19 b(may)h(be)g(more)f(appropriate)f(\227)j(it)g(will) f(lea)n(v)o(e)h(placeholders)d(unchanged)f(if)k(data)f(is)h(missing:)p 0 5549 3901 4 v 0 5649 a Fo(84)1784 b(Chapter)23 b(11.)52 b(Br)q(ief)24 b(T)-10 b(our)23 b(of)g(the)h(Standard)g(Libr)o(ar)r(y)f (\226)h(P)m(ar)s(t)f(II)p eop end %%Page: 85 93 TeXDict begin 85 92 bop 236 174 a Fk(>>>)45 b(t)f(=)h (Template\('Return)d(the)i($item)g(to)h($owner.'\))236 266 y(>>>)g(d)f(=)h(dict\(item='unladen)d(swallow'\))236 357 y(>>>)j(t.substitute\(d\))236 448 y(Traceback)f(\(most)g(recent)f (call)i(last\):)326 540 y(.)g(.)f(.)236 631 y(KeyError:)g('owner')236 722 y(>>>)h(t.safe_substitute\(d\))236 814 y('Return)f(the)g(unladen)g (swallow)g(to)g($owner.')0 1100 y Fs(T)-6 b(emplate)23 b(subclasses)h(can)f(specify)g(a)h(custom)g(delimiter)-5 b(.)35 b(F)o(or)23 b(e)o(xample,)g(a)h(batch)f(renaming)f(utility)h (for)g(a)h(photo)f(bro)n(wser)f(may)0 1199 y(elect)e(to)h(use)f (percent)f(signs)i(for)e(placeholders)g(such)h(as)h(the)f(current)f (date,)g(image)h(sequence)f(number)m(,)f(or)i(\002le)h(format:)236 1438 y Fk(>>>)45 b(import)e(time,)h(os.path)236 1529 y(>>>)h(photofiles)e(=)h(['img_1074.jpg',)f('img_1076.jpg',)f ('img_1077.jpg'])236 1620 y(>>>)j(class)f(BatchRename\(Template\):)236 1712 y(...)224 b(delimiter)43 b(=)i('\045')236 1803 y(>>>)g(fmt)f(=)h (raw_input\('Enter)d(rename)i(style)f(\(\045d-date)h(\045n-seqnum)f (\045f-format\):)88 b('\))236 1894 y(Enter)44 b(rename)g(style)g (\(\045d-date)g(\045n-seqnum)f(\045f-format\):)88 b(Ashley_\045n\045f) 236 2077 y(>>>)45 b(t)f(=)h(BatchRename\(fmt\))236 2168 y(>>>)g(date)f(=)g(time.strftime\('\045d\045b\045y'\))236 2260 y(>>>)h(for)f(i,)g(filename)g(in)g(enumerate\(photofiles\):)236 2351 y(...)224 b(base,)44 b(ext)g(=)h(os.path.splitext\(filename\))236 2442 y(...)224 b(newname)44 b(=)g(t.substitute\(d=date,)e(n=i,)i (f=ext\))236 2534 y(...)224 b(print)44 b('\045s)g(-->)h(\045s')f(\045)h (\(filename,)e(newname\))236 2716 y(img_1074.jpg)g(-->)h(Ashley_0.jpg) 236 2807 y(img_1076.jpg)f(-->)h(Ashley_1.jpg)236 2899 y(img_1077.jpg)f(-->)h(Ashley_2.jpg)0 3185 y Fs(Another)16 b(application)g(for)g(templating)g(is)i(separating)e(program)g(logic)g (from)h(the)g(details)h(of)f(multiple)f(output)g(formats.)23 b(This)18 b(mak)o(es)0 3285 y(it)j(possible)f(to)g(substitute)g(custom) g(templates)g(for)f(XML)i(\002les,)f(plain)g(te)o(xt)g(reports,)f(and)h (HTML)g(web)g(reports.)0 3612 y Fl(11.3)121 b(W)l(or)r(king)34 b(with)g(Binar)t(y)e(Data)j(Record)g(La)l(y)n(outs)0 3845 y Fs(The)27 b Fp(struct)g Fs(module)g(pro)o(vides)e Fp(pack\(\))j Fs(and)f Fp(unpack\(\))f Fs(functions)h(for)g(w)o(orking) f(with)h(v)n(ariable)g(length)g(binary)f(record)0 3944 y(formats.)37 b(The)24 b(follo)n(wing)f(e)o(xample)g(sho)n(ws)h(ho)n(w) g(to)h(loop)e(through)f(header)i(information)e(in)i(a)h(ZIP)g(\002le)g (\(with)f(pack)g(codes)g Fp("H")0 4044 y Fs(and)c Fp("L")g Fs(representing)e(tw)o(o)i(and)g(four)f(byte)h(unsigned)e(numbers)h (respecti)n(v)o(ely\):)p 0 5549 3901 4 v 0 5649 a Fo(11.3.)52 b(W)n(or)q(king)24 b(with)f(Binar)r(y)h(Data)g(Record)e(La)n(y)n(outs) 2038 b(85)p eop end %%Page: 86 94 TeXDict begin 86 93 bop 416 174 a Fk(import)43 b(struct)416 357 y(data)h(=)g(open\('myfile.zip',)e('rb'\).read\(\))416 448 y(start)i(=)g(0)416 540 y(for)g(i)h(in)f(range\(3\):)985 b(#)44 b(show)h(the)f(first)g(3)h(file)f(headers)595 631 y(start)g(+=)g(14)595 722 y(fields)g(=)g(struct.unpack\('LLLHH',)d (data[start:start+16]\))595 814 y(crc32,)j(comp_size,)f(uncomp_size,)g (filenamesize,)f(extra_size)h(=)i(fields)595 996 y(start)f(+=)g(16)595 1088 y(filename)f(=)i(data[start:start+filenamesize])595 1179 y(start)f(+=)g(filenamesize)595 1270 y(extra)g(=)h (data[start:start+extra_size])595 1362 y(print)f(filename,)f (hex\(crc32\),)g(comp_size,)g(uncomp_size)595 1544 y(start)h(+=)g (extra_size)g(+)g(comp_size)223 b(#)44 b(skip)h(to)f(the)g(next)h (header)0 1990 y Fl(11.4)121 b(Multi-threading)0 2223 y Fs(Threading)19 b(is)k(a)f(technique)e(for)h(decoupling)f(tasks)i (which)f(are)h(not)f(sequentially)f(dependent.)28 b(Threads)20 b(can)i(be)f(used)h(to)g(impro)o(v)o(e)0 2322 y(the)d(responsi)n(v)o (eness)f(of)h(applications)f(that)h(accept)g(user)g(input)g(while)g (other)g(tasks)h(run)e(in)h(the)h(background.)h(A)f(related)e(use)i (case)g(is)0 2422 y(running)e(I/O)i(in)h(parallel)f(with)g (computations)e(in)i(another)f(thread.)0 2569 y(The)31 b(follo)n(wing)f(code)g(sho)n(ws)i(ho)n(w)e(the)i(high)e(le)n(v)o(el)h Fp(threading)f Fs(module)g(can)h(run)g(tasks)h(in)f(background)d(while) k(the)f(main)0 2669 y(program)18 b(continues)h(to)h(run:)416 2907 y Fk(import)43 b(threading,)h(zipfile)416 3089 y(class)g (AsyncZip\(threading.Thread\):)595 3181 y(def)g(__init__\(self,)f (infile,)g(outfile\):)774 3272 y(threading.Thread.__init__\(self\))774 3363 y(self.infile)g(=)i(infile)774 3455 y(self.outfile)e(=)i(outfile) 595 3546 y(def)f(run\(self\):)774 3637 y(f)h(=)g (zipfile.ZipFile\(self.outfile,)39 b('w',)45 b(zipfile.ZIP_DEFLATED\)) 774 3729 y(f.write\(self.infile\))774 3820 y(f.close\(\))774 3911 y(print)f('Finished)g(background)f(zip)h(of:)g(',)h(self.infile) 416 4094 y(background)e(=)h(AsyncZip\('mydata.txt',)d ('myarchive.zip'\))416 4185 y(background.start\(\))416 4277 y(print)j('The)g(main)g(program)g(continues)f(to)h(run)h(in)f (foreground.')416 4459 y(background.join\(\))176 b(#)45 b(Wait)f(for)g(the)g(background)g(task)g(to)g(finish)416 4551 y(print)g('Main)g(program)f(waited)h(until)g(background)f(was)h (done.')0 4837 y Fs(The)24 b(principal)f(challenge)g(of)h (multi-threaded)e(applications)h(is)i(coordinating)c(threads)j(that)g (share)g(data)h(or)f(other)f(resources.)36 b(T)-7 b(o)0 4936 y(that)28 b(end,)i(the)e(threading)f(module)f(pro)o(vides)h(a)i (number)d(of)i(synchronization)d(primiti)n(v)o(es)j(including)e(locks,) k(e)n(v)o(ents,)f(condition)0 5036 y(v)n(ariables,)19 b(and)h(semaphores.)0 5183 y(While)29 b(those)f(tools)g(are)g(po)n (werful,)h(minor)e(design)h(errors)f(can)h(result)g(in)h(problems)e (that)h(are)g(dif)n(\002cult)g(to)h(reproduce.)46 b(So,)31 b(the)0 5283 y(preferred)23 b(approach)g(to)i(task)g(coordination)d(is) k(to)f(concentrate)e(all)j(access)g(to)f(a)g(resource)f(in)h(a)g (single)g(thread)f(and)h(then)f(use)i(the)0 5382 y Fp(Queue)16 b Fs(module)f(to)i(feed)f(that)g(thread)g(with)g(requests)g(from)g (other)f(threads.)23 b(Applications)15 b(using)h Fp(Queue)g Fs(objects)h(for)e(inter)n(-thread)p 0 5549 3901 4 v 0 5649 a Fo(86)1784 b(Chapter)23 b(11.)52 b(Br)q(ief)24 b(T)-10 b(our)23 b(of)g(the)h(Standard)g(Libr)o(ar)r(y)f(\226)h(P)m(ar) s(t)f(II)p eop end %%Page: 87 95 TeXDict begin 87 94 bop 0 83 a Fs(communication)17 b(and)j (coordination)d(are)k(easier)f(to)g(design,)f(more)h(readable,)f(and)g (more)h(reliable.)0 410 y Fl(11.5)121 b(Logging)0 643 y Fs(The)23 b Fp(logging)g Fs(module)f(of)n(fers)h(a)h(full)f(featured) f(and)h(\003e)o(xible)g(logging)e(system.)35 b(At)25 b(its)f(simplest,)g(log)g(messages)f(are)h(sent)f(to)h(a)0 743 y(\002le)d(or)f(to)g Fp(sys.stderr)p Fs(:)416 981 y Fk(import)43 b(logging)416 1072 y(logging.debug\('Debugging)d (information'\))416 1164 y(logging.info\('Informational)g(message'\)) 416 1255 y(logging.warning\('Warning:config)f(file)44 b(\045s)h(not)f(found',)g('server.conf'\))416 1346 y (logging.error\('Error)d(occurred'\))416 1438 y (logging.critical\('Critical)f(error)k(--)g(shutting)g(down'\))0 1724 y Fs(This)20 b(produces)f(the)h(follo)n(wing)f(output:)416 1962 y Fk(WARNING:root:Warning:config)40 b(file)k(server.conf)f(not)h (found)416 2053 y(ERROR:root:Error)e(occurred)416 2145 y(CRITICAL:root:Critical)f(error)j(--)g(shutting)g(down)0 2431 y Fs(By)31 b(def)o(ault,)g(informational)d(and)h(deb)n(ugging)f (messages)i(are)g(suppressed)f(and)h(the)g(output)e(is)j(sent)g(to)f (standard)f(error)-5 b(.)54 b(Other)0 2531 y(output)25 b(options)g(include)g(routing)f(messages)i(through)e(email,)j (datagrams,)f(sock)o(ets,)h(or)f(to)g(an)g(HTTP)g(Serv)o(er)-5 b(.)42 b(Ne)n(w)26 b(\002lters)h(can)0 2630 y(select)21 b(dif)n(ferent)d(routing)h(based)h(on)f(message)i(priority:)i Fp(DEBUG)p Fs(,)d Fp(INFO)p Fs(,)g Fp(WARNING)p Fs(,)f Fp(ERROR)p Fs(,)h(and)f Fp(CRITICAL)p Fs(.)0 2777 y(The)f(logging)f (system)h(can)g(be)g(con\002gured)e(directly)i(from)f(Python)g(or)h (can)g(be)h(loaded)e(from)g(a)i(user)f(editable)g(con\002guration)d (\002le)k(for)0 2877 y(customized)g(logging)f(without)i(altering)f(the) h(application.)0 3204 y Fl(11.6)121 b(W)l(eak)34 b(Ref)l(erences)0 3437 y Fs(Python)18 b(does)i(automatic)e(memory)g(management)f (\(reference)g(counting)h(for)h(most)g(objects)h(and)f(garbage)e (collection)h(to)i(eliminate)0 3537 y(c)o(ycles\).)k(The)c(memory)f(is) i(freed)e(shortly)g(after)h(the)h(last)g(reference)d(to)i(it)h(has)g (been)e(eliminated.)0 3683 y(This)24 b(approach)e(w)o(orks)h(\002ne)h (for)f(most)h(applications)e(b)n(ut)i(occasionally)e(there)i(is)g(a)h (need)e(to)h(track)f(objects)g(only)g(as)i(long)e(as)h(the)o(y)0 3783 y(are)f(being)g(used)g(by)g(something)f(else.)35 b(Unfortunately)-5 b(,)21 b(just)i(tracking)f(them)h(creates)h(a)g (reference)d(that)j(mak)o(es)f(them)g(permanent.)0 3883 y(The)e Fp(weakref)g Fs(module)f(pro)o(vides)g(tools)h(for)g(tracking)f (objects)h(without)g(creating)g(a)h(reference.)27 b(When)21 b(the)g(object)g(is)i(no)e(longer)0 3982 y(needed,)29 b(it)h(is)f(automatically)e(remo)o(v)o(ed)f(from)i(a)h(weakref)f(table) g(and)g(a)h(callback)f(is)i(triggered)d(for)h(weakref)f(objects.)50 b(T)-7 b(ypical)0 4082 y(applications)19 b(include)g(caching)g(objects) h(that)g(are)g(e)o(xpensi)n(v)o(e)f(to)h(create:)p 0 5549 3901 4 v 0 5649 a Fo(11.5.)52 b(Logging)3278 b(87)p eop end %%Page: 88 96 TeXDict begin 88 95 bop 416 174 a Fk(>>>)44 b(import)g(weakref,)f(gc) 416 266 y(>>>)h(class)g(A:)416 357 y(...)223 b(def)45 b(__init__\(self,)d(value\):)416 448 y(...)582 b(self.value)43 b(=)i(value)416 540 y(...)223 b(def)45 b(__repr__\(self\):)416 631 y(...)582 b(return)44 b(str\(self.value\))416 722 y(...)416 814 y(>>>)g(a)h(=)f(A\(10\))851 b(#)45 b(create)f(a)g (reference)416 905 y(>>>)g(d)h(=)f(weakref.WeakValueDictionary\(\))416 996 y(>>>)g(d['primary'])f(=)h(a)538 b(#)45 b(does)f(not)g(create)g(a)h (reference)416 1088 y(>>>)f(d['primary'])715 b(#)45 b(fetch)f(the)g (object)g(if)g(it)h(is)f(still)g(alive)416 1179 y(10)416 1270 y(>>>)g(del)g(a)1031 b(#)45 b(remove)f(the)g(one)g(reference)416 1362 y(>>>)g(gc.collect\(\))715 b(#)45 b(run)f(garbage)g(collection)f (right)h(away)416 1453 y(0)416 1544 y(>>>)g(d['primary'])715 b(#)45 b(entry)f(was)g(automatically)f(removed)416 1636 y(Traceback)g(\(most)h(recent)g(call)g(last\):)505 1727 y(File)g("",)f(line)h(1,)g(in)h(-toplevel-)595 1818 y(d['primary'])715 b(#)45 b(entry)f(was)g(automatically)f(removed) 505 1910 y(File)h("C:/python25/lib/weakref.py",)c(line)k(46,)h(in)f (__getitem__)595 2001 y(o)h(=)f(self.data[key]\(\))416 2092 y(KeyError:)f('primary')0 2541 y Fl(11.7)121 b(T)-14 b(ools)33 b(f)l(or)i(W)l(or)r(king)e(with)h(Lists)0 2774 y Fs(Man)o(y)23 b(data)h(structure)f(needs)h(can)g(be)g(met)h(with)f (the)g(b)n(uilt-in)g(list)h(type.)36 b(Ho)n(we)n(v)o(er)m(,)24 b(sometimes)f(there)h(is)h(a)g(need)e(for)h(alternati)n(v)o(e)0 2873 y(implementations)18 b(with)j(dif)n(ferent)d(performance)f (trade-of)n(fs.)0 3020 y(The)g Fp(array)g Fs(module)f(pro)o(vides)f(an) j Fp(array\(\))e Fs(object)h(that)g(is)i(lik)o(e)e(a)h(list)g(that)g (stores)f(only)g(homogenous)d(data)j(and)g(stores)g(it)h(more)0 3120 y(compactly)-5 b(.)23 b(The)d(follo)n(wing)e(e)o(xample)h(sho)n (ws)h(an)g(array)f(of)h(numbers)f(stored)g(as)i(tw)o(o)f(byte)g (unsigned)e(binary)h(numbers)g(\(typecode)0 3219 y Fp("H")p Fs(\))h(rather)f(than)h(the)g(usual)g(16)g(bytes)g(per)g(entry)f(for)h (re)o(gular)e(lists)k(of)e(p)o(ython)e(int)j(objects:)416 3457 y Fk(>>>)44 b(from)g(array)g(import)g(array)416 3549 y(>>>)g(a)h(=)f(array\('H',)f([4000,)h(10,)h(700,)f(22222]\))416 3640 y(>>>)g(sum\(a\))416 3731 y(26932)416 3823 y(>>>)g(a[1:3])416 3914 y(array\('H',)f([10,)h(700]\))0 4200 y Fs(The)20 b Fp(collections)g Fs(module)f(pro)o(vides)g(a)i Fp(deque\(\))f Fs(object)h(that)f(is)i(lik)o(e)f(a)g(list)h(with)f(f)o(aster)g (appends)e(and)i(pops)f(from)g(the)g(left)0 4300 y(side)25 b(b)n(ut)g(slo)n(wer)g(lookups)f(in)h(the)g(middle.)38 b(These)25 b(objects)g(are)g(well)h(suited)e(for)h(implementing)e (queues)h(and)g(breadth)g(\002rst)i(tree)0 4400 y(searches:)p 0 5549 3901 4 v 0 5649 a Fo(88)1784 b(Chapter)23 b(11.)52 b(Br)q(ief)24 b(T)-10 b(our)23 b(of)g(the)h(Standard)g(Libr)o(ar)r(y)f (\226)h(P)m(ar)s(t)f(II)p eop end %%Page: 89 97 TeXDict begin 89 96 bop 416 174 a Fk(>>>)44 b(from)g(collections)f (import)h(deque)416 266 y(>>>)g(d)h(=)f(deque\(["task1",)f("task2",)g ("task3"]\))416 357 y(>>>)h(d.append\("task4"\))416 448 y(>>>)g(print)g("Handling",)f(d.popleft\(\))416 540 y(Handling)g(task1) 416 722 y(unsearched)g(=)h(deque\([starting_node]\))416 814 y(def)g(breadth_first_search\(unsearched\):)595 905 y(node)g(=)h(unsearched.popleft\(\))595 996 y(for)f(m)h(in)f (gen_moves\(node\):)774 1088 y(if)h(is_goal\(m\):)954 1179 y(return)e(m)774 1270 y(unsearched.append\(m\))0 1556 y Fs(In)27 b(addition)f(to)h(alternati)n(v)o(e)f(list)i (implementations,)f(the)g(library)f(also)h(of)n(fers)g(other)f(tools)h (such)g(as)h(the)f Fp(bisect)g Fs(module)e(with)0 1656 y(functions)19 b(for)g(manipulating)f(sorted)i(lists:)416 1894 y Fk(>>>)44 b(import)g(bisect)416 1986 y(>>>)g(scores)g(=)g ([\(100,)g('perl'\),)g(\(200,)g('tcl'\),)f(\(400,)h('lua'\),)g(\(500,)g ('python'\)])416 2077 y(>>>)g(bisect.insort\(scores,)d(\(300,)j ('ruby'\)\))416 2168 y(>>>)g(scores)416 2260 y([\(100,)f('perl'\),)h (\(200,)g('tcl'\),)g(\(300,)f('ruby'\),)h(\(400,)g('lua'\),)g(\(500,)g ('python'\)])0 2546 y Fs(The)28 b Fp(heapq)g Fs(module)f(pro)o(vides)f (functions)h(for)h(implementing)e(heaps)i(based)g(on)f(re)o(gular)g (lists.)51 b(The)28 b(lo)n(west)g(v)n(alued)f(entry)h(is)0 2645 y(al)o(w)o(ays)c(k)o(ept)f(at)h(position)e(zero.)35 b(This)23 b(is)h(useful)f(for)g(applications)f(which)h(repeatedly)f (access)i(the)g(smallest)g(element)e(b)n(ut)i(do)f(not)0 2745 y(w)o(ant)d(to)h(run)e(a)i(full)f(list)h(sort:)416 2983 y Fk(>>>)44 b(from)g(heapq)g(import)g(heapify,)f(heappop,)h (heappush)416 3074 y(>>>)g(data)g(=)h([1,)f(3,)h(5,)f(7,)h(9,)f(2,)h (4,)f(6,)g(8,)h(0])416 3166 y(>>>)f(heapify\(data\))984 b(#)45 b(rearrange)e(the)h(list)g(into)h(heap)f(order)416 3257 y(>>>)g(heappush\(data,)e(-5\))762 b(#)45 b(add)f(a)h(new)f(entry) 416 3348 y(>>>)g([heappop\(data\))e(for)j(i)f(in)h(range\(3\)])88 b(#)45 b(fetch)f(the)g(three)g(smallest)f(entries)416 3440 y([-5,)h(0,)g(1])0 3886 y Fl(11.8)121 b(Decimal)33 b(Floating)i(P)-6 b(oint)33 b(Ar)r(ithmetic)0 4118 y Fs(The)d Fp(decimal)f Fs(module)g(of)n(fers)g(a)h Fp(Decimal)f Fs(datatype)g(for)h(decimal)f(\003oating)g(point)h(arithmetic.)53 b(Compared)29 b(to)h(the)g(b)n(uilt-)0 4218 y(in)25 b Fp(float)g Fs(implementation)e(of)i(binary)f(\003oating)g(point,)i(the) f(ne)n(w)g(class)h(is)g(especially)f(helpful)f(for)g(\002nancial)h (applications)f(and)0 4318 y(other)e(uses)i(which)f(require)e(e)o(xact) i(decimal)f(representation,)g(control)g(o)o(v)o(er)g(precision,)g (control)g(o)o(v)o(er)g(rounding)e(to)j(meet)g(le)o(gal)g(or)0 4417 y(re)o(gulatory)d(requirements,)i(tracking)f(of)i(signi\002cant)f (decimal)g(places,)h(or)g(for)f(applications)g(where)g(the)h(user)f(e)o (xpects)g(the)h(results)0 4517 y(to)d(match)g(calculations)f(done)g(by) h(hand.)0 4664 y(F)o(or)d(e)o(xample,)f(calculating)f(a)j(5\045)f(tax)g (on)g(a)g(70)g(cent)g(phone)e(char)o(ge)g(gi)n(v)o(es)i(dif)n(ferent)e (results)i(in)h(decimal)e(\003oating)g(point)g(and)h(binary)0 4763 y(\003oating)i(point.)25 b(The)20 b(dif)n(ference)e(becomes)h (signi\002cant)h(if)g(the)g(results)h(are)f(rounded)e(to)i(the)g (nearest)g(cent:)p 0 5549 3901 4 v 0 5649 a Fo(11.8.)52 b(Decimal)24 b(Floating)h(P)l(oint)f(Ar)q(ithmetic)2353 b(89)p eop end %%Page: 90 98 TeXDict begin 90 97 bop 236 174 a Fk(>>>)45 b(from)f(decimal)f(import) 1312 187 y(*)236 266 y(>>>)i(Decimal\('0.70'\))1133 279 y(*)1223 266 y(Decimal\('1.05'\))236 357 y(Decimal\("0.7350"\))236 448 y(>>>)g(.70)595 461 y(*)685 448 y(1.05)236 540 y (0.73499999999999999)0 826 y Fs(The)17 b Fp(Decimal)g Fs(result)h(k)o(eeps)f(a)h(trailing)f(zero,)g(automatically)f (inferring)g(four)g(place)h(signi\002cance)g(from)f(multiplicands)h (with)g(tw)o(o)0 925 y(place)26 b(signi\002cance.)42 b(Decimal)27 b(reproduces)d(mathematics)h(as)i(done)f(by)f(hand)h(and)g (a)n(v)n(oids)g(issues)h(that)f(can)h(arise)f(when)g(binary)0 1025 y(\003oating)19 b(point)h(cannot)f(e)o(xactly)g(represent)g (decimal)h(quantities.)0 1172 y(Exact)f(representation)e(enables)h(the) i Fp(Decimal)e Fs(class)i(to)f(perform)f(modulo)f(calculations)h(and)h (equality)f(tests)i(that)g(are)f(unsuitable)0 1272 y(for)h(binary)e (\003oating)i(point:)236 1510 y Fk(>>>)45 b(Decimal\('1.00'\))d(\045)j (Decimal\('.10'\))236 1601 y(Decimal\("0.00"\))236 1692 y(>>>)g(1.00)f(\045)g(0.10)236 1784 y(0.09999999999999995)236 1966 y(>>>)h(sum\([Decimal\('0.1'\)])1316 1979 y(*)1361 1966 y(10\))40 b(==)45 b(Decimal\('1.0'\))236 2058 y(True)236 2149 y(>>>)g(sum\([0.1])821 2162 y(*)866 2149 y(10\))d(==)j(1.0)236 2240 y(False)0 2527 y Fs(The)20 b Fp(decimal)f Fs(module)g(pro)o(vides) g(arithmetic)g(with)h(as)h(much)e(precision)h(as)g(needed:)236 2765 y Fk(>>>)45 b(getcontext\(\).prec)d(=)i(36)236 2856 y(>>>)h(Decimal\(1\))e(/)h(Decimal\(7\))236 2947 y (Decimal\("0.14285714285714285714285714285)o(714285)o(7"\))p 0 5549 3901 4 v 0 5649 a Fo(90)1784 b(Chapter)23 b(11.)52 b(Br)q(ief)24 b(T)-10 b(our)23 b(of)g(the)h(Standard)g(Libr)o(ar)r(y)f (\226)h(P)m(ar)s(t)f(II)p eop end %%Page: 91 99 TeXDict begin 91 98 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3415 427 y Fn(TWEL)-13 b(VE)p 0 515 V 2765 978 a Fy(What)57 b(No)m(w?)0 1457 y Fs(Reading)20 b(this)h(tutorial)f(has)h(probably)e (reinforced)f(your)i(interest)g(in)h(using)f(Python)g(\227)h(you)f (should)g(be)g(eager)g(to)h(apply)f(Python)f(to)0 1556 y(solving)g(your)g(real-w)o(orld)g(problems.)k(Where)d(should)g(you)f (go)h(to)g(learn)g(more?)0 1703 y(This)g(tutorial)g(is)h(part)f(of)g (Python')-5 b(s)19 b(documentation)f(set.)25 b(Some)20 b(other)f(documents)g(in)h(the)h(set)g(are:)137 1933 y(\225)42 b Fq(Python)19 b(Libr)o(ary)h(Refer)m(ence)p Fs(:)208 2066 y(Y)-9 b(ou)25 b(should)g(bro)n(wse)g(through)f(this)i (manual,)h(which)e(gi)n(v)o(es)g(complete)g(\(though)f(terse\))i (reference)e(material)i(about)f(types,)208 2165 y(functions,)19 b(and)h(the)h(modules)e(in)i(the)g(standard)e(library)-5 b(.)25 b(The)c(standard)e(Python)h(distrib)n(ution)f(includes)h(a)i Fq(lot)e Fs(of)h(additional)208 2265 y(code.)44 b(There)26 b(are)h(modules)e(to)i(read)i(U)t Fm(N)t(I)t(X)h Fs(mailbox)o(es,)c (retrie)n(v)o(e)g(documents)f(via)i(HTTP)-9 b(,)27 b(generate)e(random) g(numbers,)208 2365 y(parse)19 b(command-line)d(options,)j(write)g(CGI) h(programs,)e(compress)g(data,)h(and)g(man)o(y)f(other)h(tasks.)25 b(Skimming)18 b(through)g(the)208 2464 y(Library)g(Reference)i(will)g (gi)n(v)o(e)g(you)f(an)h(idea)g(of)g(what')-5 b(s)21 b(a)n(v)n(ailable.)137 2630 y(\225)42 b Fq(Installing)19 b(Python)g(Modules)g Fs(e)o(xplains)g(ho)n(w)h(to)g(install)h(e)o (xternal)e(modules)g(written)h(by)g(other)f(Python)g(users.)137 2796 y(\225)42 b Fq(Langua)o(g)o(e)18 b(Refer)m(ence)p Fs(:)24 b(A)d(detailed)f(e)o(xplanation)e(of)i(Python')-5 b(s)19 b(syntax)g(and)h(semantics.)25 b(It')-5 b(s)21 b(hea)n(vy)e(reading,)g(b)n(ut)h(is)h(useful)208 2896 y(as)f(a)h(complete)e(guide)g(to)i(the)f(language)e(itself.)0 3126 y(More)i(Python)f(resources:)137 3356 y(\225)42 b Ft(http://www)l(.p)n(ython.org)-7 b Fs(:)38 b(The)25 b(major)h(Python)e(W)-7 b(eb)27 b(site.)43 b(It)27 b(contains)e(code,)h (documentation,)e(and)i(pointers)f(to)h(Python-)208 3455 y(related)e(pages)h(around)e(the)i(W)-7 b(eb)m(.)40 b(This)25 b(W)-7 b(eb)26 b(site)g(is)g(mirrored)d(in)i(v)n(arious)f(places)h (around)e(the)i(w)o(orld,)h(such)f(as)g(Europe,)208 3555 y(Japan,)19 b(and)h(Australia;)g(a)h(mirror)d(may)i(be)g(f)o(aster)h (than)e(the)i(main)e(site,)i(depending)d(on)i(your)e(geographical)g (location.)137 3721 y(\225)42 b Ft(http://docs)o(.p)n(ython.org)-6 b Fs(:)26 b(F)o(ast)20 b(access)h(to)g(Python')-5 b(s)19 b(documentation.)137 3887 y(\225)42 b Ft(http://cheeseshop)m(.p)n (ython.)o(org)-7 b Fs(:)45 b(The)30 b(Python)f(P)o(ackage)f(Inde)o(x,)j (nicknamed)d(the)h(Cheese)i(Shop,)g(is)g(an)e(inde)o(x)g(of)h(user)n(-) 208 3987 y(created)18 b(Python)f(modules)h(that)h(are)f(a)n(v)n (ailable)h(for)f(do)n(wnload.)k(Once)d(you)f(be)o(gin)f(releasing)h (code,)g(you)g(can)h(re)o(gister)f(it)h(here)208 4086 y(so)h(that)g(others)g(can)g(\002nd)g(it.)137 4253 y(\225)42 b Ft(http://aspn.activ)n(estate)o(.com/ASPN/)o(Pytho)o(n/Co)o(okbo)o (ok/)-6 b Fs(:)30 b(The)22 b(Python)f(Cookbook)f(is)k(a)e(sizable)h (collection)e(of)i(code)e(e)o(x-)208 4352 y(amples,)g(lar)o(ger)f (modules,)h(and)g(useful)g(scripts.)30 b(P)o(articularly)20 b(notable)h(contrib)n(utions)f(are)h(collected)g(in)h(a)g(book)e(also)i (titled)208 4452 y Fq(Python)d(Cookbook)f Fs(\(O'Reilly)i(&)g (Associates,)h(ISBN)g(0-596-00797-3.\))0 4682 y(F)o(or)h (Python-related)e(questions)i(and)g(problem)f(reports,)h(you)g(can)g (post)h(to)f(the)h(ne)n(wsgroup)d Ft(comp)m(.lang.p)n(ython)p Fs(,)g(or)i(send)g(them)g(to)0 4781 y(the)g(mailing)f(list)i(at)g Ft(p)n(ython-list@p)n(ython.org)p Fs(.)h(The)e(ne)n(wsgroup)e(and)h (mailing)g(list)i(are)f(gate)n(w)o(ayed,)e(so)j(messages)f(posted)f(to) h(one)0 4881 y(will)f(automatically)e(be)h(forw)o(arded)e(to)i(the)g (other)-5 b(.)25 b(There)19 b(are)i(around)d(120)h(postings)h(a)g(day)g (\(with)g(peaks)g(up)g(to)g(se)n(v)o(eral)g(hundred\),)0 4981 y(asking)h(\(and)f(answering\))g(questions,)h(suggesting)f(ne)n(w) i(features,)f(and)g(announcing)d(ne)n(w)j(modules.)28 b(Before)21 b(posting,)g(be)g(sure)g(to)0 5080 y(check)d(the)h(list)g (of)g(Frequently)e(Ask)o(ed)i(Questions)f(\(also)h(called)f(the)h(F)-6 b(A)h(Q\),)19 b(or)f(look)g(for)g(it)i(in)f(the)g(`)p Ft(Misc/)p Fs(')f(directory)g(of)g(the)h(Python)0 5180 y(source)j(distrib)n(ution.)30 b(Mailing)22 b(list)i(archi)n(v)o(es)d (are)i(a)n(v)n(ailable)f(at)h Ft(http://mail.p)n(ython.org)o(/pi)o(pe)o (r)r(mail)o(/)-6 b Fs(.)32 b(The)23 b(F)-6 b(A)h(Q)23 b(answers)g(man)o(y)e(of)0 5279 y(the)f(questions)g(that)g(come)f(up)h (again)f(and)h(again,)f(and)h(may)f(already)g(contain)h(the)g(solution) f(for)h(your)f(problem.)p 0 5549 3901 4 v 3808 5649 a Fo(91)p eop end %%Page: 92 100 TeXDict begin 92 99 bop 0 5549 3901 4 v 0 5649 a Fo(92)p eop end %%Page: 93 101 TeXDict begin 93 100 bop 0 83 3901 9 v 3480 230 a Fo(APPENDIX)3814 427 y Fn(A)p 0 515 V 641 978 a Fy(Inter)n(activ)-5 b(e)58 b(Input)f(Editing)g(and)g(Histor)6 b(y)2821 1227 y(Substitution)0 1706 y Fs(Some)25 b(v)o(ersions)g(of)h(the)g(Python)e(interpreter)g (support)h(editing)g(of)g(the)h(current)e(input)h(line)h(and)f(history) g(substitution,)h(similar)g(to)0 1805 y(f)o(acilities)k(found)e(in)h (the)g(K)m(orn)g(shell)h(and)e(the)i(GNU)f(Bash)h(shell.)53 b(This)29 b(is)i(implemented)c(using)i(the)g Fq(GNU)h(Readline)e Fs(library)-5 b(,)0 1905 y(which)18 b(supports)f(Emacs-style)g(and)h (vi-style)f(editing.)24 b(This)18 b(library)f(has)h(its)h(o)n(wn)f (documentation)d(which)j(I)g(w)o(on')o(t)f(duplicate)g(here;)0 2005 y(ho)n(we)n(v)o(er)m(,)h(the)j(basics)g(are)f(easily)h(e)o (xplained.)j(The)c(interacti)n(v)o(e)f(editing)h(and)g(history)g (described)f(here)h(are)h(optionally)d(a)n(v)n(ailable)i(in)0 2104 y(the)i(U)t Fm(N)t(I)t(X)h Fs(and)d(Cygwin)g(v)o(ersions)f(of)h (the)g(interpreter)-5 b(.)0 2251 y(This)22 b(chapter)f(does)g Fq(not)g Fs(document)f(the)i(editing)f(f)o(acilities)h(of)g(Mark)f (Hammond')-5 b(s)20 b(PythonW)m(in)g(package)g(or)i(the)g(Tk-based)e (en)m(vi-)0 2351 y(ronment,)i(IDLE,)h(distrib)n(uted)f(with)i(Python.) 33 b(The)23 b(command)f(line)h(history)g(recall)g(which)g(operates)g (within)g(DOS)h(box)o(es)e(on)h(NT)0 2450 y(and)d(some)g(other)f(DOS)i (and)e(W)m(indo)n(ws)h(\003a)n(v)n(ors)g(is)i(yet)e(another)f(beast.)0 2778 y Fl(A.1)120 b(Line)34 b(Editing)0 3011 y Fs(If)24 b(supported,)f(input)g(line)i(editing)e(is)i(acti)n(v)o(e)f(whene)n(v)o (er)e(the)i(interpreter)f(prints)g(a)i(primary)e(or)g(secondary)g (prompt.)35 b(The)24 b(current)0 3110 y(line)19 b(can)f(be)g(edited)g (using)g(the)h(con)m(v)o(entional)c(Emacs)j(control)g(characters.)23 b(The)18 b(most)h(important)e(of)h(these)h(are:)24 b Fp(C-A)18 b Fs(\(Control-A\))0 3210 y(mo)o(v)o(es)i(the)h(cursor)f(to)h (the)g(be)o(ginning)e(of)i(the)g(line,)g Fp(C-E)g Fs(to)g(the)g(end,)g Fp(C-B)g Fs(mo)o(v)o(es)f(it)i(one)e(position)g(to)i(the)f(left,)g Fp(C-F)g Fs(to)g(the)g(right.)0 3309 y(Backspace)j(erases)i(the)e (character)g(to)h(the)g(left)g(of)f(the)h(cursor)m(,)f Fp(C-D)h Fs(the)g(character)e(to)i(its)h(right.)38 b Fp(C-K)25 b Fs(kills)g(\(erases\))g(the)g(rest)g(of)0 3409 y(the)c(line)f(to)h(the)g(right)f(of)g(the)h(cursor)m(,)e Fp(C-Y)i Fs(yanks)f(back)g(the)h(last)g(killed)f(string.)26 b Fp(C-underscore)19 b Fs(undoes)h(the)h(last)g(change)e(you)0 3509 y(made;)h(it)h(can)f(be)g(repeated)f(for)g(cumulati)n(v)o(e)g(ef)n (fect.)0 3836 y Fl(A.2)120 b(Histor)t(y)33 b(Substitution)0 4069 y Fs(History)21 b(substitution)f(w)o(orks)g(as)i(follo)n(ws.)k (All)c(non-empty)c(input)i(lines)i(issued)f(are)f(sa)n(v)o(ed)h(in)g(a) g(history)g(b)n(uf)n(fer)m(,)e(and)h(when)g(a)i(ne)n(w)0 4169 y(prompt)h(is)j(gi)n(v)o(en)e(you)g(are)h(positioned)e(on)i(a)g (ne)n(w)g(line)g(at)g(the)g(bottom)f(of)h(this)g(b)n(uf)n(fer)-5 b(.)38 b Fp(C-P)25 b Fs(mo)o(v)o(es)f(one)g(line)h(up)g(\(back\))e(in)j (the)0 4268 y(history)c(b)n(uf)n(fer)m(,)f Fp(C-N)i Fs(mo)o(v)o(es)f (one)g(do)n(wn.)32 b(An)o(y)22 b(line)h(in)g(the)f(history)g(b)n(uf)n (fer)g(can)g(be)h(edited;)h(an)f(asterisk)f(appears)g(in)h(front)f(of)h (the)0 4368 y(prompt)e(to)h(mark)g(a)g(line)h(as)g(modi\002ed.)29 b(Pressing)23 b(the)f Fp(Return)g Fs(k)o(e)o(y)f(passes)i(the)f (current)f(line)i(to)f(the)g(interpreter)-5 b(.)30 b Fp(C-R)23 b Fs(starts)g(an)0 4467 y(incremental)c(re)n(v)o(erse)g (search;)h Fp(C-S)g Fs(starts)h(a)g(forw)o(ard)d(search.)0 4795 y Fl(A.3)120 b(K)-5 b(e)n(y)33 b(Bindings)0 5028 y Fs(The)24 b(k)o(e)o(y)g(bindings)f(and)g(some)i(other)e(parameters)g (of)h(the)h(Readline)f(library)f(can)h(be)g(customized)f(by)h(placing)g (commands)e(in)j(an)0 5127 y(initialization)20 b(\002le)g(called)g(`)p Ft(\230/.inputrc)p Fs('.)j(K)n(e)o(y)d(bindings)e(ha)n(v)o(e)i(the)g (form)p 0 5549 3901 4 v 3808 5649 a Fo(93)p eop end %%Page: 94 102 TeXDict begin 94 101 bop 236 174 a Fk(key-name:)44 b(function-name)0 442 y Fs(or)236 680 y Fk("string":)g(function-name)0 967 y Fs(and)20 b(options)f(can)h(be)g(set)h(with)236 1205 y Fk(set)45 b(option-name)e(value)0 1491 y Fs(F)o(or)20 b(e)o(xample:)236 1729 y Fk(#)45 b(I)g(prefer)e(vi-style)h(editing:)236 1821 y(set)h(editing-mode)d(vi)236 2003 y(#)j(Edit)f(using)g(a)h (single)f(line:)236 2095 y(set)h(horizontal-scroll-mode)c(On)236 2277 y(#)k(Rebind)f(some)g(keys:)236 2369 y(Meta-h:)g (backward-kill-word)236 2460 y("\\C-u":)g(universal-argument)236 2551 y("\\C-x\\C-r":)f(re-read-init-file)0 2837 y Fs(Note)27 b(that)f(the)h(def)o(ault)f(binding)f(for)h Fp(Tab)h Fs(in)g(Python)e(is)j(to)f(insert)f(a)h Fp(Tab)g Fs(character)f (instead)g(of)g(Readline')-5 b(s)27 b(def)o(ault)f(\002lename)0 2937 y(completion)18 b(function.)24 b(If)c(you)f(insist,)i(you)e(can)h (o)o(v)o(erride)e(this)j(by)e(putting)236 3175 y Fk(Tab:)44 b(complete)0 3461 y Fs(in)22 b(your)e(`)p Ft(\230/.inputrc)p Fs('.)27 b(\(Of)22 b(course,)f(this)h(mak)o(es)g(it)g(harder)e(to)i (type)g(indented)e(continuation)f(lines)j(if)g(you')l(re)e(accustomed)g (to)i(using)0 3561 y Fp(Tab)e Fs(for)g(that)g(purpose.\))0 3708 y(Automatic)c(completion)f(of)i(v)n(ariable)f(and)h(module)e (names)i(is)h(optionally)d(a)n(v)n(ailable.)24 b(T)-7 b(o)17 b(enable)f(it)i(in)f(the)g(interpreter')-5 b(s)16 b(interacti)n(v)o(e)0 3807 y(mode,)j(add)h(the)g(follo)n(wing)e(to)j (your)e(startup)g(\002le:)1464 3777 y Fi(1)236 4046 y Fk(import)44 b(rlcompleter,)f(readline)236 4137 y (readline.parse_and_bind\('tab:)d(complete'\))0 4423 y Fs(This)27 b(binds)g(the)g Fp(Tab)g Fs(k)o(e)o(y)g(to)g(the)g (completion)e(function,)i(so)h(hitting)e(the)h Fp(Tab)g Fs(k)o(e)o(y)g(twice)g(suggests)h(completions;)h(it)e(looks)g(at)0 4523 y(Python)21 b(statement)g(names,)h(the)g(current)f(local)h(v)n (ariables,)f(and)g(the)h(a)n(v)n(ailable)g(module)e(names.)30 b(F)o(or)22 b(dotted)f(e)o(xpressions)f(such)i(as)0 4622 y Fp(string.a)p Fs(,)e(it)i(will)g(e)n(v)n(aluate)e(the)h(e)o (xpression)e(up)i(to)g(the)g(\002nal)g(`)p Fp(.)p Fs(')28 b(and)20 b(then)h(suggest)f(completions)g(from)g(the)h(attrib)n(utes)g (of)g(the)0 4722 y(resulting)d(object.)24 b(Note)19 b(that)g(this)h (may)f(e)o(x)o(ecute)e(application-de\002ned)f(code)i(if)i(an)f(object) f(with)h(a)h Fp(__getattr__\(\))d Fs(method)h(is)0 4822 y(part)i(of)g(the)g(e)o(xpression.)0 4969 y(A)j(more)e(capable)h (startup)f(\002le)i(might)f(look)f(lik)o(e)i(this)g(e)o(xample.)29 b(Note)23 b(that)f(this)h(deletes)f(the)g(names)g(it)h(creates)g(once)e (the)o(y)h(are)g(no)0 5068 y(longer)e(needed;)h(this)h(is)h(done)e (since)g(the)h(startup)f(\002le)h(is)h(e)o(x)o(ecuted)c(in)j(the)g (same)g(namespace)e(as)i(the)g(interacti)n(v)o(e)e(commands,)g(and)0 5168 y(remo)o(ving)j(the)i(names)g(a)n(v)n(oids)g(creating)f(side)i(ef) n(fects)f(in)g(the)g(interacti)n(v)o(e)f(en)m(vironment.)37 b(Y)-9 b(ou)25 b(may)f(\002nd)h(it)h(con)m(v)o(enient)d(to)i(k)o(eep)p 0 5239 1560 4 v 90 5294 a Fh(1)120 5318 y Fm(Python)17 b(will)h(e)o(x)o(ecute)i(the)e(contents)h(of)d(a)i(\002le)f (identi\002ed)j(by)d(the)g(PYTHONST)-6 b(AR)l(TUP)15 b(en)m(vironment)20 b(v)n(ariable)g(when)d(you)h(start)g(an)f (interacti)n(v)o(e)k(interpreter)l(.)p 0 5549 3901 4 v 0 5649 a Fo(94)1547 b(Appendix)24 b(A.)47 b(Inter)o(activ)n(e)23 b(Input)h(Editing)h(and)e(Histor)r(y)g(Substitution)p eop end %%Page: 95 103 TeXDict begin 95 102 bop 0 83 a Fs(some)20 b(of)g(the)g(imported)f (modules,)g(such)g(as)i Fp(os)p Fs(,)g(which)e(turn)h(out)g(to)g(be)g (needed)f(in)h(most)g(sessions)h(with)g(the)f(interpreter)-5 b(.)236 230 y Fk(#)45 b(Add)f(auto-completion)f(and)h(a)h(stored)e (history)h(file)g(of)h(commands)e(to)i(your)f(Python)236 321 y(#)h(interactive)e(interpreter.)g(Requires)g(Python)h(2.0+,)g (readline.)f(Autocomplete)g(is)236 413 y(#)i(bound)f(to)g(the)h(Esc)f (key)g(by)h(default)f(\(you)g(can)g(change)g(it)g(-)h(see)f(readline)g (docs\).)236 504 y(#)236 595 y(#)h(Store)f(the)g(file)g(in)h (~/.pystartup,)e(and)h(set)g(an)h(environment)e(variable)g(to)i(point) 236 687 y(#)g(to)f(it:)90 b("export)43 b (PYTHONSTARTUP=/max/home/itamar/.pystartu)o(p")c(in)44 b(bash.)236 778 y(#)236 869 y(#)h(Note)f(that)g(PYTHONSTARTUP)f(does) 1626 882 y(*)1671 869 y(not)1806 882 y(*)1895 869 y(expand)h("~",)g(so) g(you)h(have)f(to)g(put)h(in)f(the)236 960 y(#)h(full)f(path)g(to)h (your)f(home)g(directory.)236 1143 y(import)g(atexit)236 1234 y(import)g(os)236 1326 y(import)g(readline)236 1417 y(import)g(rlcompleter)236 1600 y(historyPath)f(=)i (os.path.expanduser\("~/.pyhistory"\))236 1782 y(def)g (save_history\(historyPath=historyPath)o(\):)416 1874 y(import)e(readline)416 1965 y(readline.write_history_file\(historyP)o (ath\))236 2148 y(if)i(os.path.exists\(historyPath\):)416 2239 y(readline.read_history_file\(historyPa)o(th\))236 2422 y(atexit.register\(save_history\))236 2513 y(del)g(os,)f(atexit,)g (readline,)f(rlcompleter,)g(save_history,)f(historyPath)0 2962 y Fl(A.4)120 b(Commentar)t(y)0 3194 y Fs(This)25 b(f)o(acility)h(is)g(an)f(enormous)e(step)i(forw)o(ard)f(compared)f(to) j(earlier)e(v)o(ersions)h(of)g(the)g(interpreter;)h(ho)n(we)n(v)o(er)m (,)e(some)h(wishes)h(are)0 3294 y(left:)38 b(It)26 b(w)o(ould)g(be)g (nice)h(if)f(the)h(proper)d(indentation)h(were)h(suggested)f(on)h (continuation)e(lines)j(\(the)f(parser)g(kno)n(ws)g(if)g(an)g(indent)0 3394 y(tok)o(en)18 b(is)h(required)e(ne)o(xt\).)23 b(The)c(completion)d (mechanism)i(might)f(use)i(the)g(interpreter')-5 b(s)17 b(symbol)h(table.)24 b(A)19 b(command)e(to)h(check)g(\(or)0 3493 y(e)n(v)o(en)h(suggest\))h(matching)e(parentheses,)h(quotes,)g (etc.,)i(w)o(ould)e(also)i(be)f(useful.)p 0 5549 3901 4 v 0 5649 a Fo(A.4.)52 b(Commentar)r(y)3132 b(95)p eop end %%Page: 96 104 TeXDict begin 96 103 bop 0 5549 3901 4 v 0 5649 a Fo(96)p eop end %%Page: 97 105 TeXDict begin 97 104 bop 0 83 3901 9 v 3480 230 a Fo(APPENDIX)3814 427 y Fn(B)p 0 515 V 541 978 a Fy(Floating)58 b(P)-10 b(oint)56 b(Ar)s(ithmetic:)72 b(Issues)58 b(and)2913 1227 y(Limitations)0 1706 y Fs(Floating-point)20 b(numbers)h(are)h (represented)f(in)h(computer)e(hardw)o(are)h(as)i(base)g(2)f (\(binary\))e(fractions.)31 b(F)o(or)22 b(e)o(xample,)f(the)h(decimal)0 1805 y(fraction)236 1952 y Fk(0.125)0 2238 y Fs(has)e(v)n(alue)g(1/10)f (+)i(2/100)e(+)h(5/1000,)e(and)i(in)g(the)h(same)f(w)o(ay)g(the)g (binary)f(fraction)236 2385 y Fk(0.001)0 2671 y Fs(has)27 b(v)n(alue)f(0/2)g(+)i(0/4)e(+)h(1/8.)44 b(These)27 b(tw)o(o)g (fractions)f(ha)n(v)o(e)g(identical)g(v)n(alues,)i(the)e(only)g(real)h (dif)n(ference)e(being)h(that)g(the)h(\002rst)h(is)0 2771 y(written)20 b(in)g(base)h(10)e(fractional)g(notation,)g(and)h (the)g(second)f(in)h(base)h(2.)0 2918 y(Unfortunately)-5 b(,)26 b(most)h(decimal)g(fractions)f(cannot)h(be)g(represented)e(e)o (xactly)i(as)h(binary)e(fractions.)45 b(A)28 b(consequence)d(is)j (that,)h(in)0 3018 y(general,)g(the)f(decimal)f(\003oating-point)f (numbers)g(you)h(enter)h(are)g(only)f(approximated)e(by)j(the)g(binary) e(\003oating-point)g(numbers)0 3117 y(actually)20 b(stored)f(in)i(the)f (machine.)0 3264 y(The)h(problem)f(is)j(easier)f(to)g(understand)d(at)j (\002rst)h(in)f(base)f(10.)29 b(Consider)21 b(the)h(fraction)e(1/3.)29 b(Y)-9 b(ou)21 b(can)h(approximate)d(that)j(as)g(a)g(base)0 3364 y(10)e(fraction:)236 3511 y Fk(0.3)0 3797 y Fs(or)m(,)f(better)m (,)236 3944 y Fk(0.33)0 4230 y Fs(or)m(,)g(better)m(,)236 4377 y Fk(0.333)0 4663 y Fs(and)g(so)h(on.)k(No)19 b(matter)g(ho)n(w)g (man)o(y)f(digits)i(you')l(re)d(willing)i(to)h(write)f(do)n(wn,)f(the)i (result)f(will)h(ne)n(v)o(er)e(be)i(e)o(xactly)e(1/3,)h(b)n(ut)g(will)h (be)g(an)0 4762 y(increasingly)f(better)g(approximation)e(of)j(1/3.)0 4909 y(In)g(the)h(same)f(w)o(ay)-5 b(,)20 b(no)g(matter)g(ho)n(w)g(man) o(y)f(base)i(2)f(digits)h(you')l(re)d(willing)j(to)f(use,)h(the)f (decimal)g(v)n(alue)g(0.1)g(cannot)f(be)h(represented)0 5009 y(e)o(xactly)f(as)i(a)g(base)f(2)g(fraction.)k(In)c(base)g(2,)g (1/10)g(is)h(the)f(in\002nitely)g(repeating)e(fraction)236 5156 y Fk(0.00011001100110011001100110011001100110)o(011001)o(10011.)o (..)p 0 5549 3901 4 v 3808 5649 a Fo(97)p eop end %%Page: 98 106 TeXDict begin 98 105 bop 0 83 a Fs(Stop)20 b(at)h(an)o(y)e(\002nite)i (number)d(of)i(bits,)g(and)g(you)f(get)h(an)h(approximation.)g(This)g (is)g(why)e(you)g(see)i(things)f(lik)o(e:)236 230 y Fk(>>>)45 b(0.1)236 321 y(0.10000000000000001)0 607 y Fs(On)24 b(most)f(machines)g(today)-5 b(,)23 b(that)h(is)g(what)g(you')o(ll)f (see)h(if)g(you)f(enter)g(0.1)g(at)h(a)g(Python)f(prompt.)34 b(Y)-9 b(ou)23 b(may)g(not,)h(though,)e(because)0 707 y(the)28 b(number)e(of)i(bits)h(used)f(by)f(the)h(hardw)o(are)f(to)h (store)g(\003oating-point)e(v)n(alues)i(can)g(v)n(ary)f(across)h (machines,)h(and)e(Python)g(only)0 807 y(prints)19 b(a)i(decimal)e (approximation)e(to)j(the)f(true)h(decimal)f(v)n(alue)g(of)h(the)f (binary)g(approximation)e(stored)i(by)g(the)h(machine.)k(On)c(most)0 906 y(machines,)i(if)h(Python)e(were)i(to)f(print)g(the)h(true)f (decimal)g(v)n(alue)g(of)g(the)h(binary)e(approximation)e(stored)j(for) g(0.1,)g(it)i(w)o(ould)d(ha)n(v)o(e)h(to)0 1006 y(display)236 1153 y Fk(>>>)45 b(0.1)236 1244 y (0.10000000000000000555111512312578270211)o(815834)o(045410)o(15625)0 1530 y Fs(instead!)25 b(The)19 b(Python)g(prompt)g(uses)h(the)h(b)n (uiltin)f Fp(repr\(\))f Fs(function)f(to)j(obtain)e(a)i(string)e(v)o (ersion)g(of)h(e)n(v)o(erything)d(it)k(displays.)k(F)o(or)0 1630 y(\003oats,)20 b Fp(repr\()p Fq(\003oat)q Fp(\))f Fs(rounds)g(the)h(true)g(decimal)g(v)n(alue)f(to)h(17)g(signi\002cant)g (digits,)g(gi)n(ving)236 1868 y Fk(0.10000000000000001)0 2154 y Fp(repr\()p Fq(\003oat)q Fp(\))46 b Fs(produces)g(17)h (signi\002cant)g(digits)h(because)f(it)h(turns)f(out)g(that')-5 b(s)48 b(enough)e(\(on)h(most)g(machines\))g(so)g(that)0 2254 y Fp(eval\(repr\()p Fq(x)p Fp(\)\))h(==)h Fq(x)21 b Fs(e)o(xactly)e(for)h(all)h(\002nite)f(\003oats)h Fq(x)p Fs(,)f(b)n(ut)h(rounding)c(to)j(16)g(digits)g(is)i(not)d(enough)g(to)h (mak)o(e)g(that)g(true.)0 2401 y(Note)27 b(that)g(this)g(is)h(in)f(the) f(v)o(ery)g(nature)g(of)g(binary)g(\003oating-point:)36 b(this)27 b(is)h(not)e(a)h(b)n(ug)g(in)g(Python,)g(and)f(it)h(is)h(not) e(a)i(b)n(ug)e(in)h(your)0 2500 y(code)22 b(either)-5 b(.)33 b(Y)-9 b(ou')o(ll)22 b(see)i(the)f(same)g(kind)f(of)g(thing)g (in)h(all)h(languages)d(that)i(support)f(your)f(hardw)o(are')-5 b(s)22 b(\003oating-point)e(arithmetic)0 2600 y(\(although)e(some)i (languages)f(may)g(not)h Fq(display)g Fs(the)g(dif)n(ference)e(by)i (def)o(ault,)f(or)h(in)h(all)f(output)f(modes\).)0 2747 y(Python')-5 b(s)16 b(b)n(uiltin)h Fp(str\(\))f Fs(function)g(produces) f(only)h(12)g(signi\002cant)h(digits,)g(and)g(you)f(may)g(wish)h(to)g (use)h(that)f(instead.)23 b(It')-5 b(s)18 b(unusual)0 2847 y(for)i Fp(eval\(str\()p Fq(x)p Fp(\)\))e Fs(to)j(reproduce)c Fq(x)p Fs(,)k(b)n(ut)f(the)g(output)f(may)h(be)g(more)g(pleasant)f(to)i (look)e(at:)236 3085 y Fk(>>>)45 b(print)f(str\(0.1\))236 3176 y(0.1)0 3462 y Fs(It')-5 b(s)26 b(important)d(to)j(realize)f(that) g(this)h(is,)h(in)e(a)h(real)f(sense,)i(an)e(illusion:)35 b(the)25 b(v)n(alue)f(in)i(the)f(machine)f(is)i(not)f(e)o(xactly)f (1/10,)h(you')l(re)0 3562 y(simply)20 b(rounding)d(the)k Fq(display)e Fs(of)h(the)g(true)g(machine)f(v)n(alue.)0 3709 y(Other)h(surprises)g(follo)n(w)f(from)g(this)i(one.)j(F)o(or)c(e) o(xample,)f(after)h(seeing)236 3947 y Fk(>>>)45 b(0.1)236 4038 y(0.10000000000000001)0 4324 y Fs(you)23 b(may)f(be)i(tempted)e (to)i(use)g(the)f Fp(round\(\))g Fs(function)e(to)j(chop)e(it)j(back)d (to)i(the)f(single)h(digit)f(you)g(e)o(xpect.)33 b(But)24 b(that)g(mak)o(es)f(no)0 4424 y(dif)n(ference:)236 4662 y Fk(>>>)45 b(round\(0.1,)e(1\))236 4754 y(0.10000000000000001)0 5040 y Fs(The)18 b(problem)f(is)i(that)g(the)f(binary)f (\003oating-point)f(v)n(alue)i(stored)g(for)g("0.1")g(w)o(as)h(already) e(the)i(best)f(possible)h(binary)e(approximation)0 5139 y(to)j(1/10,)f(so)i(trying)e(to)h(round)f(it)i(again)e(can')o(t)g(mak)o (e)h(it)h(better:)k(it)c(w)o(as)g(already)e(as)i(good)d(as)j(it)g (gets.)0 5286 y(Another)d(consequence)f(is)j(that)g(since)f(0.1)g(is)h (not)f(e)o(xactly)f(1/10,)h(summing)e(ten)j(v)n(alues)f(of)g(0.1)f(may) h(not)g(yield)g(e)o(xactly)f(1.0,)h(either:)p 0 5549 3901 4 v 0 5649 a Fo(98)1546 b(Appendix)24 b(B)n(.)47 b(Floating)25 b(P)l(oint)f(Ar)q(ithmetic:)30 b(Issues)22 b(and)h(Limitations)p eop end %%Page: 99 107 TeXDict begin 99 106 bop 236 174 a Fk(>>>)45 b(sum)f(=)h(0.0)236 266 y(>>>)g(for)f(i)h(in)f(range\(10\):)236 357 y(...)224 b(sum)44 b(+=)h(0.1)236 448 y(...)236 540 y(>>>)g(sum)236 631 y(0.99999999999999989)0 917 y Fs(Binary)22 b(\003oating-point)e (arithmetic)i(holds)f(man)o(y)h(surprises)g(lik)o(e)g(this.)32 b(The)22 b(problem)f(with)i("0.1")e(is)i(e)o(xplained)e(in)h(precise)h (detail)0 1017 y(belo)n(w)-5 b(,)24 b(in)g(the)g("Representation)e (Error")h(section.)36 b(See)24 b Fq(The)g(P)-7 b(erils)25 b(of)f(Floating)f(P)-7 b(oint)23 b Fs(for)h(a)g(more)f(complete)g (account)g(of)g(other)0 1116 y(common)18 b(surprises.)0 1263 y(As)26 b(that)g(says)g(near)f(the)h(end,)g(\223there)f(are)h(no)f (easy)h(answers.)-6 b(\224)41 b(Still,)28 b(don')o(t)c(be)h(unduly)f(w) o(ary)h(of)h(\003oating-point!)38 b(The)26 b(errors)e(in)0 1363 y(Python)f(\003oat)h(operations)e(are)i(inherited)e(from)h(the)h (\003oating-point)d(hardw)o(are,)i(and)h(on)f(most)h(machines)f(are)h (on)f(the)h(order)e(of)i(no)0 1463 y(more)e(than)g(1)g(part)g(in)h (2**53)e(per)h(operation.)30 b(That')-5 b(s)22 b(more)g(than)g (adequate)f(for)h(most)h(tasks,)g(b)n(ut)g(you)e(do)h(need)g(to)h(k)o (eep)f(in)g(mind)0 1562 y(that)e(it')-5 b(s)22 b(not)d(decimal)h (arithmetic,)f(and)h(that)g(e)n(v)o(ery)f(\003oat)h(operation)f(can)h (suf)n(fer)f(a)i(ne)n(w)f(rounding)d(error)-5 b(.)0 1709 y(While)29 b(pathological)d(cases)i(do)g(e)o(xist,)i(for)d(most)h (casual)h(use)f(of)g(\003oating-point)d(arithmetic)j(you')o(ll)f(see)h (the)g(result)g(you)g(e)o(xpect)0 1809 y(in)d(the)g(end)f(if)h(you)f (simply)g(round)f(the)i(display)f(of)h(your)e(\002nal)i(results)g(to)g (the)g(number)e(of)h(decimal)h(digits)f(you)g(e)o(xpect.)38 b Fp(str\(\))0 1908 y Fs(usually)26 b(suf)n(\002ces,)j(and)d(for)g (\002ner)h(control)f(see)h(the)g(discussion)g(of)f(Python')-5 b(s)27 b Fp(\045)g Fs(format)f(operator:)37 b(the)27 b Fp(\045g)p Fs(,)h Fp(\045f)f Fs(and)g Fp(\045e)g Fs(format)0 2008 y(codes)20 b(supply)f(\003e)o(xible)h(and)f(easy)h(w)o(ays)h(to)f (round)f(\003oat)h(results)h(for)e(display)-5 b(.)0 2335 y Fl(B)n(.1)120 b(Representation)37 b(Error)0 2568 y Fs(This)21 b(section)g(e)o(xplains)f(the)h(\2230.1\224)f(e)o(xample)g (in)h(detail,)g(and)f(sho)n(ws)h(ho)n(w)g(you)f(can)h(perform)e(an)i(e) o(xact)f(analysis)h(of)g(cases)h(lik)o(e)f(this)0 2668 y(yourself.)j(Basic)d(f)o(amiliarity)e(with)i(binary)d (\003oating-point)g(representation)g(is)j(assumed.)0 2815 y Fq(Repr)m(esentation)i(err)l(or)j Fs(refers)e(to)h(the)g(f)o (act)g(that)g(some)f(\(most,)i(actually\))e(decimal)g(fractions)g (cannot)f(be)i(represented)e(e)o(xactly)h(as)0 2914 y(binary)c(\(base)g (2\))h(fractions.)26 b(This)21 b(is)h(the)f(chief)g(reason)f(why)g (Python)g(\(or)g(Perl,)h(C,)h(C)2523 2907 y(++)2617 2914 y(,)f(Ja)n(v)n(a,)h(F)o(ortran,)d(and)i(man)o(y)e(others\))h(often)0 3014 y(w)o(on')o(t)f(display)h(the)g(e)o(xact)g(decimal)f(number)g(you) g(e)o(xpect:)236 3252 y Fk(>>>)45 b(0.1)236 3343 y(0.10000000000000001) 0 3630 y Fs(Why)22 b(is)h(that?)31 b(1/10)22 b(is)h(not)f(e)o(xactly)f (representable)g(as)i(a)g(binary)d(fraction.)30 b(Almost)23 b(all)g(machines)e(today)g(\(No)o(v)o(ember)f(2000\))g(use)0 3729 y(IEEE-754)f(\003oating)j(point)f(arithmetic,)g(and)g(almost)h (all)h(platforms)d(map)i(Python)f(\003oats)h(to)g(IEEE-754)d("double)i (precision".)28 b(754)0 3829 y(doubles)19 b(contain)g(53)g(bits)h(of)g (precision,)e(so)j(on)e(input)g(the)h(computer)e(stri)n(v)o(es)i(to)g (con)m(v)o(ert)e(0.1)h(to)h(the)g(closest)g(fraction)f(it)h(can)g(of)g (the)0 3928 y(form)f Fq(J)t Fs(/2**)p Fq(N)25 b Fs(where)19 b Fq(J)25 b Fs(is)c(an)f(inte)o(ger)f(containing)f(e)o(xactly)i(53)f (bits.)26 b(Re)n(writing)281 4167 y Fk(1)45 b(/)f(10)h(~=)f(J)h(/)g (\(2)999 4180 y(**)1089 4167 y(N\))0 4435 y Fs(as)236 4673 y Fk(J)g(~=)f(2)505 4686 y(**)595 4673 y(N)h(/)f(10)0 4959 y Fs(and)20 b(recalling)f(that)h Fq(J)25 b Fs(has)20 b(e)o(xactly)f(53)h(bits)h(\(is)g Fp(>=)49 b(2)1597 4974 y(**)1697 4959 y(52)20 b Fs(b)n(ut)g Fp(<)50 b(2)2092 4974 y(**)2192 4959 y(53)p Fs(\),)19 b(the)h(best)h(v)n(alue)e(for)h Fq(N)26 b Fs(is)21 b(56:)p 0 5549 3901 4 v 0 5649 a Fo(B)n(.1.)53 b(Representation)24 b(Error)2839 b(99)p eop end %%Page: 100 108 TeXDict begin 100 107 bop 236 174 a Fk(>>>)45 b(2)461 187 y(**)551 174 y(52)236 266 y(4503599627370496L)236 357 y(>>>)g(2)461 370 y(**)551 357 y(53)236 448 y(9007199254740992L)236 540 y(>>>)g(2)461 553 y(**)551 540 y(56/10)236 631 y(7205759403792793L) 0 917 y Fs(That)20 b(is,)h(56)f(is)h(the)f(only)f(v)n(alue)h(for)f Fq(N)27 b Fs(that)20 b(lea)n(v)o(es)g Fq(J)25 b Fs(with)20 b(e)o(xactly)f(53)h(bits.)26 b(The)20 b(best)g(possible)g(v)n(alue)g (for)f Fq(J)25 b Fs(is)c(then)f(that)g(quotient)0 1017 y(rounded:)236 1255 y Fk(>>>)45 b(q,)f(r)h(=)f(divmod\(2)1089 1268 y(**)1179 1255 y(56,)f(10\))236 1346 y(>>>)i(r)236 1438 y(6L)0 1724 y Fs(Since)20 b(the)g(remainder)f(is)i(more)e(than)h (half)g(of)g(10,)f(the)h(best)h(approximation)c(is)k(obtained)e(by)h (rounding)d(up:)236 1962 y Fk(>>>)45 b(q+1)236 2053 y (7205759403792794L)0 2339 y Fs(Therefore)18 b(the)i(best)h(possible)f (approximation)d(to)j(1/10)f(in)i(754)e(double)g(precision)g(is)i(that) f(o)o(v)o(er)f(2**56,)f(or)236 2578 y Fk(7205759403792794)42 b(/)j(72057594037927936)0 2864 y Fs(Note)18 b(that)f(since)h(we)g (rounded)d(up,)j(this)g(is)h(actually)e(a)h(little)g(bit)g(lar)o(ger)e (than)h(1/10;)h(if)g(we)g(had)f(not)h(rounded)d(up,)i(the)h(quotient)e (w)o(ould)0 2963 y(ha)n(v)o(e)k(been)f(a)i(little)g(bit)f(smaller)g (than)g(1/10.)k(But)d(in)f(no)g(case)h(can)f(it)h(be)f Fq(e)n(xactly)g Fs(1/10!)0 3110 y(So)i(the)g(computer)e(ne)n(v)o(er)g (\223sees\224)i(1/10:)28 b(what)22 b(it)g(sees)h(is)f(the)g(e)o(xact)g (fraction)e(gi)n(v)o(en)h(abo)o(v)o(e,)f(the)i(best)g(754)e(double)h (approximation)0 3210 y(it)g(can)f(get:)236 3448 y Fk(>>>)45 b(.1)550 3461 y(*)640 3448 y(2)685 3461 y(**)775 3448 y(56)236 3540 y(7205759403792794.0)0 3826 y Fs(If)20 b(we)h(multiply)e(that)h(fraction)f(by)h(10**30,)d(we)k(can)f(see)h (the)f(\(truncated\))e(v)n(alue)h(of)h(its)i(30)d(most)i(signi\002cant) e(decimal)h(digits:)236 4064 y Fk(>>>)45 b(7205759403792794)1178 4077 y(*)1267 4064 y(10)1357 4077 y(**)1447 4064 y(30)f(/)h(2)1716 4077 y(**)1806 4064 y(56)236 4155 y(100000000000000005551115123125L)0 4441 y Fs(meaning)62 b(that)h(the)g(e)o(xact)g(number)e(stored)i(in)g (the)h(computer)d(is)j(approximately)c(equal)j(to)g(the)h(decimal)e(v)n (alue)0 4541 y(0.100000000000)o(00)o(00)o(05)o(55)o(111)o(51)o(23)o(12) o(5.)35 b(Rounding)23 b(that)j(to)g(17)f(signi\002cant)g(digits)g(gi)n (v)o(es)g(the)h(0.10000000000)o(00)o(00)o(01)19 b(that)0 4641 y(Python)i(displays)h(\(well,)h(will)g(display)f(on)g(an)o(y)g (754-conforming)17 b(platform)k(that)h(does)h(best-possible)e(input)h (and)g(output)f(con)m(v)o(er)n(-)0 4740 y(sions)f(in)h(its)g(C)g (library)e(\227)i(yours)e(may)h(not!\).)p 0 5549 3901 4 v 0 5649 a Fo(100)1500 b(Appendix)24 b(B)n(.)47 b(Floating)25 b(P)l(oint)f(Ar)q(ithmetic:)30 b(Issues)22 b(and)h(Limitations)p eop end %%Page: 101 109 TeXDict begin 101 108 bop 0 83 3901 9 v 3480 230 a Fo(APPENDIX)3814 427 y Fn(C)p 0 515 V 2080 978 a Fy(Histor)6 b(y)58 b(and)f(License)0 1506 y Fl(C)l(.1)121 b(Histor)t(y)33 b(of)h(the)g(softw)n(are)0 1739 y Fs(Python)39 b(w)o(as)i(created)f(in)g(the)h(early)e(1990s)h(by) g(Guido)f(v)n(an)h(Rossum)g(at)h(Stichting)f(Mathematisch)f(Centrum)g (\(CWI,)i(see)0 1839 y Ft(http://www)l(.cwi.nl/)l Fs(\))30 b(in)g(the)g(Netherlands)e(as)j(a)f(successor)g(of)f(a)i(language)d (called)i(ABC.)g(Guido)f(remains)h(Python')-5 b(s)29 b(principal)0 1939 y(author)m(,)18 b(although)h(it)i(includes)e(man)o (y)g(contrib)n(utions)f(from)h(others.)0 2085 y(In)36 b(1995,)j(Guido)c(continued)f(his)j(w)o(ork)e(on)h(Python)f(at)i(the)f (Corporation)e(for)i(National)g(Research)g(Initiati)n(v)o(es)f(\(CNRI,) i(see)0 2185 y Ft(http://www)l(.cnr)q(i.reston.v)n(a.us/)-6 b Fs(\))21 b(in)f(Reston,)g(V)-5 b(ir)o(ginia)19 b(where)h(he)g (released)g(se)n(v)o(eral)f(v)o(ersions)g(of)h(the)g(softw)o(are.)0 2332 y(In)h(May)h(2000,)e(Guido)h(and)g(the)h(Python)e(core)h(de)n(v)o (elopment)e(team)j(mo)o(v)o(ed)d(to)j(BeOpen.com)e(to)i(form)e(the)i (BeOpen)f(PythonLabs)0 2432 y(team.)49 b(In)27 b(October)g(of)h(the)g (same)h(year)m(,)g(the)f(PythonLabs)e(team)i(mo)o(v)o(ed)e(to)i (Digital)h(Creations)f(\(no)n(w)f(Zope)g(Corporation;)j(see)0 2531 y Ft(http://www)l(.z)o(ope)o(.com/)l Fs(\).)g(In)21 b(2001,)g(the)h(Python)f(Softw)o(are)g(F)o(oundation)f(\(PSF)-7 b(,)22 b(see)h Ft(http://www)l(.p)n(ython.org/)o(psf/)-6 b Fs(\))22 b(w)o(as)h(formed,)d(a)0 2631 y(non-pro\002t)h(or)o (ganization)f(created)i(speci\002cally)h(to)g(o)n(wn)g(Python-related)d (Intellectual)j(Property)-5 b(.)32 b(Zope)22 b(Corporation)f(is)j(a)g (spon-)0 2730 y(soring)19 b(member)g(of)h(the)g(PSF)-7 b(.)0 2877 y(All)26 b(Python)d(releases)j(are)e(Open)h(Source)f(\(see)h Ft(http://www)l(.opensource)o(.o)o(rg/)19 b Fs(for)24 b(the)h(Open)g(Source)f(De\002nition\).)38 b(Historically)-5 b(,)0 2977 y(most,)20 b(b)n(ut)g(not)g(all,)h(Python)e(releases)h(ha)n (v)o(e)g(also)g(been)g(GPL-compatible;)e(the)i(table)g(belo)n(w)g (summarizes)f(the)h(v)n(arious)g(releases.)p 0 5549 3901 4 v 3762 5649 a Fo(101)p eop end %%Page: 102 110 TeXDict begin 102 109 bop 670 70 a Fr(Release)p 1074 100 4 100 v 188 w(Deri)o(v)o(ed)20 b(fr)o(om)p 1648 100 V 195 w(Y)-9 b(ear)p 2107 100 V 291 w(Owner)p 2643 100 V 195 w(GPL)21 b(compatible?)p 531 103 2838 4 v 581 173 a Fs(0.9.0)e(thru)g(1.2)p 1074 203 4 100 v 285 w(n/a)p 1648 203 V 285 w(1991-1995)p 2107 203 V 233 w(CWI)p 2643 203 V 494 w(yes)581 272 y(1.3)g(thru)h(1.5.2)p 1074 302 V 283 w(1.2)p 1648 302 V 283 w(1995-1999)p 2107 302 V 214 w(CNRI)p 2643 302 V 476 w(yes)752 372 y(1.6)p 1074 402 V 423 w(1.5.2)p 1648 402 V 348 w(2000)p 2107 402 V 313 w(CNRI)p 2643 402 V 490 w(no)752 472 y(2.0)p 1074 501 V 454 w(1.6)p 1648 501 V 380 w(2000)p 2107 501 V 195 w(BeOpen.com)p 2643 501 V 368 w(no)720 571 y(1.6.1)p 1074 601 V 423 w(1.6)p 1648 601 V 380 w(2001)p 2107 601 V 313 w(CNRI)p 2643 601 V 490 w(no)752 671 y(2.1)p 1074 701 V 347 w(2.0+1.6.1)p 1648 701 V 272 w(2001)p 2107 701 V 343 w(PSF)p 2643 701 V 520 w(no)720 770 y(2.0.1)p 1074 800 V 316 w(2.0+1.6.1)p 1648 800 V 272 w(2001)p 2107 800 V 343 w(PSF)p 2643 800 V 506 w(yes)720 870 y(2.1.1)p 1074 900 V 316 w(2.1+2.0.1)p 1648 900 V 272 w(2001)p 2107 900 V 343 w(PSF)p 2643 900 V 506 w(yes)752 970 y(2.2)p 1074 1000 V 423 w(2.1.1)p 1648 1000 V 348 w(2001)p 2107 1000 V 343 w(PSF)p 2643 1000 V 506 w(yes)720 1069 y(2.1.2)p 1074 1099 V 392 w(2.1.1)p 1648 1099 V 348 w(2002)p 2107 1099 V 343 w(PSF)p 2643 1099 V 506 w(yes)720 1169 y(2.1.3)p 1074 1199 V 392 w(2.1.2)p 1648 1199 V 348 w(2002)p 2107 1199 V 343 w(PSF)p 2643 1199 V 506 w(yes)720 1269 y(2.2.1)p 1074 1298 V 423 w(2.2)p 1648 1298 V 380 w(2002)p 2107 1298 V 343 w(PSF)p 2643 1298 V 506 w(yes)720 1368 y(2.2.2)p 1074 1398 V 392 w(2.2.1)p 1648 1398 V 348 w(2002)p 2107 1398 V 343 w(PSF)p 2643 1398 V 506 w(yes)720 1468 y(2.2.3)p 1074 1498 V 392 w(2.2.2)p 1648 1498 V 251 w(2002-2003)p 2107 1498 V 244 w(PSF)p 2643 1498 V 506 w(yes)752 1567 y(2.3)p 1074 1597 V 423 w(2.2.2)p 1648 1597 V 251 w(2002-2003)p 2107 1597 V 244 w(PSF)p 2643 1597 V 506 w(yes)720 1667 y(2.3.1)p 1074 1697 V 423 w(2.3)p 1648 1697 V 283 w(2002-2003)p 2107 1697 V 244 w(PSF)p 2643 1697 V 506 w(yes)720 1767 y(2.3.2)p 1074 1797 V 392 w(2.3.1)p 1648 1797 V 348 w(2003)p 2107 1797 V 343 w(PSF)p 2643 1797 V 506 w(yes)720 1866 y(2.3.3)p 1074 1896 V 392 w(2.3.2)p 1648 1896 V 348 w(2003)p 2107 1896 V 343 w(PSF)p 2643 1896 V 506 w(yes)720 1966 y(2.3.4)p 1074 1996 V 392 w(2.3.3)p 1648 1996 V 348 w(2004)p 2107 1996 V 343 w(PSF)p 2643 1996 V 506 w(yes)720 2066 y(2.3.5)p 1074 2095 V 392 w(2.3.4)p 1648 2095 V 348 w(2005)p 2107 2095 V 343 w(PSF)p 2643 2095 V 506 w(yes)752 2165 y(2.4)p 1074 2195 V 454 w(2.3)p 1648 2195 V 380 w(2004)p 2107 2195 V 343 w(PSF)p 2643 2195 V 506 w(yes)720 2265 y(2.4.1)p 1074 2295 V 423 w(2.4)p 1648 2295 V 380 w(2005)p 2107 2295 V 343 w(PSF)p 2643 2295 V 506 w(yes)720 2364 y(2.4.2)p 1074 2394 V 392 w(2.4.1)p 1648 2394 V 348 w(2005)p 2107 2394 V 343 w(PSF)p 2643 2394 V 506 w(yes)720 2464 y(2.4.3)p 1074 2494 V 392 w(2.4.2)p 1648 2494 V 348 w(2006)p 2107 2494 V 343 w(PSF)p 2643 2494 V 506 w(yes)720 2564 y(2.4.4)p 1074 2594 V 392 w(2.4.3)p 1648 2594 V 348 w(2006)p 2107 2594 V 343 w(PSF)p 2643 2594 V 506 w(yes)752 2663 y(2.5)p 1074 2693 V 454 w(2.4)p 1648 2693 V 380 w(2006)p 2107 2693 V 343 w(PSF)p 2643 2693 V 506 w(yes)720 2763 y(2.5.1)p 1074 2793 V 423 w(2.5)p 1648 2793 V 380 w(2007)p 2107 2793 V 343 w(PSF)p 2643 2793 V 506 w(yes)720 2863 y(2.5.2)p 1074 2892 V 392 w(2.5.1)p 1648 2892 V 348 w(2008)p 2107 2892 V 343 w(PSF)p 2643 2892 V 506 w(yes)0 3088 y Fr(Note:)35 b Fs(GPL-compatible)24 b(doesn')o(t)g(mean)g(that)i(we')l (re)f(distrib)n(uting)f(Python)h(under)f(the)h(GPL.)h(All)g(Python)f (licenses,)i(unlik)o(e)e(the)0 3188 y(GPL,)e(let)g(you)f(distrib)n(ute) h(a)g(modi\002ed)f(v)o(ersion)f(without)h(making)g(your)f(changes)h (open)g(source.)32 b(The)22 b(GPL-compatible)f(licenses)0 3288 y(mak)o(e)f(it)h(possible)f(to)g(combine)f(Python)g(with)h(other)f (softw)o(are)h(that)g(is)i(released)d(under)g(the)h(GPL;)h(the)f (others)g(don')o(t.)0 3434 y(Thanks)f(to)i(the)f(man)o(y)f(outside)g(v) n(olunteers)g(who)h(ha)n(v)o(e)g(w)o(ork)o(ed)f(under)f(Guido')-5 b(s)20 b(direction)f(to)h(mak)o(e)g(these)g(releases)h(possible.)0 3762 y Fl(C)l(.2)121 b(T)-14 b(er)s(ms)33 b(and)h(conditions)h(f)l(or)f (accessing)g(or)f(otherwise)i(using)f(Python)1024 3947 y Fr(PSF)20 b(LICENSE)j(A)-5 b(GREEMENT)22 b(FOR)e(PYTHON)g(2.5.3)104 4094 y Fs(1.)41 b(This)22 b(LICENSE)g(A)m(GREEMENT)f(is)j(between)d (the)h(Python)f(Softw)o(are)h(F)o(oundation)e(\(\223PSF\224\),)i(and)f (the)i(Indi)n(vidual)c(or)j(Or)n(-)208 4194 y(ganization)g (\(\223Licensee\224\))h(accessing)h(and)g(otherwise)f(using)h(Python)f (2.5.3)g(softw)o(are)h(in)h(source)e(or)h(binary)f(form)h(and)g(its)208 4293 y(associated)c(documentation.)104 4460 y(2.)41 b(Subject)28 b(to)g(the)h(terms)f(and)g(conditions)f(of)h(this)h(License)f (Agreement,)h(PSF)g(hereby)e(grants)h(Licensee)g(a)h(none)o(xclusi)n(v) o(e,)208 4559 y(ro)o(yalty-free,)e(w)o(orld-wide)g(license)h(to)g (reproduce,)f(analyze,)i(test,)i(perform)26 b(and/or)h(display)g (publicly)-5 b(,)28 b(prepare)f(deri)n(v)n(a-)208 4659 y(ti)n(v)o(e)g(w)o(orks,)h(distrib)n(ute,)h(and)e(otherwise)g(use)h (Python)e(2.5.3)g(alone)h(or)g(in)h(an)o(y)e(deri)n(v)n(ati)n(v)o(e)g (v)o(ersion,)i(pro)o(vided,)e(ho)n(we)n(v)o(er)m(,)208 4758 y(that)f(PSF')-5 b(s)28 b(License)d(Agreement)f(and)h(PSF')-5 b(s)28 b(notice)d(of)g(cop)o(yright,)g(i.e.,)i(\223Cop)o(yright)d(\251) i(2001-2008)d(Python)h(Softw)o(are)208 4858 y(F)o(oundation;)h(All)h (Rights)g(Reserv)o(ed\224)e(are)i(retained)e(in)h(Python)f(2.5.3)g (alone)h(or)g(in)h(an)o(y)e(deri)n(v)n(ati)n(v)o(e)g(v)o(ersion)g (prepared)f(by)208 4958 y(Licensee.)104 5124 y(3.)41 b(In)16 b(the)h(e)n(v)o(ent)f(Licensee)h(prepares)e(a)j(deri)n(v)n(ati) n(v)o(e)d(w)o(ork)h(that)h(is)h(based)e(on)h(or)g(incorporates)d (Python)i(2.5.3)g(or)g(an)o(y)h(part)f(thereof,)208 5223 y(and)25 b(w)o(ants)h(to)f(mak)o(e)g(the)h(deri)n(v)n(ati)n(v)o(e)d(w)o (ork)i(a)n(v)n(ailable)g(to)h(others)f(as)h(pro)o(vided)d(herein,)j (then)f(Licensee)g(hereby)f(agrees)h(to)208 5323 y(include)19 b(in)h(an)o(y)f(such)h(w)o(ork)g(a)h(brief)e(summary)g(of)h(the)g (changes)f(made)h(to)g(Python)f(2.5.3.)p 0 5549 3901 4 v 0 5649 a Fo(102)2535 b(Appendix)24 b(C)n(.)46 b(Histor)r(y)22 b(and)i(License)p eop end %%Page: 103 111 TeXDict begin 103 110 bop 104 83 a Fs(4.)41 b(PSF)30 b(is)g(making)e(Python)h(2.5.3)f(a)n(v)n(ailable)h(to)g(Licensee)g(on)g (an)g(\223)-7 b(AS)31 b(IS\224)e(basis.)53 b(PSF)31 b(MAKES)e(NO)h (REPRESENT)-8 b(A-)208 183 y(TIONS)25 b(OR)h(W)-10 b(ARRANTIES,)26 b(EXPRESS)g(OR)g(IMPLIED.)f(BY)h(W)-10 b(A)h(Y)26 b(OF)g(EXAMPLE,)f(B)o (UT)h(NO)m(T)f(LIMIT)-8 b(A)f(TION,)208 282 y(PSF)26 b(MAKES)f(NO)h(AND)g(DISCLAIMS)f(ANY)h(REPRESENT)-8 b(A)f(TION)25 b(OR)h(W)-10 b(ARRANTY)27 b(OF)f(MERCHANT)-8 b(ABIL-)208 382 y(ITY)17 b(OR)h(FITNESS)f(FOR)i(ANY)e(P)-8 b(AR)j(TICULAR)19 b(PURPOSE)f(OR)g(THA)-9 b(T)17 b(THE)g(USE)h(OF)g(PYTHON)f(2.5.3)f (WILL)i(NO)m(T)208 482 y(INFRINGE)i(ANY)g(THIRD)h(P)-8 b(AR)j(TY)21 b(RIGHTS.)104 648 y(5.)41 b(PSF)29 b(SHALL)g(NO)m(T)g(BE)g (LIABLE)g(T)o(O)f(LICENSEE)g(OR)i(ANY)f(O)m(THER)g(USERS)h(OF)f(PYTHON) g(2.5.3)e(FOR)j(ANY)208 747 y(INCIDENT)-8 b(AL,)18 b(SPECIAL,)h(OR)h (CONSEQ)o(UENTIAL)f(D)m(AMA)m(GES)f(OR)i(LOSS)g(AS)g(A)g(RESUL)-8 b(T)19 b(OF)h(MODIFYING,)208 847 y(DISTRIB)o(UTING,)j(OR)h(O)m(THER)-5 b(WISE)24 b(USING)g(PYTHON)g(2.5.3,)f(OR)h(ANY)g(DERIV)-11 b(A)i(TIVE)23 b(THEREOF)-7 b(,)24 b(EVEN)f(IF)208 946 y(AD)m(VISED)c(OF)i(THE)f(POSSIBILITY)g(THEREOF)-7 b(.)104 1112 y(6.)41 b(This)20 b(License)g(Agreement)e(will)j(automatically)e (terminate)g(upon)g(a)i(material)f(breach)f(of)h(its)h(terms)f(and)g (conditions.)104 1279 y(7.)41 b(Nothing)23 b(in)i(this)g(License)g (Agreement)e(shall)i(be)g(deemed)e(to)i(create)g(an)o(y)f(relationship) f(of)i(agenc)o(y)-5 b(,)23 b(partnership,)h(or)h(joint)208 1378 y(v)o(enture)20 b(between)h(PSF)h(and)g(Licensee.)29 b(This)22 b(License)f(Agreement)g(does)g(not)h(grant)f(permission)f(to) i(use)g(PSF)h(trademarks)208 1478 y(or)c(trade)h(name)g(in)g(a)h (trademark)d(sense)j(to)f(endorse)f(or)h(promote)e(products)h(or)h (services)g(of)g(Licensee,)g(or)f(an)o(y)h(third)f(party)-5 b(.)104 1644 y(8.)41 b(By)18 b(cop)o(ying,)e(installing)h(or)h (otherwise)f(using)g(Python)g(2.5.3,)g(Licensee)g(agrees)g(to)h(be)g (bound)e(by)h(the)h(terms)g(and)f(conditions)208 1743 y(of)i(this)i(License)f(Agreement.)846 1926 y Fr(BEOPEN.COM)h(LICENSE)h (A)-5 b(GREEMENT)23 b(FOR)d(PYTHON)g(2.0)579 2026 y(BEOPEN)h(PYTHON)f (OPEN)g(SOURCE)h(LICENSE)h(A)-5 b(GREEMENT)23 b(VERSION)e(1)104 2256 y Fs(1.)41 b(This)20 b(LICENSE)g(A)m(GREEMENT)f(is)i(between)f (BeOpen.com)e(\(\223BeOpen\224\),)h(ha)n(ving)g(an)h(of)n(\002ce)g(at)g (160)g(Saratoga)e(A)-6 b(v)o(enue,)208 2355 y(Santa)29 b(Clara,)i(CA)f(95051,)f(and)f(the)h(Indi)n(vidual)e(or)i(Or)o (ganization)d(\(\223Licensee\224\))h(accessing)i(and)f(otherwise)h (using)f(this)208 2455 y(softw)o(are)19 b(in)i(source)e(or)h(binary)f (form)g(and)h(its)h(associated)f(documentation)d(\(\223the)j(Softw)o (are\224\).)104 2621 y(2.)41 b(Subject)18 b(to)g(the)h(terms)g(and)f (conditions)f(of)h(this)h(BeOpen)f(Python)f(License)i(Agreement,)e (BeOpen)h(hereby)f(grants)h(Licensee)208 2721 y(a)25 b(non-e)o(xclusi)n(v)o(e,)e(ro)o(yalty-free,)h(w)o(orld-wide)f(license) j(to)f(reproduce,)f(analyze,)h(test,)i(perform)c(and/or)h(display)g (publicly)-5 b(,)208 2820 y(prepare)16 b(deri)n(v)n(ati)n(v)o(e)f(w)o (orks,)j(distrib)n(ute,)f(and)g(otherwise)g(use)h(the)g(Softw)o(are)f (alone)g(or)g(in)h(an)o(y)f(deri)n(v)n(ati)n(v)o(e)f(v)o(ersion,)g(pro) o(vided,)208 2920 y(ho)n(we)n(v)o(er)m(,)f(that)i(the)g(BeOpen)g (Python)f(License)i(is)g(retained)e(in)h(the)h(Softw)o(are,)f(alone)f (or)h(in)h(an)o(y)e(deri)n(v)n(ati)n(v)o(e)g(v)o(ersion)g(prepared)208 3019 y(by)j(Licensee.)104 3185 y(3.)41 b(BeOpen)25 b(is)j(making)c(the) j(Softw)o(are)e(a)n(v)n(ailable)h(to)g(Licensee)g(on)g(an)g(\223)-7 b(AS)27 b(IS\224)g(basis.)43 b(BEOPEN)27 b(MAKES)f(NO)h(REPRE-)208 3285 y(SENT)-8 b(A)f(TIONS)27 b(OR)i(W)-10 b(ARRANTIES,)29 b(EXPRESS)g(OR)g(IMPLIED.)f(BY)h(W)-10 b(A)h(Y)29 b(OF)g(EXAMPLE,)e(B)o (UT)i(NO)m(T)g(LIMI-)208 3385 y(T)-8 b(A)f(TION,)20 b(BEOPEN)h(MAKES)f (NO)i(AND)f(DISCLAIMS)f(ANY)h(REPRESENT)-8 b(A)f(TION)21 b(OR)g(W)-10 b(ARRANTY)22 b(OF)f(MER-)208 3484 y(CHANT)-8 b(ABILITY)21 b(OR)i(FITNESS)e(FOR)i(ANY)f(P)-8 b(AR)j(TICULAR)23 b(PURPOSE)g(OR)f(THA)-9 b(T)21 b(THE)h(USE)g(OF)g(THE)g(SOFT)-8 b(-)208 3584 y(W)e(ARE)21 b(WILL)f(NO)m(T)g(INFRINGE)g(ANY)h(THIRD)f(P) -8 b(AR)j(TY)21 b(RIGHTS.)104 3750 y(4.)41 b(BEOPEN)24 b(SHALL)f(NO)m(T)h(BE)g(LIABLE)g(T)o(O)f(LICENSEE)g(OR)i(ANY)f(O)m (THER)f(USERS)i(OF)f(THE)g(SOFTW)-10 b(ARE)25 b(FOR)208 3850 y(ANY)c(INCIDENT)-8 b(AL,)20 b(SPECIAL,)h(OR)h(CONSEQ)o(UENTIAL)e (D)m(AMA)m(GES)h(OR)h(LOSS)f(AS)h(A)f(RESUL)-8 b(T)22 b(OF)g(USING,)208 3949 y(MODIFYING)j(OR)i(DISTRIB)o(UTING)f(THE)h (SOFTW)-10 b(ARE,)27 b(OR)g(ANY)f(DERIV)-11 b(A)i(TIVE)25 b(THEREOF)-7 b(,)27 b(EVEN)f(IF)g(AD-)208 4049 y(VISED)20 b(OF)h(THE)f(POSSIBILITY)g(THEREOF)-7 b(.)104 4215 y(5.)41 b(This)20 b(License)g(Agreement)e(will)j(automatically)e(terminate)g (upon)g(a)i(material)f(breach)f(of)h(its)h(terms)f(and)g(conditions.) 104 4381 y(6.)41 b(This)27 b(License)f(Agreement)g(shall)h(be)g(go)o(v) o(erned)d(by)j(and)f(interpreted)f(in)j(all)f(respects)g(by)g(the)g(la) o(w)g(of)g(the)g(State)g(of)g(Cali-)208 4481 y(fornia,)22 b(e)o(xcluding)e(con\003ict)j(of)f(la)o(w)i(pro)o(visions.)31 b(Nothing)21 b(in)j(this)f(License)g(Agreement)e(shall)i(be)g(deemed)f (to)h(create)f(an)o(y)208 4580 y(relationship)i(of)h(agenc)o(y)-5 b(,)25 b(partnership,)g(or)g(joint)h(v)o(enture)e(between)h(BeOpen)g (and)g(Licensee.)41 b(This)26 b(License)f(Agreement)208 4680 y(does)18 b(not)h(grant)f(permission)f(to)i(use)h(BeOpen)e (trademarks)f(or)i(trade)f(names)h(in)g(a)g(trademark)e(sense)i(to)g (endorse)f(or)g(promote)208 4779 y(products)k(or)h(services)g(of)h (Licensee,)f(or)h(an)o(y)e(third)h(party)-5 b(.)34 b(As)24 b(an)g(e)o(xception,)e(the)i(\223BeOpen)f(Python\224)f(logos)h(a)n(v)n (ailable)g(at)208 4879 y(http://www)-5 b(.p)o(ythonlabs.com/logo)o (s.htm)o(l)15 b(may)20 b(be)g(used)g(according)e(to)i(the)h (permissions)e(granted)g(on)h(that)g(web)g(page.)104 5045 y(7.)41 b(By)19 b(cop)o(ying,)e(installing)h(or)g(otherwise)g (using)h(the)f(softw)o(are,)h(Licensee)f(agrees)g(to)h(be)f(bound)f(by) i(the)f(terms)h(and)f(conditions)208 5145 y(of)h(this)i(License)f (Agreement.)992 5327 y Fr(CNRI)h(LICENSE)h(A)-5 b(GREEMENT)22 b(FOR)e(PYTHON)g(1.6.1)p 0 5549 3901 4 v 0 5649 a Fo(C)n(.2.)51 b(T)-10 b(er)r(ms)23 b(and)g(conditions)h(f)n(or)f(accessing)f(or)h (otherwise)g(using)h(Python)1287 b(103)p eop end %%Page: 104 112 TeXDict begin 104 111 bop 104 83 a Fs(1.)41 b(This)25 b(LICENSE)g(A)m(GREEMENT)f(is)i(between)e(the)h(Corporation)e(for)h (National)h(Research)g(Initiati)n(v)o(es,)g(ha)n(ving)f(an)h(of)n (\002ce)208 183 y(at)j(1895)f(Preston)h(White)h(Dri)n(v)o(e,)g(Reston,) h(V)-11 b(A)29 b(20191)d(\(\223CNRI\224\),)i(and)f(the)i(Indi)n(vidual) d(or)i(Or)o(ganization)d(\(\223Licensee\224\))208 282 y(accessing)18 b(and)g(otherwise)g(using)g(Python)f(1.6.1)h(softw)o (are)g(in)h(source)e(or)i(binary)e(form)h(and)g(its)h(associated)g (documentation.)104 443 y(2.)41 b(Subject)24 b(to)g(the)h(terms)f(and)g (conditions)f(of)h(this)h(License)g(Agreement,)e(CNRI)j(hereby)d (grants)h(Licensee)g(a)h(none)o(xclusi)n(v)o(e,)208 542 y(ro)o(yalty-free,)18 b(w)o(orld-wide)i(license)h(to)g(reproduce,)d (analyze,)j(test,)g(perform)e(and/or)h(display)h(publicly)-5 b(,)19 b(prepare)g(deri)n(v)n(ati)n(v)o(e)208 642 y(w)o(orks,)28 b(distrib)n(ute,)g(and)f(otherwise)f(use)i(Python)e(1.6.1)f(alone)i(or) g(in)g(an)o(y)f(deri)n(v)n(ati)n(v)o(e)g(v)o(ersion,)h(pro)o(vided,)f (ho)n(we)n(v)o(er)m(,)g(that)208 741 y(CNRI')-5 b(s)32 b(License)e(Agreement)f(and)h(CNRI')-5 b(s)32 b(notice)f(of)f(cop)o (yright,)h(i.e.,)i(\223Cop)o(yright)c(\251)j(1995-2001)27 b(Corporation)h(for)208 841 y(National)j(Research)h(Initiati)n(v)o(es;) 37 b(All)32 b(Rights)g(Reserv)o(ed\224)f(are)h(retained)f(in)h(Python)e (1.6.1)h(alone)g(or)g(in)h(an)o(y)f(deri)n(v)n(ati)n(v)o(e)208 941 y(v)o(ersion)22 b(prepared)g(by)h(Licensee.)35 b(Alternately)-5 b(,)23 b(in)h(lieu)g(of)f(CNRI')-5 b(s)25 b(License)f(Agreement,)e (Licensee)i(may)f(substitute)h(the)208 1040 y(follo)n(wing)j(te)o(xt)i (\(omitting)e(the)i(quotes\):)42 b(\223Python)27 b(1.6.1)h(is)h(made)g (a)n(v)n(ailable)f(subject)h(to)g(the)g(terms)g(and)f(conditions)f(in) 208 1140 y(CNRI')-5 b(s)24 b(License)f(Agreement.)33 b(This)24 b(Agreement)d(together)h(with)i(Python)e(1.6.1)g(may)h(be)g (located)g(on)g(the)g(Internet)f(using)208 1240 y(the)27 b(follo)n(wing)f(unique,)h(persistent)g(identi\002er)g(\(kno)n(wn)f(as) i(a)f(handle\):)39 b(1895.22/1013.)j(This)27 b(Agreement)f(may)h(also)h (be)208 1339 y(obtained)18 b(from)h(a)i(proxy)d(serv)o(er)i(on)g(the)g (Internet)f(using)g(the)i(follo)n(wing)d(URL:)j Ft(http://hdl.handle)o (.)o(ne)o(t/18)o(95)o(.2)o(2/1)o(01)o(3)-6 b Fs(.)g(\224)104 1499 y(3.)41 b(In)16 b(the)h(e)n(v)o(ent)f(Licensee)h(prepares)e(a)j (deri)n(v)n(ati)n(v)o(e)d(w)o(ork)h(that)h(is)h(based)e(on)h(or)g (incorporates)d(Python)i(1.6.1)g(or)g(an)o(y)h(part)f(thereof,)208 1599 y(and)25 b(w)o(ants)h(to)f(mak)o(e)g(the)h(deri)n(v)n(ati)n(v)o(e) d(w)o(ork)i(a)n(v)n(ailable)g(to)h(others)f(as)h(pro)o(vided)d(herein,) j(then)f(Licensee)g(hereby)f(agrees)h(to)208 1699 y(include)19 b(in)h(an)o(y)f(such)h(w)o(ork)g(a)h(brief)e(summary)g(of)h(the)g (changes)f(made)h(to)g(Python)f(1.6.1.)104 1859 y(4.)41 b(CNRI)23 b(is)h(making)d(Python)h(1.6.1)f(a)n(v)n(ailable)h(to)h (Licensee)g(on)f(an)h(\223)-7 b(AS)23 b(IS\224)g(basis.)34 b(CNRI)23 b(MAKES)g(NO)g(REPRESENT)-8 b(A-)208 1959 y(TIONS)25 b(OR)h(W)-10 b(ARRANTIES,)26 b(EXPRESS)g(OR)g(IMPLIED.)f(BY)h(W)-10 b(A)h(Y)26 b(OF)g(EXAMPLE,)f(B)o(UT)h(NO)m(T)f(LIMIT)-8 b(A)f(TION,)208 2058 y(CNRI)20 b(MAKES)f(NO)h(AND)g(DISCLAIMS)f(ANY)h (REPRESENT)-8 b(A)f(TION)19 b(OR)h(W)-10 b(ARRANTY)21 b(OF)f(MERCHANT)-8 b(ABIL-)208 2158 y(ITY)17 b(OR)h(FITNESS)f(FOR)i (ANY)e(P)-8 b(AR)j(TICULAR)19 b(PURPOSE)f(OR)g(THA)-9 b(T)17 b(THE)g(USE)h(OF)g(PYTHON)f(1.6.1)f(WILL)i(NO)m(T)208 2257 y(INFRINGE)i(ANY)g(THIRD)h(P)-8 b(AR)j(TY)21 b(RIGHTS.)104 2418 y(5.)41 b(CNRI)25 b(SHALL)g(NO)m(T)g(BE)g(LIABLE)g(T)o(O)f (LICENSEE)g(OR)i(ANY)f(O)m(THER)g(USERS)h(OF)f(PYTHON)g(1.6.1)e(FOR)j (ANY)208 2517 y(INCIDENT)-8 b(AL,)18 b(SPECIAL,)h(OR)h(CONSEQ)o (UENTIAL)f(D)m(AMA)m(GES)f(OR)i(LOSS)g(AS)g(A)g(RESUL)-8 b(T)19 b(OF)h(MODIFYING,)208 2617 y(DISTRIB)o(UTING,)j(OR)h(O)m(THER)-5 b(WISE)24 b(USING)g(PYTHON)g(1.6.1,)f(OR)h(ANY)g(DERIV)-11 b(A)i(TIVE)23 b(THEREOF)-7 b(,)24 b(EVEN)f(IF)208 2717 y(AD)m(VISED)c(OF)i(THE)f(POSSIBILITY)g(THEREOF)-7 b(.)104 2877 y(6.)41 b(This)20 b(License)g(Agreement)e(will)j(automatically)e (terminate)g(upon)g(a)i(material)f(breach)f(of)h(its)h(terms)f(and)g (conditions.)104 3037 y(7.)41 b(This)16 b(License)f(Agreement)f(shall)j (be)e(go)o(v)o(erned)e(by)j(the)f(federal)g(intellectual)h(property)d (la)o(w)j(of)g(the)g(United)f(States,)i(including)208 3137 y(without)28 b(limitation)g(the)g(federal)g(cop)o(yright)f(la)o(w) -5 b(,)30 b(and,)g(to)f(the)g(e)o(xtent)e(such)i(U.S.)g(federal)e(la)o (w)i(does)g(not)f(apply)-5 b(,)29 b(by)g(the)208 3236 y(la)o(w)f(of)h(the)f(Commonwealth)f(of)h(V)-5 b(ir)o(ginia,)30 b(e)o(xcluding)c(V)-5 b(ir)o(ginia')g(s)28 b(con\003ict)g(of)g(la)o(w)h (pro)o(visions.)49 b(Notwithstanding)27 b(the)208 3336 y(fore)o(going,)c(with)i(re)o(gard)f(to)h(deri)n(v)n(ati)n(v)o(e)e(w)o (orks)i(based)g(on)g(Python)f(1.6.1)g(that)h(incorporate)e (non-separable)g(material)h(that)208 3435 y(w)o(as)29 b(pre)n(viously)d(distrib)n(uted)i(under)f(the)h(GNU)h(General)f (Public)g(License)h(\(GPL\),)f(the)g(la)o(w)h(of)f(the)g(Commonwealth)f (of)208 3535 y(V)-5 b(ir)o(ginia)19 b(shall)j(go)o(v)o(ern)c(this)k (License)e(Agreement)g(only)g(as)h(to)g(issues)h(arising)f(under)e(or)i (with)g(respect)g(to)g(P)o(aragraphs)e(4,)i(5,)208 3635 y(and)d(7)i(of)f(this)h(License)f(Agreement.)k(Nothing)18 b(in)h(this)h(License)f(Agreement)f(shall)i(be)f(deemed)f(to)h(create)g (an)o(y)g(relationship)208 3734 y(of)24 b(agenc)o(y)-5 b(,)24 b(partnership,)g(or)g(joint)h(v)o(enture)e(between)h(CNRI)h(and) f(Licensee.)39 b(This)25 b(License)f(Agreement)f(does)i(not)f(grant)208 3834 y(permission)29 b(to)i(use)g(CNRI)g(trademarks)e(or)h(trade)g (name)g(in)h(a)g(trademark)e(sense)i(to)f(endorse)g(or)g(promote)f (products)g(or)208 3934 y(services)20 b(of)g(Licensee,)f(or)h(an)o(y)g (third)f(party)-5 b(.)104 4094 y(8.)41 b(By)17 b(clicking)f(on)g(the)h (\223)-7 b(A)m(CCEPT\224)18 b(b)n(utton)e(where)g(indicated,)g(or)h(by) f(cop)o(ying,)g(installing)g(or)h(otherwise)f(using)g(Python)g(1.6.1,) 208 4193 y(Licensee)j(agrees)h(to)h(be)f(bound)e(by)i(the)g(terms)g (and)g(conditions)e(of)i(this)h(License)f(Agreement.)1793 4362 y(A)m(CCEPT)722 4461 y Fr(CWI)h(LICENSE)h(A)-5 b(GREEMENT)23 b(FOR)d(PYTHON)g(0.9.0)e(THR)n(OUGH)h(1.2)0 4608 y Fs(Cop)o(yright)g (\251)i(1991)d(-)j(1995,)d(Stichting)i(Mathematisch)f(Centrum)g (Amsterdam,)g(The)h(Netherlands.)k(All)d(rights)e(reserv)o(ed.)0 4755 y(Permission)g(to)h(use,)g(cop)o(y)-5 b(,)19 b(modify)-5 b(,)17 b(and)i(distrib)n(ute)h(this)g(softw)o(are)f(and)h(its)g (documentation)d(for)i(an)o(y)g(purpose)g(and)g(without)g(fee)h(is)0 4855 y(hereby)i(granted,)g(pro)o(vided)f(that)i(the)h(abo)o(v)o(e)d (cop)o(yright)h(notice)g(appear)h(in)g(all)h(copies)f(and)g(that)g (both)g(that)g(cop)o(yright)e(notice)i(and)0 4954 y(this)f(permission)f (notice)g(appear)f(in)i(supporting)d(documentation,)g(and)i(that)h(the) f(name)g(of)h(Stichting)f(Mathematisch)f(Centrum)h(or)0 5054 y(CWI)26 b(not)f(be)h(used)f(in)g(adv)o(ertising)f(or)h(publicity) f(pertaining)g(to)h(distrib)n(ution)g(of)g(the)g(softw)o(are)g(without) g(speci\002c,)h(written)g(prior)0 5154 y(permission.)0 5300 y(STICHTING)18 b(MA)-9 b(THEMA)g(TISCH)18 b(CENTR)m(UM)g (DISCLAIMS)h(ALL)f(W)-10 b(ARRANTIES)20 b(WITH)f(REGARD)g(T)o(O)f(THIS) h(SOFT)-8 b(-)0 5400 y(W)e(ARE,)23 b(INCLUDING)e(ALL)h(IMPLIED)f(W)-10 b(ARRANTIES)23 b(OF)g(MERCHANT)-8 b(ABILITY)21 b(AND)i(FITNESS,)e(IN)i (NO)f(EVENT)p 0 5549 3901 4 v 0 5649 a Fo(104)2535 b(Appendix)24 b(C)n(.)46 b(Histor)r(y)22 b(and)i(License)p eop end %%Page: 105 113 TeXDict begin 105 112 bop 0 83 a Fs(SHALL)30 b(STICHTING)g(MA)-9 b(THEMA)g(TISCH)29 b(CENTR)m(UM)h(BE)g(LIABLE)g(FOR)h(ANY)f(SPECIAL,)g (INDIRECT)g(OR)h(CON-)0 183 y(SEQ)o(UENTIAL)e(D)m(AMA)m(GES)h(OR)h(ANY) f(D)m(AMA)m(GES)f(WHA)-9 b(TSOEVER)31 b(RESUL)-8 b(TING)30 b(FR)m(OM)g(LOSS)h(OF)g(USE,)f(D)m(A)-9 b(T)h(A)0 282 y(OR)25 b(PR)m(OFITS,)f(WHETHER)h(IN)f(AN)h(A)m(CTION)f(OF)h(CONTRA)m (CT)-6 b(,)24 b(NEGLIGENCE)g(OR)h(O)m(THER)f(T)o(OR)-5 b(TIOUS)24 b(A)m(CTION,)0 382 y(ARISING)c(OUT)h(OF)g(OR)g(IN)f (CONNECTION)g(WITH)h(THE)f(USE)h(OR)g(PERFORMANCE)g(OF)g(THIS)f(SOFTW) -10 b(ARE.)0 709 y Fl(C)l(.3)121 b(Licenses)34 b(and)h(Ac)n(kno)n (wledgements)g(f)l(or)f(Incor)t(por)o(ated)i(Softw)n(are)0 942 y Fs(This)18 b(section)f(is)h(an)g(incomplete,)e(b)n(ut)h(gro)n (wing)f(list)i(of)g(licenses)f(and)g(ackno)n(wledgements)e(for)h (third-party)f(softw)o(are)i(incorporated)0 1042 y(in)j(the)h(Python)e (distrib)n(ution.)0 1327 y Fj(C)m(.3.1)100 b(Mersenne)29 b(T)-12 b(wister)0 1530 y Fs(The)15 b Fp(_random)f Fs(module)g (includes)h(code)f(based)h(on)g(a)g(do)n(wnload)f(from)g Ft(http://www)l(.math.k)o(eio)m(.ac.j)o(p/)g(matumoto/MT2002/emt19937)o (ar)l(.h)o(tml)-6 b Fs(.)0 1629 y(The)20 b(follo)n(wing)e(are)j(the)f (v)o(erbatim)e(comments)h(from)h(the)g(original)f(code:)p 0 5549 3901 4 v 0 5649 a Fo(C)n(.3.)51 b(Licenses)23 b(and)h(Ac)n(kno)o(wledgements)g(f)n(or)e(Incor)r(por)o(ated)i(Softw)o (are)1374 b(105)p eop end %%Page: 106 114 TeXDict begin 106 113 bop 236 174 a Fk(A)45 b(C-program)e(for)i (MT19937,)e(with)h(initialization)f(improved)g(2002/1/26.)236 266 y(Coded)h(by)h(Takuji)f(Nishimura)f(and)h(Makoto)g(Matsumoto.)236 448 y(Before)g(using,)g(initialize)f(the)h(state)g(by)h(using)f (init_genrand\(seed\))236 540 y(or)h(init_by_array\(init_key,)c (key_length\).)236 722 y(Copyright)j(\(C\))g(1997)g(-)h(2002,)f(Makoto) g(Matsumoto)f(and)h(Takuji)g(Nishimura,)236 814 y(All)h(rights)e (reserved.)236 996 y(Redistribution)g(and)h(use)g(in)h(source)f(and)g (binary)g(forms,)g(with)g(or)g(without)236 1088 y(modification,)f(are)h (permitted)g(provided)f(that)h(the)g(following)g(conditions)236 1179 y(are)h(met:)281 1362 y(1.)g(Redistributions)d(of)i(source)g(code) g(must)g(retain)g(the)h(above)f(copyright)416 1453 y(notice,)f(this)h (list)h(of)f(conditions)f(and)h(the)h(following)e(disclaimer.)281 1636 y(2.)i(Redistributions)d(in)i(binary)g(form)g(must)g(reproduce)g (the)g(above)g(copyright)416 1727 y(notice,)f(this)h(list)h(of)f (conditions)f(and)h(the)h(following)e(disclaimer)g(in)i(the)416 1818 y(documentation)d(and/or)i(other)g(materials)f(provided)h(with)g (the)g(distribution.)281 2001 y(3.)h(The)f(names)g(of)g(its)h (contributors)e(may)h(not)g(be)h(used)f(to)g(endorse)g(or)g(promote)416 2092 y(products)f(derived)h(from)g(this)g(software)f(without)h (specific)g(prior)g(written)416 2183 y(permission.)236 2366 y(THIS)g(SOFTWARE)g(IS)g(PROVIDED)g(BY)g(THE)h(COPYRIGHT)e (HOLDERS)h(AND)g(CONTRIBUTORS)236 2457 y("AS)h(IS")f(AND)g(ANY)h (EXPRESS)e(OR)i(IMPLIED)e(WARRANTIES,)g(INCLUDING,)g(BUT)i(NOT)236 2549 y(LIMITED)f(TO,)g(THE)h(IMPLIED)e(WARRANTIES)g(OF)i (MERCHANTABILITY)d(AND)i(FITNESS)g(FOR)236 2640 y(A)h(PARTICULAR)e (PURPOSE)h(ARE)g(DISCLAIMED.)88 b(IN)44 b(NO)h(EVENT)f(SHALL)g(THE)g (COPYRIGHT)f(OWNER)h(OR)236 2731 y(CONTRIBUTORS)f(BE)i(LIABLE)e(FOR)i (ANY)f(DIRECT,)g(INDIRECT,)f(INCIDENTAL,)g(SPECIAL,)236 2823 y(EXEMPLARY,)g(OR)i(CONSEQUENTIAL)e(DAMAGES)g(\(INCLUDING,)g(BUT)h (NOT)h(LIMITED)e(TO,)236 2914 y(PROCUREMENT)g(OF)i(SUBSTITUTE)e(GOODS)h (OR)g(SERVICES;)g(LOSS)g(OF)g(USE,)g(DATA,)g(OR)236 3005 y(PROFITS;)g(OR)g(BUSINESS)g(INTERRUPTION\))e(HOWEVER)i(CAUSED)g(AND)g (ON)h(ANY)f(THEORY)g(OF)236 3097 y(LIABILITY,)f(WHETHER)h(IN)h (CONTRACT,)e(STRICT)h(LIABILITY,)f(OR)h(TORT)g(\(INCLUDING)236 3188 y(NEGLIGENCE)f(OR)i(OTHERWISE\))e(ARISING)h(IN)g(ANY)g(WAY)h(OUT)f (OF)h(THE)f(USE)g(OF)h(THIS)236 3279 y(SOFTWARE,)f(EVEN)g(IF)g(ADVISED) g(OF)g(THE)h(POSSIBILITY)e(OF)h(SUCH)g(DAMAGE.)236 3553 y(Any)h(feedback)e(is)i(very)f(welcome.)236 3645 y (http://www.math.keio.ac.jp/matumoto/emt.)o(html)236 3736 y(email:)g(matumoto@math.keio.ac.jp)0 4161 y Fj(C)m(.3.2)100 b(Soc)n(k)n(ets)0 4363 y Fs(The)23 b Fp(socket)g Fs(module)e(uses)j (the)g(functions,)e Fp(getaddrinfo)p Fs(,)g(and)h Fp(getnameinfo)p Fs(,)f(which)h(are)g(coded)f(in)i(separate)e(source)0 4463 y(\002les)f(from)e(the)i(WIDE)f(Project,)g Ft(http://www)l(.wide)o (.ad.j)o(p/a)o(bo)o(ut/)o(ind)o(e)m(x.html)-6 b Fs(.)p 0 5549 3901 4 v 0 5649 a Fo(106)2535 b(Appendix)24 b(C)n(.)46 b(Histor)r(y)22 b(and)i(License)p eop end %%Page: 107 115 TeXDict begin 107 114 bop 236 83 a Fk(Copyright)44 b(\(C\))g(1995,)g (1996,)g(1997,)g(and)g(1998)g(WIDE)g(Project.)236 174 y(All)h(rights)e(reserved.)236 357 y(Redistribution)g(and)h(use)g(in)h (source)f(and)g(binary)g(forms,)g(with)g(or)g(without)236 448 y(modification,)f(are)h(permitted)g(provided)f(that)h(the)g (following)g(conditions)236 540 y(are)h(met:)236 631 y(1.)g(Redistributions)d(of)j(source)e(code)i(must)f(retain)f(the)i (above)f(copyright)371 722 y(notice,)f(this)i(list)f(of)g(conditions)f (and)i(the)f(following)f(disclaimer.)236 814 y(2.)i(Redistributions)d (in)j(binary)e(form)i(must)f(reproduce)f(the)h(above)g(copyright)371 905 y(notice,)f(this)i(list)f(of)g(conditions)f(and)i(the)f(following)f (disclaimer)g(in)i(the)371 996 y(documentation)d(and/or)i(other)g (materials)f(provided)h(with)g(the)g(distribution.)236 1088 y(3.)h(Neither)e(the)i(name)f(of)g(the)h(project)e(nor)i(the)f (names)g(of)g(its)h(contributors)371 1179 y(may)f(be)h(used)f(to)g (endorse)g(or)g(promote)g(products)f(derived)h(from)g(this)g(software) 371 1270 y(without)f(specific)h(prior)g(written)g(permission.)236 1453 y(THIS)g(SOFTWARE)g(IS)g(PROVIDED)g(BY)g(THE)h(PROJECT)e(AND)i (CONTRIBUTORS)d(``AS)j(IS'')f(AND)236 1544 y(GAI_ANY)g(EXPRESS)g(OR)g (IMPLIED)g(WARRANTIES,)f(INCLUDING,)g(BUT)h(NOT)g(LIMITED)g(TO,)g(THE) 236 1636 y(IMPLIED)g(WARRANTIES)f(OF)i(MERCHANTABILITY)d(AND)i(FITNESS) g(FOR)g(A)h(PARTICULAR)e(PURPOSE)236 1727 y(ARE)i(DISCLAIMED.)87 b(IN)45 b(NO)f(EVENT)g(SHALL)g(THE)h(PROJECT)e(OR)i(CONTRIBUTORS)e(BE)h (LIABLE)236 1818 y(FOR)h(GAI_ANY)e(DIRECT,)h(INDIRECT,)f(INCIDENTAL,)g (SPECIAL,)h(EXEMPLARY,)f(OR)h(CONSEQUENTIAL)236 1910 y(DAMAGES)g(\(INCLUDING,)f(BUT)h(NOT)h(LIMITED)e(TO,)h(PROCUREMENT)f (OF)i(SUBSTITUTE)e(GOODS)236 2001 y(OR)i(SERVICES;)e(LOSS)h(OF)h(USE,)f (DATA,)g(OR)g(PROFITS;)g(OR)g(BUSINESS)g(INTERRUPTION\))236 2092 y(HOWEVER)g(CAUSED)g(AND)g(ON)h(GAI_ANY)e(THEORY)h(OF)g (LIABILITY,)g(WHETHER)f(IN)i(CONTRACT,)e(STRICT)236 2183 y(LIABILITY,)g(OR)i(TORT)f(\(INCLUDING)f(NEGLIGENCE)g(OR)i(OTHERWISE\)) e(ARISING)h(IN)g(GAI_ANY)g(WAY)236 2275 y(OUT)h(OF)f(THE)g(USE)h(OF)f (THIS)g(SOFTWARE,)g(EVEN)g(IF)g(ADVISED)g(OF)g(THE)h(POSSIBILITY)e(OF) 236 2366 y(SUCH)h(DAMAGE.)0 2791 y Fj(C)m(.3.3)100 b(Floating)29 b(point)f(e)m(xception)i(control)0 2994 y Fs(The)20 b(source)f(for)h (the)g Fp(fpectl)g Fs(module)f(includes)g(the)h(follo)n(wing)f(notice:) p 0 5549 3901 4 v 0 5649 a Fo(C)n(.3.)51 b(Licenses)23 b(and)h(Ac)n(kno)o(wledgements)g(f)n(or)e(Incor)r(por)o(ated)i(Softw)o (are)1374 b(107)p eop end %%Page: 108 116 TeXDict begin 108 115 bop 460 174 a Fk (-----------------------------------------)o(------)o(------)o(------)o (------)o(----)416 266 y(/)1031 b(Copyright)43 b(\(c\))h(1996.)1210 b(\\)371 357 y(|)448 b(The)44 b(Regents)g(of)g(the)h(University)e(of)h (California.)761 b(|)371 448 y(|)1076 b(All)44 b(rights)g(reserved.) 1209 b(|)371 540 y(|)3183 b(|)371 631 y(|)134 b(Permission)43 b(to)i(use,)f(copy,)g(modify,)f(and)i(distribute)e(this)h(software)g (for)134 b(|)371 722 y(|)g(any)44 b(purpose)g(without)g(fee)g(is)h (hereby)e(granted,)h(provided)f(that)h(this)h(en-)134 b(|)371 814 y(|)g(tire)44 b(notice)g(is)h(included)e(in)i(all)f(copies) g(of)g(any)g(software)g(which)g(is)g(or)135 b(|)371 905 y(|)f(includes)88 b(a)i(copy)f(or)g(modification)f(of)h(this)44 b(software)g(and)g(in)h(all)134 b(|)371 996 y(|)g(copies)44 b(of)g(the)h(supporting)e(documentation)f(for)j(such)f(software.)492 b(|)371 1088 y(|)3183 b(|)371 1179 y(|)134 b(This)89 b(work)44 b(was)h(produced)e(at)i(the)f(University)f(of)h(California,)f (Lawrence)134 b(|)371 1270 y(|)g(Livermore)43 b(National)h(Laboratory)f (under)89 b(contract)f(no.)h(W-7405-ENG-48)133 b(|)371 1362 y(|)h(between)89 b(the)g(U.S.)g(Department)f(of)h(Energy)44 b(and)g(The)g(Regents)g(of)h(the)134 b(|)371 1453 y(|)g(University)43 b(of)i(California)e(for)h(the)g(operation)g(of)g(UC)h(LLNL.)627 b(|)371 1544 y(|)3183 b(|)371 1636 y(|)1345 b(DISCLAIMER)1388 b(|)371 1727 y(|)3183 b(|)371 1818 y(|)134 b(This)89 b(software)44 b(was)g(prepared)f(as)i(an)f(account)g(of)g(work)h (sponsored)e(by)h(an)135 b(|)371 1910 y(|)f(agency)44 b(of)g(the)h(United)f(States)f(Government.)g(Neither)h(the)g(United)g (States)134 b(|)371 2001 y(|)g(Government)88 b(nor)44 b(the)h(University)e(of)h(California)f(nor)i(any)f(of)h(their)f(em-)134 b(|)371 2092 y(|)g(ployees,)44 b(makes)g(any)g(warranty,)f(express)h (or)g(implied,)g(or)89 b(assumes)g(any)134 b(|)371 2183 y(|)g(liability)88 b(or)i(responsibility)d(for)44 b(the)g(accuracy,)g (completeness,)e(or)135 b(|)371 2275 y(|)f(usefulness)43 b(of)i(any)f(information,)88 b(apparatus,)g(product,)g(or)h(process)134 b(|)371 2366 y(|)g(disclosed,)f(or)89 b(represents)f(that)h(its)g(use)g (would)g(not)g(infringe)134 b(|)371 2457 y(|)g(privately-owned)42 b(rights.)i(Reference)f(herein)h(to)h(any)f(specific)88 b(commer-)134 b(|)371 2549 y(|)g(cial)89 b(products,)f(process,)h(or)g (service)f(by)45 b(trade)f(name,)g(trademark,)133 b(|)371 2640 y(|)h(manufacturer,)43 b(or)h(otherwise,)f(does)h(not)90 b(necessarily)d(constitute)h(or)135 b(|)371 2731 y(|)f(imply)89 b(its)44 b(endorsement,)f(recommendation,)f(or)j(favoring)e(by)i(the)f (United)134 b(|)371 2823 y(|)g(States)44 b(Government)f(or)i(the)f (University)f(of)h(California.)f(The)i(views)89 b(and)134 b(|)371 2914 y(|)g(opinions)88 b(of)45 b(authors)f(expressed)f(herein)h (do)g(not)g(necessarily)f(state)h(or)135 b(|)371 3005 y(|)f(reflect)44 b(those)g(of)g(the)h(United)e(States)h(Government)f (or)90 b(the)f(University)133 b(|)371 3097 y(|)h(of)89 b(California,)f(and)45 b(shall)f(not)g(be)g(used)g(for)h(advertising)e (or)h(product)134 b(|)416 3188 y(\\)89 b(endorsement)43 b(purposes.)2061 b(/)460 3279 y (-----------------------------------------)o(------)o(------)o(------)o (------)o(----)0 3704 y Fj(C)m(.3.4)100 b(MD5)28 b(message)h(digest)f (algor)q(ithm)0 3907 y Fs(The)20 b(source)f(code)h(for)f(the)i Fp(md5)f Fs(module)f(contains)g(the)h(follo)n(wing)f(notice:)p 0 5549 3901 4 v 0 5649 a Fo(108)2535 b(Appendix)24 b(C)n(.)46 b(Histor)r(y)22 b(and)i(License)p eop end %%Page: 109 117 TeXDict begin 109 116 bop 326 174 a Fk(Copyright)43 b(\(C\))i(1999,)f (2002)g(Aladdin)f(Enterprises.)88 b(All)44 b(rights)g(reserved.)326 357 y(This)g(software)g(is)g(provided)g('as-is',)f(without)h(any)g (express)g(or)g(implied)326 448 y(warranty.)88 b(In)45 b(no)f(event)g(will)g(the)h(authors)e(be)i(held)f(liable)g(for)g(any)g (damages)326 540 y(arising)g(from)g(the)g(use)g(of)h(this)f(software.) 326 722 y(Permission)f(is)i(granted)e(to)i(anyone)f(to)g(use)g(this)g (software)g(for)g(any)h(purpose,)326 814 y(including)e(commercial)g (applications,)g(and)h(to)h(alter)f(it)g(and)h(redistribute)d(it)326 905 y(freely,)i(subject)f(to)i(the)f(following)f(restrictions:)326 1088 y(1.)h(The)h(origin)f(of)g(this)g(software)g(must)g(not)g(be)h (misrepresented;)d(you)i(must)g(not)460 1179 y(claim)g(that)h(you)f (wrote)g(the)g(original)g(software.)f(If)h(you)h(use)f(this)g(software) 460 1270 y(in)h(a)g(product,)e(an)i(acknowledgment)d(in)i(the)h (product)e(documentation)g(would)h(be)460 1362 y(appreciated)f(but)i (is)f(not)h(required.)326 1453 y(2.)f(Altered)g(source)g(versions)f (must)i(be)f(plainly)g(marked)f(as)i(such,)f(and)g(must)g(not)h(be)460 1544 y(misrepresented)e(as)h(being)g(the)h(original)e(software.)326 1636 y(3.)h(This)h(notice)e(may)i(not)f(be)h(removed)e(or)i(altered)e (from)h(any)h(source)f(distribution.)326 1818 y(L.)g(Peter)g(Deutsch) 326 1910 y(ghost@aladdin.com)326 2092 y(Independent)f(implementation)f (of)j(MD5)f(\(RFC)g(1321\).)326 2275 y(This)g(code)g(implements)f(the)i (MD5)f(Algorithm)f(defined)h(in)g(RFC)h(1321,)f(whose)326 2366 y(text)g(is)h(available)e(at)236 2457 y (http://www.ietf.org/rfc/rfc1321.txt)326 2549 y(The)h(code)g(is)h (derived)f(from)g(the)g(text)g(of)h(the)f(RFC,)g(including)f(the)i (test)f(suite)326 2640 y(\(section)f(A.5\))i(but)f(excluding)f(the)i (rest)f(of)g(Appendix)g(A.)89 b(It)44 b(does)h(not)f(include)326 2731 y(any)g(code)g(or)h(documentation)e(that)h(is)g(identified)f(in)i (the)f(RFC)g(as)h(being)326 2823 y(copyrighted.)326 3005 y(The)f(original)g(and)g(principal)f(author)h(of)h(md5.h)f(is)g(L.)h (Peter)f(Deutsch)326 3097 y(.)86 b(Other)44 b(authors)g(are)g(noted)g(in)h(the)f(change)g(history)326 3188 y(that)g(follows)g(\(in)g(reverse)g(chronological)e(order\):)326 3371 y(2002-04-13)h(lpd)h(Removed)g(support)g(for)g(non-ANSI)f (compilers;)h(removed)236 3462 y(references)f(to)i(Ghostscript;)e (clarified)g(derivation)g(from)h(RFC)h(1321;)236 3553 y(now)g(handles)e(byte)h(order)g(either)g(statically)f(or)i (dynamically.)326 3645 y(1999-11-04)e(lpd)h(Edited)g(comments)g (slightly)f(for)h(automatic)g(TOC)g(extraction.)326 3736 y(1999-10-18)f(lpd)h(Fixed)g(typo)h(in)f(header)g(comment)f(\(ansi2knr) h(rather)g(than)g(md5\);)236 3827 y(added)g(conditionalization)e(for)i (C++)h(compilation)e(from)h(Martin)236 3919 y(Purschke)g (.)326 4010 y(1999-05-03)f(lpd)h(Original)g(version.) 0 4435 y Fj(C)m(.3.5)100 b(Asynchronous)30 b(soc)n(k)n(et)d(ser)s (vices)0 4637 y Fs(The)20 b Fp(asynchat)f Fs(and)h Fp(asyncore)f Fs(modules)g(contain)g(the)i(follo)n(wing)d(notice:)p 0 5549 3901 4 v 0 5649 a Fo(C)n(.3.)51 b(Licenses)23 b(and)h(Ac)n(kno)o(wledgements)g(f)n(or)e(Incor)r(por)o(ated)i(Softw)o (are)1374 b(109)p eop end %%Page: 110 118 TeXDict begin 110 117 bop 281 174 a Fk(Copyright)43 b(1996)i(by)f(Sam)g (Rushing)1357 357 y(All)g(Rights)g(Reserved)281 540 y(Permission)f(to)i (use,)f(copy,)g(modify,)g(and)g(distribute)f(this)h(software)g(and)281 631 y(its)g(documentation)f(for)h(any)h(purpose)e(and)i(without)e(fee)i (is)f(hereby)281 722 y(granted,)g(provided)f(that)h(the)h(above)f (copyright)f(notice)h(appear)f(in)i(all)281 814 y(copies)f(and)g(that)g (both)h(that)f(copyright)f(notice)h(and)g(this)g(permission)281 905 y(notice)g(appear)g(in)g(supporting)f(documentation,)g(and)h(that)g (the)h(name)f(of)g(Sam)281 996 y(Rushing)g(not)g(be)h(used)f(in)g (advertising)f(or)i(publicity)e(pertaining)g(to)281 1088 y(distribution)g(of)h(the)h(software)e(without)h(specific,)f(written)h (prior)281 1179 y(permission.)281 1362 y(SAM)g(RUSHING)g(DISCLAIMS)f (ALL)i(WARRANTIES)e(WITH)h(REGARD)g(TO)g(THIS)g(SOFTWARE,)281 1453 y(INCLUDING)f(ALL)i(IMPLIED)e(WARRANTIES)h(OF)g(MERCHANTABILITY)e (AND)j(FITNESS,)e(IN)281 1544 y(NO)i(EVENT)f(SHALL)g(SAM)g(RUSHING)g (BE)g(LIABLE)g(FOR)g(ANY)g(SPECIAL,)g(INDIRECT)f(OR)281 1636 y(CONSEQUENTIAL)g(DAMAGES)g(OR)i(ANY)f(DAMAGES)g(WHATSOEVER)f (RESULTING)g(FROM)h(LOSS)281 1727 y(OF)h(USE,)f(DATA)g(OR)g(PROFITS,)g (WHETHER)g(IN)g(AN)g(ACTION)g(OF)h(CONTRACT,)281 1818 y(NEGLIGENCE)e(OR)i(OTHER)f(TORTIOUS)f(ACTION,)h(ARISING)f(OUT)i(OF)f (OR)h(IN)281 1910 y(CONNECTION)e(WITH)h(THE)h(USE)f(OR)h(PERFORMANCE)d (OF)j(THIS)f(SOFTWARE.)0 2334 y Fj(C)m(.3.6)100 b(Cookie)28 b(management)0 2537 y Fs(The)20 b Fp(Cookie)f Fs(module)g(contains)h (the)g(follo)n(wing)f(notice:)281 2775 y Fk(Copyright)43 b(2000)i(by)f(Timothy)g(O'Malley)f()954 2958 y(All)h(Rights)g(Reserved)281 3140 y(Permission)f(to)i(use,)f (copy,)g(modify,)g(and)g(distribute)f(this)h(software)281 3232 y(and)g(its)h(documentation)d(for)j(any)f(purpose)g(and)g(without) g(fee)g(is)g(hereby)281 3323 y(granted,)g(provided)f(that)h(the)h (above)f(copyright)f(notice)h(appear)f(in)i(all)281 3414 y(copies)f(and)g(that)g(both)h(that)f(copyright)f(notice)h(and)g(this)g (permission)281 3506 y(notice)g(appear)g(in)g(supporting)f (documentation,)g(and)h(that)g(the)h(name)f(of)281 3597 y(Timothy)g(O'Malley)88 b(not)44 b(be)h(used)f(in)h(advertising)d(or)j (publicity)281 3688 y(pertaining)e(to)i(distribution)e(of)h(the)g (software)g(without)f(specific,)h(written)281 3780 y(prior)g (permission.)281 3962 y(Timothy)g(O'Malley)f(DISCLAIMS)h(ALL)g (WARRANTIES)f(WITH)h(REGARD)g(TO)g(THIS)281 4054 y(SOFTWARE,)f (INCLUDING)h(ALL)g(IMPLIED)g(WARRANTIES)f(OF)h(MERCHANTABILITY)281 4145 y(AND)g(FITNESS,)g(IN)g(NO)h(EVENT)f(SHALL)g(Timothy)f(O'Malley)h (BE)g(LIABLE)g(FOR)281 4236 y(ANY)g(SPECIAL,)g(INDIRECT)f(OR)i (CONSEQUENTIAL)d(DAMAGES)i(OR)h(ANY)f(DAMAGES)281 4328 y(WHATSOEVER)f(RESULTING)h(FROM)g(LOSS)g(OF)g(USE,)g(DATA)h(OR)f (PROFITS,)281 4419 y(WHETHER)g(IN)g(AN)h(ACTION)f(OF)g(CONTRACT,)f (NEGLIGENCE)g(OR)i(OTHER)f(TORTIOUS)281 4510 y(ACTION,)g(ARISING)f(OUT) i(OF)f(OR)h(IN)f(CONNECTION)f(WITH)h(THE)h(USE)f(OR)281 4602 y(PERFORMANCE)f(OF)i(THIS)f(SOFTWARE.)0 5026 y Fj(C)m(.3.7)100 b(Pro\002ling)0 5229 y Fs(The)20 b Fp(profile)f Fs(and)h Fp(pstats)g Fs(modules)f(contain)g(the)h(follo)n(wing)f(notice:)p 0 5549 3901 4 v 0 5649 a Fo(110)2535 b(Appendix)24 b(C)n(.)46 b(Histor)r(y)22 b(and)i(License)p eop end %%Page: 111 119 TeXDict begin 111 118 bop 281 174 a Fk(Copyright)43 b(1994,)h(by)h (InfoSeek)e(Corporation,)g(all)h(rights)g(reserved.)281 266 y(Written)g(by)g(James)g(Roskind)281 448 y(Permission)f(to)i(use,)f (copy,)g(modify,)g(and)g(distribute)f(this)h(Python)g(software)281 540 y(and)g(its)h(associated)e(documentation)g(for)h(any)g(purpose)g (\(subject)f(to)i(the)281 631 y(restriction)e(in)i(the)f(following)f (sentence\))g(without)h(fee)g(is)h(hereby)f(granted,)281 722 y(provided)g(that)g(the)g(above)g(copyright)f(notice)h(appears)g (in)g(all)h(copies,)e(and)281 814 y(that)h(both)g(that)h(copyright)e (notice)h(and)g(this)g(permission)f(notice)h(appear)g(in)281 905 y(supporting)f(documentation,)g(and)h(that)g(the)g(name)h(of)f (InfoSeek)g(not)g(be)g(used)h(in)281 996 y(advertising)e(or)i (publicity)e(pertaining)g(to)h(distribution)f(of)i(the)f(software)281 1088 y(without)g(specific,)f(written)h(prior)g(permission.)87 b(This)45 b(permission)e(is)281 1179 y(explicitly)g(restricted)g(to)i (the)f(copying)g(and)g(modification)f(of)h(the)h(software)281 1270 y(to)g(remain)e(in)i(Python,)f(compiled)f(Python,)h(or)g(other)g (languages)f(\(such)h(as)h(C\))281 1362 y(wherein)f(the)g(modified)g (or)g(derived)g(code)g(is)g(exclusively)f(imported)h(into)g(a)281 1453 y(Python)g(module.)281 1636 y(INFOSEEK)g(CORPORATION)f(DISCLAIMS)g (ALL)h(WARRANTIES)f(WITH)h(REGARD)g(TO)h(THIS)281 1727 y(SOFTWARE,)e(INCLUDING)h(ALL)g(IMPLIED)g(WARRANTIES)f(OF)h (MERCHANTABILITY)f(AND)281 1818 y(FITNESS.)h(IN)g(NO)h(EVENT)f(SHALL)g (INFOSEEK)f(CORPORATION)g(BE)h(LIABLE)g(FOR)h(ANY)281 1910 y(SPECIAL,)f(INDIRECT)f(OR)i(CONSEQUENTIAL)d(DAMAGES)i(OR)g(ANY)h (DAMAGES)e(WHATSOEVER)281 2001 y(RESULTING)g(FROM)i(LOSS)f(OF)g(USE,)g (DATA)h(OR)f(PROFITS,)f(WHETHER)h(IN)h(AN)f(ACTION)g(OF)281 2092 y(CONTRACT,)f(NEGLIGENCE)h(OR)g(OTHER)g(TORTIOUS)f(ACTION,)h (ARISING)g(OUT)g(OF)h(OR)f(IN)281 2183 y(CONNECTION)f(WITH)h(THE)h(USE) f(OR)h(PERFORMANCE)d(OF)j(THIS)f(SOFTWARE.)0 2608 y Fj(C)m(.3.8)100 b(Ex)m(ecution)29 b(tr)o(acing)0 2811 y Fs(The)20 b Fp(trace)g Fs(module)e(contains)i(the)g(follo)n(wing)f(notice:)p 0 5549 3901 4 v 0 5649 a Fo(C)n(.3.)51 b(Licenses)23 b(and)h(Ac)n(kno)o(wledgements)g(f)n(or)e(Incor)r(por)o(ated)i(Softw)o (are)1374 b(111)p eop end %%Page: 112 120 TeXDict begin 112 119 bop 281 174 a Fk(portions)44 b(copyright)f(2001,) h(Autonomous)f(Zones)h(Industries,)f(Inc.,)h(all)g(rights...)281 266 y(err...)89 b(reserved)43 b(and)i(offered)e(to)i(the)f(public)g (under)g(the)g(terms)g(of)h(the)281 357 y(Python)f(2.2)g(license.)281 448 y(Author:)g(Zooko)g(O'Whielacronx)281 540 y(http://zooko.com/)281 631 y(mailto:zooko@zooko.com)281 814 y(Copyright)f(2000,)h(Mojam)g (Media,)g(Inc.,)g(all)g(rights)g(reserved.)281 905 y(Author:)g(Skip)g (Montanaro)281 1088 y(Copyright)f(1999,)h(Bioreason,)g(Inc.,)g(all)g (rights)g(reserved.)281 1179 y(Author:)g(Andrew)g(Dalke)281 1362 y(Copyright)f(1995-1997,)h(Automatrix,)f(Inc.,)g(all)i(rights)f (reserved.)281 1453 y(Author:)g(Skip)g(Montanaro)281 1636 y(Copyright)f(1991-1995,)h(Stichting)f(Mathematisch)g(Centrum,)g (all)h(rights)g(reserved.)281 1910 y(Permission)f(to)i(use,)f(copy,)g (modify,)g(and)g(distribute)f(this)h(Python)g(software)f(and)281 2001 y(its)h(associated)g(documentation)e(for)j(any)f(purpose)f (without)h(fee)g(is)h(hereby)281 2092 y(granted,)f(provided)f(that)h (the)h(above)f(copyright)f(notice)h(appears)f(in)i(all)f(copies,)281 2183 y(and)g(that)h(both)f(that)g(copyright)f(notice)h(and)g(this)g (permission)g(notice)f(appear)h(in)281 2275 y(supporting)f (documentation,)g(and)h(that)g(the)g(name)h(of)f(neither)g(Automatrix,) 281 2366 y(Bioreason)f(or)i(Mojam)f(Media)g(be)g(used)h(in)f (advertising)f(or)h(publicity)g(pertaining)f(to)281 2457 y(distribution)g(of)h(the)h(software)e(without)h(specific,)f(written)h (prior)g(permission.)0 2882 y Fj(C)m(.3.9)100 b(UUencode)29 b(and)g(UUdecode)g(functions)0 3085 y Fs(The)20 b Fp(uu)g Fs(module)f(contains)h(the)g(follo)n(wing)e(notice:)p 0 5549 3901 4 v 0 5649 a Fo(112)2535 b(Appendix)24 b(C)n(.)46 b(Histor)r(y)22 b(and)i(License)p eop end %%Page: 113 121 TeXDict begin 113 120 bop 281 174 a Fk(Copyright)43 b(1994)i(by)f (Lance)g(Ellinghouse)281 266 y(Cathedral)f(City,)h(California)g (Republic,)f(United)h(States)f(of)i(America.)1312 357 y(All)g(Rights)e(Reserved)281 448 y(Permission)g(to)i(use,)f(copy,)g (modify,)g(and)g(distribute)f(this)h(software)g(and)g(its)281 540 y(documentation)f(for)h(any)g(purpose)g(and)g(without)g(fee)g(is)h (hereby)f(granted,)281 631 y(provided)g(that)g(the)g(above)g(copyright) f(notice)h(appear)g(in)g(all)h(copies)f(and)g(that)281 722 y(both)g(that)g(copyright)g(notice)g(and)g(this)g(permission)f (notice)h(appear)g(in)281 814 y(supporting)f(documentation,)g(and)h (that)g(the)g(name)h(of)f(Lance)g(Ellinghouse)281 905 y(not)g(be)h(used)f(in)h(advertising)e(or)h(publicity)f(pertaining)g (to)i(distribution)281 996 y(of)g(the)f(software)f(without)h(specific,) f(written)h(prior)g(permission.)281 1088 y(LANCE)g(ELLINGHOUSE)f (DISCLAIMS)g(ALL)i(WARRANTIES)e(WITH)h(REGARD)g(TO)281 1179 y(THIS)g(SOFTWARE,)g(INCLUDING)f(ALL)h(IMPLIED)g(WARRANTIES)f(OF)h (MERCHANTABILITY)f(AND)281 1270 y(FITNESS,)h(IN)g(NO)h(EVENT)f(SHALL)g (LANCE)g(ELLINGHOUSE)e(CENTRUM)i(BE)h(LIABLE)281 1362 y(FOR)f(ANY)h(SPECIAL,)e(INDIRECT)h(OR)g(CONSEQUENTIAL)f(DAMAGES)g(OR)i (ANY)f(DAMAGES)281 1453 y(WHATSOEVER)f(RESULTING)h(FROM)g(LOSS)g(OF)g (USE,)g(DATA)h(OR)f(PROFITS,)g(WHETHER)f(IN)i(AN)281 1544 y(ACTION)f(OF)g(CONTRACT,)g(NEGLIGENCE)f(OR)h(OTHER)g(TORTIOUS)g (ACTION,)f(ARISING)h(OUT)281 1636 y(OF)h(OR)f(IN)h(CONNECTION)e(WITH)h (THE)g(USE)h(OR)f(PERFORMANCE)f(OF)h(THIS)h(SOFTWARE.)281 1818 y(Modified)f(by)g(Jack)g(Jansen,)g(CWI,)g(July)g(1995:)281 1910 y(-)h(Use)f(binascii)g(module)f(to)i(do)f(the)h(actual)e (line-by-line)g(conversion)371 2001 y(between)g(ascii)h(and)h(binary.)e (This)i(results)e(in)i(a)f(1000-fold)g(speedup.)f(The)h(C)371 2092 y(version)f(is)i(still)f(5)h(times)f(faster,)f(though.)281 2183 y(-)i(Arguments)e(more)h(compliant)g(with)g(python)f(standard)0 2608 y Fj(C)m(.3.10)100 b(XML)29 b(Remote)f(Procedure)i(Calls)0 2811 y Fs(The)20 b Fp(xmlrpclib)f Fs(module)g(contains)g(the)h(follo)n (wing)f(notice:)p 0 5549 3901 4 v 0 5649 a Fo(C)n(.3.)51 b(Licenses)23 b(and)h(Ac)n(kno)o(wledgements)g(f)n(or)e(Incor)r(por)o (ated)i(Softw)o(are)1374 b(113)p eop end %%Page: 114 122 TeXDict begin 114 121 bop 460 174 a Fk(The)45 b(XML-RPC)e(client)h (interface)g(is)281 357 y(Copyright)f(\(c\))i(1999-2002)e(by)i(Secret)e (Labs)h(AB)281 448 y(Copyright)f(\(c\))i(1999-2002)e(by)i(Fredrik)e (Lundh)281 631 y(By)i(obtaining,)e(using,)h(and/or)f(copying)h(this)g (software)g(and/or)f(its)281 722 y(associated)g(documentation,)g(you)h (agree)g(that)g(you)g(have)h(read,)f(understood,)281 814 y(and)g(will)h(comply)e(with)i(the)f(following)f(terms)h(and)g (conditions:)281 996 y(Permission)f(to)i(use,)f(copy,)g(modify,)g(and)g (distribute)f(this)h(software)g(and)281 1088 y(its)g(associated)g (documentation)e(for)j(any)f(purpose)f(and)i(without)e(fee)i(is)281 1179 y(hereby)f(granted,)f(provided)h(that)g(the)g(above)g(copyright)g (notice)f(appears)h(in)281 1270 y(all)g(copies,)g(and)g(that)h(both)f (that)g(copyright)f(notice)h(and)g(this)g(permission)281 1362 y(notice)g(appear)g(in)g(supporting)f(documentation,)g(and)h(that) g(the)h(name)f(of)281 1453 y(Secret)g(Labs)g(AB)h(or)f(the)g(author)g (not)h(be)f(used)g(in)h(advertising)e(or)h(publicity)281 1544 y(pertaining)f(to)i(distribution)e(of)h(the)g(software)g(without)f (specific,)h(written)281 1636 y(prior)g(permission.)281 1818 y(SECRET)g(LABS)g(AB)h(AND)f(THE)g(AUTHOR)g(DISCLAIMS)f(ALL)i (WARRANTIES)e(WITH)h(REGARD)281 1910 y(TO)h(THIS)f(SOFTWARE,)f (INCLUDING)g(ALL)i(IMPLIED)e(WARRANTIES)g(OF)i(MERCHANT-)281 2001 y(ABILITY)f(AND)g(FITNESS.)88 b(IN)45 b(NO)f(EVENT)g(SHALL)g (SECRET)g(LABS)g(AB)h(OR)f(THE)h(AUTHOR)281 2092 y(BE)g(LIABLE)e(FOR)i (ANY)f(SPECIAL,)g(INDIRECT)f(OR)h(CONSEQUENTIAL)f(DAMAGES)h(OR)g(ANY) 281 2183 y(DAMAGES)g(WHATSOEVER)f(RESULTING)g(FROM)h(LOSS)g(OF)h(USE,)f (DATA)g(OR)h(PROFITS,)281 2275 y(WHETHER)f(IN)g(AN)h(ACTION)f(OF)g (CONTRACT,)f(NEGLIGENCE)g(OR)i(OTHER)f(TORTIOUS)281 2366 y(ACTION,)g(ARISING)f(OUT)i(OF)f(OR)h(IN)f(CONNECTION)f(WITH)h(THE)h (USE)f(OR)h(PERFORMANCE)281 2457 y(OF)g(THIS)f(SOFTWARE.)p 0 5549 3901 4 v 0 5649 a Fo(114)2535 b(Appendix)24 b(C)n(.)46 b(Histor)r(y)22 b(and)i(License)p eop end %%Page: 115 123 TeXDict begin 115 122 bop 0 83 3901 9 v 3480 230 a Fo(APPENDIX)3814 427 y Fn(D)p 0 515 V 3079 978 a Fy(Glossar)6 b(y)0 1465 y Fd(\273>)41 b Fs(The)23 b(typical)g(Python)f(prompt)g(of)h(the)h (interacti)n(v)o(e)e(shell.)34 b(Often)23 b(seen)h(for)f(code)f(e)o (xamples)h(that)g(can)g(be)h(tried)f(right)f(a)o(w)o(ay)h(in)208 1565 y(the)d(interpreter)-5 b(.)0 1725 y Fd(...)41 b Fs(The)20 b(typical)g(Python)f(prompt)f(of)i(the)g(interacti)n(v)o(e)f (shell)i(when)e(entering)g(code)h(for)f(an)h(indented)f(code)h(block.)0 1886 y Fr(BDFL)42 b Fs(Bene)n(v)n(olent)19 b(Dictator)h(F)o(or)g(Life,) g(a.k.a.)k(Guido)19 b(v)n(an)h(Rossum,)g(Python')-5 b(s)19 b(creator)-5 b(.)0 2046 y Fr(byte)20 b(code)41 b Fs(The)22 b(internal)g(representation)f(of)i(a)g(Python)f(program)e(in)j(the)g (interpreter)-5 b(.)32 b(The)22 b(byte)h(code)f(is)i(also)f(cached)f (in)h Fp(.pyc)208 2146 y Fs(and)c Fp(.pyo)h Fs(\002les)g(so)h(that)f(e) o(x)o(ecuting)d(the)j(same)g(\002le)h(is)g(f)o(aster)f(the)g(second)f (time)h(\(recompilation)d(from)i(source)g(to)h(byte)f(code)208 2245 y(can)24 b(be)g(a)n(v)n(oided\).)36 b(This)25 b(\223intermediate)e (language\224)f(is)k(said)e(to)h(run)f(on)g(a)g(\223virtual)g (machine\224)f(that)i(calls)g(the)f(subroutines)208 2345 y(corresponding)16 b(to)21 b(each)e(bytecode.)0 2505 y Fr(classic)i(class)41 b Fs(An)o(y)20 b(class)h(which)f(does)g(not)f (inherit)h(from)f Fp(object)p Fs(.)24 b(See)d Fq(ne)o(w-style)f(class)p Fs(.)0 2666 y Fr(coer)o(cion)40 b Fs(The)20 b(implicit)h(con)m(v)o (ersion)d(of)j(an)f(instance)h(of)f(one)h(type)f(to)h(another)e(during) h(an)g(operation)f(which)i(in)m(v)n(olv)o(es)e(tw)o(o)i(ar)o(gu-)208 2766 y(ments)h(of)h(the)g(same)g(type.)33 b(F)o(or)22 b(e)o(xample,)g Fp(int\(3.15\))g Fs(con)m(v)o(erts)f(the)i(\003oating)g (point)f(number)f(to)i(the)g(inte)o(ger)f Fp(3)p Fs(,)h(b)n(ut)g(in)208 2865 y Fp(3+4.5)p Fs(,)17 b(each)g(ar)o(gument)f(is)i(of)f(a)h(dif)n (ferent)e(type)h(\(one)g(int,)h(one)f(\003oat\),)h(and)f(both)g(must)g (be)h(con)m(v)o(erted)d(to)j(the)f(same)h(type)f(be-)208 2965 y(fore)e(the)o(y)g(can)h(be)g(added)f(or)h(it)h(will)g(raise)f(a)h Fp(TypeError)p Fs(.)22 b(Coercion)16 b(between)f(tw)o(o)h(operands)f (can)h(be)g(performed)d(with)k(the)208 3064 y Fp(coerce)j Fs(b)n(uiltin)h(function;)f(thus,)h Fp(3+4.5)g Fs(is)h(equi)n(v)n (alent)e(to)h(calling)g Fp(operator.add\()2964 3079 y(*)3014 3064 y(coerce\(3,)45 b(4.5\)\))21 b Fs(and)208 3164 y(results)26 b(in)h Fp(operator.add\(3.0,)47 b(4.5\))p Fs(.)d(W)m(ithout)26 b(coercion,)g(all)h(ar)o(guments)e(of)h(e)n(v)o(en)g(compatible)f (types)h(w)o(ould)208 3264 y(ha)n(v)o(e)19 b(to)h(be)h(normalized)d(to) i(the)g(same)h(v)n(alue)e(by)h(the)g(programmer)m(,)d(e.g.,)i Fp(float\(3\)+4.5)g Fs(rather)g(than)h(just)h Fp(3+4.5)p Fs(.)0 3424 y Fr(complex)f(number)42 b Fs(An)24 b(e)o(xtension)f(of)h (the)h(f)o(amiliar)f(real)g(number)e(system)j(in)g(which)e(all)i (numbers)e(are)i(e)o(xpressed)e(as)i(a)g(sum)f(of)208 3524 y(a)f(real)g(part)g(and)g(an)g(imaginary)f(part.)33 b(Imaginary)21 b(numbers)h(are)h(real)g(multiples)g(of)g(the)g (imaginary)f(unit)h(\(the)g(square)f(root)208 3623 y(of)i Fp(-1)p Fs(\),)h(often)f(written)g Fp(i)h Fs(in)f(mathematics)g(or)g Fp(j)h Fs(in)g(engineering.)35 b(Python)24 b(has)g(b)n(uiltin)h (support)e(for)h(comple)o(x)e(numbers,)208 3723 y(which)27 b(are)h(written)g(with)g(this)h(latter)f(notation;)j(the)d(imaginary)e (part)i(is)h(written)e(with)i(a)f Fp(j)g Fs(suf)n(\002x,)i(e.g.,)f Fp(3+1j)p Fs(.)48 b(T)-7 b(o)29 b(get)208 3823 y(access)22 b(to)g(comple)o(x)d(equi)n(v)n(alents)i(of)g(the)h Fp(math)f Fs(module,)f(use)i Fp(cmath)p Fs(.)29 b(Use)22 b(of)g(comple)o(x)e (numbers)g(is)i(a)g(f)o(airly)g(adv)n(anced)208 3922 y(mathematical)d(feature.)24 b(If)c(you')l(re)e(not)i(a)o(w)o(are)g(of) g(a)g(need)g(for)f(them,)h(it')-5 b(s)21 b(almost)f(certain)g(you)f (can)h(safely)g(ignore)f(them.)0 4083 y Fr(descriptor)41 b Fs(An)o(y)17 b Fq(ne)o(w-style)i Fs(object)e(that)i(de\002nes)f(the)g (methods)g Fp(__get__\(\))p Fs(,)f Fp(__set__\(\))p Fs(,)g(or)h Fp(__delete__\(\))p Fs(.)23 b(When)18 b(a)208 4182 y(class)k(attrib)n (ute)f(is)i(a)f(descriptor)m(,)e(its)j(special)f(binding)d(beha)n(vior) h(is)j(triggered)d(upon)g(attrib)n(ute)i(lookup.)27 b(Normally)-5 b(,)20 b(writing)208 4282 y Fq(a.b)k Fs(looks)h(up)g(the)g(object)g Fq(b)g Fs(in)g(the)h(class)g(dictionary)d(for)i Fq(a)p Fs(,)h(b)n(ut)g(if)f Fq(b)h Fs(is)g(a)f(descriptor)m(,)g(the)g (de\002ned)g(method)e(gets)j(called.)208 4382 y(Understanding)i (descriptors)j(is)h(a)g(k)o(e)o(y)e(to)i(a)g(deep)e(understanding)f(of) i(Python)f(because)h(the)o(y)f(are)i(the)f(basis)h(for)f(man)o(y)208 4481 y(features)19 b(including)f(functions,)g(methods,)h(properties,)f (class)j(methods,)e(static)h(methods,)f(and)g(reference)f(to)i(super)g (classes.)0 4642 y Fr(dictionary)40 b Fs(An)23 b(associati)n(v)o(e)h (array)-5 b(,)22 b(where)h(arbitrary)f(k)o(e)o(ys)h(are)h(mapped)e(to)i (v)n(alues.)34 b(The)23 b(use)h(of)f Fp(dict)h Fs(much)e(resembles)h (that)208 4741 y(for)h Fp(list)p Fs(,)i(b)n(ut)f(the)g(k)o(e)o(ys)f (can)h(be)g(an)o(y)f(object)h(with)g(a)g Fp(__hash__\(\))f Fs(function,)g(not)h(just)h(inte)o(gers)e(starting)g(from)g(zero.)208 4841 y(Called)c(a)h(hash)f(in)g(Perl.)0 5001 y Fr(duck-typing)41 b Fs(Pythonic)26 b(programming)e(style)k(that)f(determines)f(an)i (object')-5 b(s)27 b(type)g(by)g(inspection)f(of)h(its)i(method)d(or)h (attrib)n(ute)208 5101 y(signature)h(rather)g(than)h(by)g(e)o(xplicit)g (relationship)e(to)j(some)f(type)g(object)g(\("If)f(it)i(looks)f(lik)o (e)h(a)f(duck)f(and)h(quacks)g(lik)o(e)g(a)208 5201 y(duck,)17 b(it)j(must)f(be)g(a)g(duck."\))e(By)j(emphasizing)d(interf)o(aces)h (rather)g(than)g(speci\002c)h(types,)g(well-designed)e(code)i(impro)o (v)o(es)e(its)208 5300 y(\003e)o(xibility)h(by)g(allo)n(wing)g (polymorphic)e(substitution.)23 b(Duck-typing)16 b(a)n(v)n(oids)j (tests)h(using)f Fp(type\(\))f Fs(or)g Fp(isinstance\(\))p Fs(.)208 5400 y(Instead,)h(it)i(typically)e(emplo)o(ys)g Fp(hasattr\(\))g Fs(tests)j(or)e Fq(EAFP)f Fs(programming.)p 0 5549 3901 4 v 3762 5649 a Fo(115)p eop end %%Page: 116 124 TeXDict begin 116 123 bop 0 83 a Fr(EAFP)41 b Fs(Easier)28 b(to)g(ask)g(for)f(for)o(gi)n(v)o(eness)e(than)j(permission.)46 b(This)28 b(common)e(Python)h(coding)f(style)i(assumes)g(the)g(e)o (xistence)f(of)208 183 y(v)n(alid)k(k)o(e)o(ys)h(or)g(attrib)n(utes)g (and)f(catches)h(e)o(xceptions)f(if)h(the)g(assumption)f(pro)o(v)o(es)g (f)o(alse.)61 b(This)32 b(clean)g(and)f(f)o(ast)i(style)g(is)208 282 y(characterized)23 b(by)h(the)h(presence)f(of)h(man)o(y)f Fp(try)h Fs(and)f Fp(except)h Fs(statements.)39 b(The)25 b(technique)e(contrasts)i(with)g(the)g Fq(LBYL)208 382 y Fs(style)20 b(that)g(is)i(common)c(in)i(man)o(y)f(other)h(languages)e (such)i(as)h(C.)0 548 y Fr(__futur)o(e__)38 b Fs(A)20 b(pseudo)e(module)g(which)h(programmers)d(can)j(use)h(to)f(enable)g(ne) n(w)g(language)f(features)g(which)h(are)g(not)g(compatible)208 648 y(with)h(the)g(current)f(interpreter)-5 b(.)24 b(F)o(or)c(e)o (xample,)f(the)h(e)o(xpression)e Fp(11/4)i Fs(currently)f(e)n(v)n (aluates)h(to)g Fp(2)p Fs(.)25 b(If)c(the)f(module)f(in)h(which)208 747 y(it)g(is)i(e)o(x)o(ecuted)c(had)i(enabled)f Fq(true)h(division)g Fs(by)f(e)o(x)o(ecuting:)444 880 y Fk(from)44 b(__future__)f(import)h (division)208 1152 y Fs(the)30 b(e)o(xpression)f Fp(11/4)h Fs(w)o(ould)g(e)n(v)n(aluate)g(to)g Fp(2.75)p Fs(.)56 b(By)31 b(importing)e(the)h Fp(__future__)g Fs(module)f(and)h(e)n(v)n (aluating)f(its)208 1252 y(v)n(ariables,)19 b(you)g(can)h(see)h(when)e (a)i(ne)n(w)f(feature)f(w)o(as)i(\002rst)g(added)e(to)h(the)h(language) d(and)i(when)f(it)i(will)g(become)e(the)h(def)o(ault:)444 1476 y Fk(>>>)44 b(import)g(__future__)444 1567 y(>>>)g (__future__.division)444 1659 y(_Feature\(\(2,)f(2,)h(0,)h('alpha',)e (2\),)h(\(3,)h(0,)f(0,)h('alpha',)e(0\),)i(8192\))0 1964 y Fr(generator)39 b Fs(A)27 b(function)d(that)h(returns)g(an)h (iterator)-5 b(.)41 b(It)26 b(looks)f(lik)o(e)h(a)h(normal)d(function)g (e)o(xcept)h(that)h(v)n(alues)f(are)h(returned)e(to)i(the)208 2064 y(caller)19 b(using)g(a)h Fp(yield)g Fs(statement)f(instead)h(of)f (a)h Fp(return)f Fs(statement.)25 b(Generator)18 b(functions)g(often)h (contain)g(one)g(or)g(more)208 2163 y Fp(for)i Fs(or)g Fp(while)g Fs(loops)g(that)g Fp(yield)g Fs(elements)h(back)e(to)i(the)f (caller)-5 b(.)29 b(The)21 b(function)f(e)o(x)o(ecution)f(is)j(stopped) f(at)h(the)f Fp(yield)208 2263 y Fs(k)o(e)o(yw)o(ord)e(\(returning)f (the)k(result\))e(and)h(is)h(resumed)e(there)g(when)h(the)g(ne)o(xt)f (element)h(is)h(requested)e(by)g(calling)h(the)g Fp(next\(\))208 2362 y Fs(method)d(of)i(the)g(returned)f(iterator)-5 b(.)0 2529 y Fr(generator)19 b(expr)o(ession)40 b Fs(An)25 b(e)o(xpression)f(that)h(returns)f(a)i(generator)-5 b(.)38 b(It)26 b(looks)e(lik)o(e)i(a)f(normal)f(e)o(xpression)g(follo)n(wed)f (by)i(a)h Fp(for)208 2628 y Fs(e)o(xpression)20 b(de\002ning)g(a)j (loop)e(v)n(ariable,)f(range,)h(and)h(an)f(optional)g Fp(if)h Fs(e)o(xpression.)28 b(The)21 b(combined)f(e)o(xpression)h (generates)208 2728 y(v)n(alues)e(for)h(an)g(enclosing)f(function:)444 2952 y Fk(>>>)44 b(sum\(i)848 2965 y(*)893 2952 y(i)g(for)g(i)h(in)f (range\(10\)\))402 b(#)45 b(sum)f(of)h(squares)e(0,)i(1,)f(4,)h(...)f (81)444 3043 y(285)0 3350 y Fr(GIL)e Fs(See)20 b Fq(global)f(interpr)m (eter)i(loc)n(k)p Fs(.)0 3516 y Fr(global)f(inter)o(pr)o(eter)f(lock)41 b Fs(The)26 b(lock)f(used)h(by)f(Python)g(threads)g(to)h(assure)g(that) g(only)f(one)g(thread)g(can)h(be)g(run)f(at)h(a)g(time.)42 b(This)208 3615 y(simpli\002es)20 b(Python)e(by)i(assuring)f(that)h(no) f(tw)o(o)h(processes)f(can)h(access)g(the)g(same)g(memory)e(at)i(the)g (same)g(time.)25 b(Locking)18 b(the)208 3715 y(entire)j(interpreter)g (mak)o(es)h(it)h(easier)f(for)f(the)h(interpreter)f(to)h(be)g (multi-threaded,)e(at)j(the)f(e)o(xpense)f(of)g(some)h(parallelism)g (on)208 3814 y(multi-processor)17 b(machines.)24 b(Ef)n(forts)18 b(ha)n(v)o(e)h(been)g(made)g(in)g(the)h(past)g(to)f(create)g(a)h (\223free-threaded\224)d(interpreter)h(\(one)g(which)208 3914 y(locks)i(shared)f(data)h(at)h(a)f(much)g(\002ner)g (granularity\),)d(b)n(ut)j(performance)d(suf)n(fered)i(in)h(the)h (common)d(single-processor)g(case.)0 4080 y Fr(IDLE)42 b Fs(An)23 b(Inte)o(grated)e(De)n(v)o(elopment)g(En)m(vironment)f(for)i (Python.)32 b(IDLE)22 b(is)i(a)g(basic)f(editor)f(and)g(interpreter)g (en)m(vironment)d(that)208 4180 y(ships)f(with)f(the)h(standard)f (distrib)n(ution)f(of)i(Python.)23 b(Good)16 b(for)h(be)o(ginners,)f (it)j(also)f(serv)o(es)f(as)i(clear)e(e)o(xample)g(code)g(for)g(those) 208 4279 y(w)o(anting)i(to)h(implement)f(a)i(moderately)d (sophisticated,)h(multi-platform)f(GUI)i(application.)0 4445 y Fr(immutable)41 b Fs(An)21 b(object)f(with)h(\002x)o(ed)g(v)n (alue.)26 b(Immutable)19 b(objects)h(are)h(numbers,)e(strings)i(or)g (tuples)f(\(and)g(more\).)26 b(Such)20 b(an)h(object)208 4545 y(cannot)g(be)i(altered.)33 b(A)23 b(ne)n(w)g(object)f(has)h(to)g (be)g(created)f(if)i(a)f(dif)n(ferent)e(v)n(alue)h(has)i(to)f(be)f (stored.)33 b(The)o(y)22 b(play)g(an)h(important)208 4645 y(role)c(in)i(places)f(where)g(a)g(constant)g(hash)g(v)n(alue)f (is)i(needed,)e(for)g(e)o(xample)g(as)i(a)g(k)o(e)o(y)e(in)i(a)f (dictionary)-5 b(.)0 4811 y Fr(integer)20 b(di)o(vision)41 b Fs(Mathematical)20 b(di)n(vision)g(discarding)g(an)o(y)g(remainder)-5 b(.)25 b(F)o(or)c(e)o(xample,)e(the)i(e)o(xpression)e Fp(11/4)i Fs(currently)e(e)n(v)n(al-)208 4910 y(uates)27 b(to)f Fp(2)i Fs(in)f(contrast)f(to)h(the)g Fp(2.75)f Fs(returned)f(by)i(\003oat)g(di)n(vision.)43 b(Also)28 b(called)e Fq(\003oor)g(division)p Fs(.)45 b(When)26 b(di)n(viding)f(tw)o(o)208 5010 y(inte)o(gers)19 b(the)h(outcome)f (will)i(al)o(w)o(ays)g(be)g(another)d(inte)o(ger)i(\(ha)n(ving)f(the)h (\003oor)g(function)e(applied)i(to)g(it\).)26 b(Ho)n(we)n(v)o(er)m(,)18 b(if)j(one)f(of)208 5110 y(the)g(operands)e(is)k(another)c(numeric)h (type)h(\(such)g(as)h(a)g Fp(float)p Fs(\),)e(the)h(result)h(will)g(be) f(coerced)f(\(see)h Fq(coer)m(cion)p Fs(\))f(to)i(a)f(common)208 5209 y(type.)42 b(F)o(or)26 b(e)o(xample,)g(an)g(inte)o(ger)f(di)n (vided)f(by)i(a)h(\003oat)f(will)h(result)f(in)g(a)h(\003oat)f(v)n (alue,)h(possibly)f(with)g(a)g(decimal)g(fraction.)208 5309 y(Inte)o(ger)18 b(di)n(vision)h(can)h(be)h(forced)d(by)i(using)g (the)g Fp(//)g Fs(operator)f(instead)h(of)g(the)g Fp(/)g Fs(operator)-5 b(.)24 b(See)d(also)f Fq(__futur)m(e__)p Fs(.)p 0 5549 3901 4 v 0 5649 a Fo(116)2939 b(Appendix)25 b(D)-6 b(.)46 b(Glossar)r(y)p eop end %%Page: 117 125 TeXDict begin 117 124 bop 0 83 a Fr(interacti)o(v)o(e)40 b Fs(Python)23 b(has)h(an)g(interacti)n(v)o(e)e(interpreter)g(which)i (means)f(that)h(you)f(can)h(try)g(out)f(things)h(and)f(immediately)f (see)j(their)208 183 y(results.)k(Just)22 b(launch)f Fp(python)g Fs(with)g(no)h(ar)o(guments)d(\(possibly)i(by)g(selecting)g (it)h(from)f(your)f(computer')-5 b(s)20 b(main)i(menu\).)27 b(It)208 282 y(is)21 b(a)f(v)o(ery)f(po)n(werful)g(w)o(ay)h(to)g(test)h (out)f(ne)n(w)g(ideas)h(or)f(inspect)f(modules)h(and)f(packages)g (\(remember)f Fp(help\(x\))p Fs(\).)0 435 y Fr(inter)o(pr)o(eted)40 b Fs(Python)19 b(is)j(an)e(interpreted)e(language,)g(as)j(opposed)e(to) h(a)h(compiled)e(one.)24 b(This)d(means)e(that)i(the)f(source)f (\002les)j(can)e(be)208 535 y(run)d(directly)g(without)h(\002rst)h (creating)e(an)h(e)o(x)o(ecutable)e(which)i(is)h(then)f(run.)23 b(Interpreted)16 b(languages)h(typically)g(ha)n(v)o(e)h(a)g(shorter)208 635 y(de)n(v)o(elopment/deb)n(ug)g(c)o(ycle)k(than)g(compiled)f(ones,)i (though)e(their)h(programs)f(generally)g(also)i(run)f(more)f(slo)n(wly) -5 b(.)32 b(See)23 b(also)208 734 y Fq(inter)o(active)p Fs(.)0 887 y Fr(iterable)41 b Fs(A)17 b(container)f(object)h(capable)f (of)h(returning)e(its)k(members)d(one)h(at)g(a)h(time.)24 b(Examples)17 b(of)g(iterables)g(include)f(all)i(sequence)208 987 y(types)h(\(such)h(as)g Fp(list)p Fs(,)g Fp(str)p Fs(,)g(and)f Fp(tuple)p Fs(\))g(and)h(some)g(non-sequence)d(types)i (lik)o(e)i Fp(dict)e Fs(and)h Fp(file)g Fs(and)f(objects)h(of)f(an)o(y) 208 1087 y(classes)24 b(you)e(de\002ne)h(with)h(an)f Fp(__iter__\(\))f Fs(or)h Fp(__getitem__\(\))f Fs(method.)33 b(Iterables)22 b(can)h(be)h(used)f(in)g(a)h Fp(for)f Fs(loop)208 1186 y(and)f(in)g(man)o(y)g(other)g(places)g(where)g(a)i (sequence)d(is)j(needed)d(\()p Fp(zip\(\))p Fs(,)h Fp(map\(\))p Fs(,)h(...\).)32 b(When)22 b(an)h(iterable)f(object)g(is)i(passed)208 1286 y(as)c(an)g(ar)o(gument)d(to)j(the)g(b)n(uiltin)g(function)e Fp(iter\(\))p Fs(,)h(it)h(returns)f(an)h(iterator)f(for)g(the)h (object.)25 b(This)20 b(iterator)f(is)h(good)f(for)g(one)208 1386 y(pass)f(o)o(v)o(er)f(the)h(set)h(of)f(v)n(alues.)24 b(When)18 b(using)g(iterables,)g(it)h(is)g(usually)f(not)g(necessary)f (to)i(call)f Fp(iter\(\))g Fs(or)g(deal)g(with)g(iterator)208 1485 y(objects)i(yourself.)j(The)d Fp(for)g Fs(statement)g(does)g(that) h(automatically)e(for)g(you,)g(creating)g(a)i(temporary)d(unnamed)g(v)n (ariable)h(to)208 1585 y(hold)g(the)h(iterator)g(for)f(the)h(duration)f (of)h(the)g(loop.)k(See)d(also)f Fq(iter)o(ator)p Fs(,)g Fq(sequence)p Fs(,)f(and)h Fq(g)o(ener)o(ator)p Fs(.)0 1738 y Fr(iterator)39 b Fs(An)24 b(object)f(representing)e(a)j(stream)f (of)g(data.)34 b(Repeated)23 b(calls)h(to)g(the)f(iterator')-5 b(s)23 b Fp(next\(\))g Fs(method)f(return)g(successi)n(v)o(e)208 1838 y(items)i(in)g(the)g(stream.)37 b(When)24 b(no)f(more)h(data)g(is) h(a)n(v)n(ailable)e(a)i Fp(StopIteration)d Fs(e)o(xception)g(is)j (raised)f(instead.)36 b(At)25 b(this)208 1937 y(point,)19 b(the)i(iterator)f(object)g(is)h(e)o(xhausted)e(and)h(an)o(y)g(further) f(calls)j(to)e(its)i Fp(next\(\))e Fs(method)f(just)j(raise)e Fp(StopIteration)208 2037 y Fs(again.)46 b(Iterators)27 b(are)h(required)e(to)i(ha)n(v)o(e)f(an)h Fp(__iter__\(\))e Fs(method)h(that)g(returns)g(the)h(iterator)f(object)g(itself)i(so)f(e) n(v)o(ery)208 2136 y(iterator)15 b(is)j(also)f(iterable)f(and)g(may)g (be)h(used)f(in)h(most)f(places)h(where)f(other)g(iterables)g(are)h (accepted.)22 b(One)17 b(notable)e(e)o(xception)208 2236 y(is)j(code)e(that)h(attempts)g(multiple)g(iteration)f(passes.)25 b(A)17 b(container)f(object)g(\(such)h(as)h(a)f Fp(list)p Fs(\))g(produces)e(a)j(fresh)e(ne)n(w)h(iterator)208 2336 y(each)j(time)h(you)f(pass)i(it)f(to)g(the)g Fp(iter\(\))g Fs(function)e(or)i(use)g(it)g(in)g(a)h Fp(for)f Fs(loop.)26 b(Attempting)20 b(this)h(with)g(an)g(iterator)f(will)i(just)208 2435 y(return)k(the)i(same)f(e)o(xhausted)f(iterator)h(object)g(used)g (in)h(the)g(pre)n(vious)e(iteration)h(pass,)i(making)d(it)j(appear)d (lik)o(e)i(an)f(empty)208 2535 y(container)-5 b(.)0 2688 y Fr(LBYL)43 b Fs(Look)25 b(before)h(you)f(leap.)45 b(This)27 b(coding)e(style)j(e)o(xplicitly)d(tests)j(for)e(pre-conditions)e (before)i(making)f(calls)j(or)e(lookups.)208 2788 y(This)20 b(style)h(contrasts)e(with)i(the)f Fq(EAFP)g Fs(approach)e(and)h(is)i (characterized)e(by)g(the)i(presence)e(of)h(man)o(y)f Fp(if)h Fs(statements.)0 2941 y Fr(list)h(compr)o(ehension)41 b Fs(A)27 b(compact)f(w)o(ay)i(to)f(process)g(all)h(or)f(a)h(subset)f (of)g(elements)g(in)g(a)h(sequence)e(and)h(return)f(a)i(list)g(with)g (the)208 3040 y(results.)61 b Fp(result)49 b(=)h(["0x\04502x")e(\045x)h (for)h(x)f(in)h(range\(256\))e(if)h(x)h(\0452)f(==)h(0])32 b Fs(generates)g(a)g(list)i(of)208 3140 y(strings)19 b(containing)e(he)o(x)i(numbers)f(\(0x..\))23 b(that)c(are)g(e)n(v)o (en)g(and)f(in)i(the)f(range)g(from)f(0)h(to)h(255.)j(The)c Fp(if)h Fs(clause)f(is)h(optional.)k(If)208 3240 y(omitted,)19 b(all)i(elements)e(in)i Fp(range\(256\))e Fs(are)h(processed.)0 3393 y Fr(mapping)41 b Fs(A)34 b(container)f(object)g(\(such)g(as)h Fp(dict)p Fs(\))g(that)f(supports)g(arbitrary)f(k)o(e)o(y)h(lookups)f (using)i(the)f(special)h(method)f Fp(__-)208 3492 y(getitem__\(\))p Fs(.)0 3646 y Fr(metaclass)41 b Fs(The)26 b(class)h(of)g(a)f(class.)45 b(Class)28 b(de\002nitions)e(create)g(a)h(class)g(name,)h(a)e(class)i (dictionary)-5 b(,)26 b(and)f(a)i(list)h(of)e(base)h(classes.)208 3745 y(The)j(metaclass)i(is)g(responsible)d(for)i(taking)f(those)h (three)f(ar)o(guments)f(and)i(creating)f(the)h(class.)58 b(Most)31 b(object)g(oriented)208 3845 y(programming)20 b(languages)i(pro)o(vide)f(a)j(def)o(ault)f(implementation.)32 b(What)24 b(mak)o(es)g(Python)e(special)i(is)g(that)g(it)g(is)g (possible)f(to)208 3944 y(create)f(custom)g(metaclasses.)32 b(Most)23 b(users)g(ne)n(v)o(er)e(need)h(this)h(tool,)g(b)n(ut)f(when)g (the)h(need)f(arises,)h(metaclasses)g(can)g(pro)o(vide)208 4044 y(po)n(werful,)j(ele)o(gant)f(solutions.)44 b(The)o(y)26 b(ha)n(v)o(e)g(been)g(used)h(for)f(logging)f(attrib)n(ute)h(access,)j (adding)c(thread-safety)-5 b(,)26 b(tracking)208 4144 y(object)19 b(creation,)g(implementing)f(singletons,)h(and)h(man)o(y)f (other)g(tasks.)0 4297 y Fr(mutable)41 b Fs(Mutable)20 b(objects)g(can)f(change)g(their)h(v)n(alue)g(b)n(ut)g(k)o(eep)g(their) g Fp(id\(\))p Fs(.)k(See)d(also)f Fq(immutable)p Fs(.)0 4450 y Fr(namespace)41 b Fs(The)23 b(place)g(where)f(a)i(v)n(ariable)e (is)i(stored.)33 b(Namespaces)23 b(are)g(implemented)e(as)j (dictionaries.)32 b(There)23 b(are)g(the)g(local,)208 4549 y(global)18 b(and)h(b)n(uiltin)g(namespaces)f(as)i(well)g(as)f (nested)g(namespaces)g(in)g(objects)g(\(in)g(methods\).)k(Namespaces)c (support)f(mod-)208 4649 y(ularity)f(by)g(pre)n(v)o(enting)e(naming)h (con\003icts.)24 b(F)o(or)17 b(instance,)h(the)g(functions)e Fp(__builtin__.open\(\))f Fs(and)i Fp(os.open\(\))208 4749 y Fs(are)22 b(distinguished)e(by)i(their)g(namespaces.)30 b(Namespaces)22 b(also)h(aid)f(readability)f(and)g(maintainability)g (by)h(making)f(it)i(clear)208 4848 y(which)31 b(module)g(implements)g (a)h(function.)59 b(F)o(or)32 b(instance,)i(writing)e Fp(random.seed\(\))e Fs(or)i Fp(itertools.izip\(\))208 4948 y Fs(mak)o(es)20 b(it)h(clear)f(that)g(those)g(functions)f(are)h (implemented)e(by)i(the)g Fp(random)g Fs(and)f Fp(itertools)g Fs(modules)g(respecti)n(v)o(ely)-5 b(.)0 5101 y Fr(nested)21 b(scope)41 b Fs(The)30 b(ability)g(to)h(refer)f(to)g(a)h(v)n(ariable)f (in)g(an)h(enclosing)e(de\002nition.)55 b(F)o(or)30 b(instance,)i(a)f (function)e(de\002ned)h(inside)208 5201 y(another)23 b(function)h(can)h(refer)g(to)g(v)n(ariables)g(in)g(the)h(outer)e (function.)39 b(Note)25 b(that)h(nested)f(scopes)g(w)o(ork)g(only)g (for)f(reference)208 5300 y(and)g(not)h(for)g(assignment)f(which)h (will)h(al)o(w)o(ays)f(write)h(to)f(the)g(innermost)f(scope.)40 b(In)25 b(contrast,)g(local)g(v)n(ariables)g(both)f(read)208 5400 y(and)19 b(write)i(in)f(the)g(innermost)f(scope.)24 b(Lik)o(e)n(wise,)c(global)g(v)n(ariables)f(read)h(and)f(write)i(to)f (the)g(global)g(namespace.)p 0 5549 3901 4 v 3762 5649 a Fo(117)p eop end %%Page: 118 126 TeXDict begin 118 125 bop 0 83 a Fr(new-style)20 b(class)41 b Fs(An)o(y)25 b(class)i(that)f(inherits)f(from)g Fp(object)p Fs(.)41 b(This)26 b(includes)f(all)i(b)n(uilt-in)e(types)h(lik)o(e)g Fp(list)f Fs(and)g Fp(dict)p Fs(.)42 b(Only)208 183 y(ne)n(w-style)29 b(classes)j(can)e(use)h(Python')-5 b(s)30 b(ne)n(wer)m(,)i(v)o (ersatile)e(features)g(lik)o(e)h Fp(__slots__)p Fs(,)g(descriptors,)h (properties,)g Fp(__-)208 282 y(getattribute__\(\))p Fs(,)17 b(class)k(methods,)e(and)h(static)h(methods.)0 448 y Fr(Python3000)38 b Fs(A)21 b(mythical)e(p)o(ython)g(release,)h (not)g(required)e(to)i(be)h(backw)o(ard)d(compatible,)h(with)h (telepathic)g(interf)o(ace.)0 614 y Fr(__slots__)39 b Fs(A)21 b(declaration)d(inside)i(a)h Fq(ne)o(w-style)f(class)g Fs(that)g(sa)n(v)o(es)h(memory)d(by)i(pre-declaring)d(space)j(for)g (instance)f(attrib)n(utes)h(and)208 714 y(eliminating)27 b(instance)i(dictionaries.)50 b(Though)26 b(popular)m(,)j(the)g (technique)e(is)j(some)n(what)e(trick)o(y)g(to)h(get)g(right)f(and)g (is)i(best)208 814 y(reserv)o(ed)18 b(for)i(rare)g(cases)h(where)e (there)h(are)g(lar)o(ge)f(numbers)g(of)h(instances)g(in)g(a)h (memory-critical)c(application.)0 980 y Fr(sequence)42 b Fs(An)25 b Fq(iter)o(able)g Fs(which)g(supports)f(ef)n(\002cient)h (element)g(access)h(using)f(inte)o(ger)f(indices)i(via)f(the)h Fp(__getitem__\(\))d Fs(and)208 1079 y Fp(__len__\(\))16 b Fs(special)h(methods.)23 b(Some)16 b(b)n(uilt-in)h(sequence)f(types)h (are)g Fp(list)p Fs(,)g Fp(str)p Fs(,)h Fp(tuple)p Fs(,)f(and)f Fp(unicode)p Fs(.)23 b(Note)17 b(that)208 1179 y Fp(dict)k Fs(also)i(supports)e Fp(__getitem__\(\))f Fs(and)h Fp(__len__\(\))p Fs(,)h(b)n(ut)g(is)h(considered)d(a)i(mapping)e(rather)i(than)f(a)i (sequence)208 1279 y(because)c(the)h(lookups)f(use)h(arbitrary)f Fq(immutable)g Fs(k)o(e)o(ys)h(rather)f(than)h(inte)o(gers.)0 1445 y Fr(Zen)h(of)f(Python)40 b Fs(Listing)23 b(of)h(Python)e(design)g (principles)h(and)g(philosophies)e(that)j(are)f(helpful)f(in)i (understanding)c(and)j(using)g(the)208 1544 y(language.)g(The)d (listing)g(can)g(be)g(found)f(by)g(typing)g(\223)p Fp(import)49 b(this)p Fs(\224)20 b(at)g(the)h(interacti)n(v)o(e)d(prompt.)p 0 5549 3901 4 v 0 5649 a Fo(118)2939 b(Appendix)25 b(D)-6 b(.)46 b(Glossar)r(y)p eop end %%Page: 119 127 TeXDict begin 119 126 bop 3269 562 a Fy(INDEX)0 1314 y Fl(Symbols)0 1438 y Fs(...,)20 b(115)0 1537 y(\273>,)g(115)0 1637 y Fp(__all__)p Fs(,)f(48)0 1737 y Fp(__builtin__)g Fs(\(b)n(uilt-in)g(module\),)f(45)0 1836 y(__future__,)f(116)0 1936 y(__slots__,)i(118)0 2115 y Fl(A)0 2239 y Fp(append\(\))g Fs(\(list)i(method\),)d(29)0 2427 y Fl(B)0 2550 y Fs(BDFL,)j(115)0 2650 y(byte)f(code,)f(115)0 2844 y Fl(C)0 2968 y Fs(classic)i(class,)g (115)0 3067 y(coercion,)d(115)0 3167 y Fp(compileall)h Fs(\(standard)g(module\),)f(44)0 3266 y(comple)o(x)g(number)m(,)g(115)0 3366 y Fp(count\(\))h Fs(\(list)i(method\),)e(29)0 3554 y Fl(D)0 3678 y Fs(descriptor)m(,)f(115)0 3777 y(dictionary)-5 b(,)18 b(115)0 3877 y(docstrings,)h(22,)g(27)0 3977 y(documentation)e (strings,)j(22,)g(27)0 4076 y(duck-typing,)d(115)0 4268 y Fl(E)0 4391 y Fs(EAFP)-9 b(,)20 b(115)0 4491 y(en)m(vironment)d(v)n (ariables)166 4590 y(P)-8 b(A)f(TH,)20 b(5,)g(43)166 4690 y(PYTHONP)-8 b(A)f(TH,)20 b(43,)g(44)166 4789 y(PYTHONST)-8 b(AR)j(TUP,)21 b(6,)f(94)0 4889 y Fp(extend\(\))f Fs(\(list)i (method\),)d(29)0 5077 y Fl(F)0 5201 y Fs(\002le)166 5300 y(object,)h(54)0 5400 y Fp(for)2158 1309 y Fs(statement,)g(19)1992 1487 y Fl(G)1992 1610 y Fs(generator)m(,)e(116)1992 1709 y(generator)h(e)o(xpression,)g(116)1992 1809 y(GIL,)h(116)1992 1909 y(global)g(interpreter)f(lock,)i(116)1992 2089 y Fl(H)1992 2212 y Fp(help\(\))f Fs(\(b)n(uilt-in)g(function\),)f(77)1992 2390 y Fl(I)1992 2513 y Fs(IDLE,)h(116)1992 2612 y(immutable,)f(116) 1992 2712 y Fp(index\(\))h Fs(\(list)i(method\),)d(29)1992 2812 y Fp(insert\(\))h Fs(\(list)i(method\),)d(29)1992 2911 y(inte)o(ger)h(di)n(vision,)g(116)1992 3011 y(interacti)n(v)o(e,)f (116)1992 3111 y(interpreted,)g(117)1992 3210 y(iterable,)h(117)1992 3310 y(iterator)m(,)f(117)1992 3478 y Fl(L)1992 3602 y Fs(LBYL,)i(117)1992 3701 y(list)h(comprehension,)c(117)1992 3882 y Fl(M)1992 4005 y Fs(mapping,)h(117)1992 4104 y(metaclass,)i(117) 1992 4204 y(method)2158 4304 y(object,)f(69)1992 4403 y(module)2158 4503 y(search)g(path,)h(43)1992 4603 y(mutable,)f(117) 1992 4771 y Fl(N)1992 4894 y Fs(namespace,)f(117)1992 4994 y(nested)h(scope,)h(117)1992 5094 y(ne)n(w-style)f(class,)i(117) 1992 5277 y Fl(O)1992 5400 y Fs(object)p 0 5549 3901 4 v 3762 5649 a Fo(119)p eop end %%Page: 120 128 TeXDict begin 120 127 bop 166 83 a Fs(\002le,)21 b(54)166 183 y(method,)e(69)0 282 y Fp(open\(\))h Fs(\(b)n(uilt-in)f (function\),)f(54)0 474 y Fl(P)0 597 y Fs(P)-8 b(A)f(TH,)20 b(5,)g(43)0 697 y(path)166 796 y(module)f(search,)g(43)0 896 y Fp(pickle)h Fs(\(standard)e(module\),)h(56)0 995 y Fp(pop\(\))h Fs(\(list)h(method\),)d(29)0 1095 y(Python3000,)f(118)0 1195 y(PYTHONP)-8 b(A)f(TH,)20 b(43,)g(44)0 1294 y(PYTHONST)-8 b(AR)j(TUP)c(,)20 b(6,)g(94)0 1477 y Fl(R)0 1600 y Fp(readline)f Fs(\(b)n(uilt-in)g(module\),)g(94)0 1700 y Fp(remove\(\))g Fs(\(list)i(method\),)d(29)0 1799 y Fp(reverse\(\))h Fs(\(list)i(method\),)d(29)0 1899 y Fp(rlcompleter)h Fs(\(standard)f(module\),)h(94)0 2093 y Fl(S)0 2216 y Fs(search)166 2316 y(path,)h(module,)e(43)0 2416 y(sequence,)h(118)0 2515 y Fp(sort\(\))h Fs(\(list)h(method\),)d(29)0 2615 y(statement)166 2715 y Fp(for)p Fs(,)i(19)0 2814 y Fp(string)g Fs(\(standard)e(module\),)h(51)0 2914 y(strings,)h(documentation,)d (22,)i(27)0 3013 y Fp(sys)h Fs(\(standard)f(module\),)f(44)0 3205 y Fl(U)0 3328 y Fp(unicode\(\))h Fs(\(b)n(uilt-in)g(function\),)f (14)0 3519 y Fl(Z)0 3643 y Fs(Zen)i(of)g(Python,)f(118)p 0 5549 3901 4 v 0 5649 a Fo(120)3561 b(Inde)n(x)p eop end %%Trailer userdict /end-hook known{end-hook}if %%EOF .