(lp1 (ccopy_reg _reconstructor p2 (cpygments.token _TokenType p3 c__builtin__ tuple p4 (S'Generic' p5 S'Heading' p6 ttRp7 (dp8 S'subtypes' p9 c__builtin__ set p10 ((ltRp11 sS'parent' p12 g2 (g3 g4 (g5 ttRp13 (dp14 S'Prompt' p15 g2 (g3 g4 (g5 g15 ttRp16 (dp17 g9 g10 ((ltRp18 sg12 g13 sbsg12 g2 (g3 g4 (ttRp19 (dp20 S'Comment' p21 g2 (g3 g4 (g21 ttRp22 (dp23 g12 g19 sS'Preproc' p24 g2 (g3 g4 (g21 g24 ttRp25 (dp26 g9 g10 ((ltRp27 sg12 g22 sbsS'Single' p28 g2 (g3 g4 (g21 g28 ttRp29 (dp30 g9 g10 ((ltRp31 sg12 g22 sbsS'Multiline' p32 g2 (g3 g4 (g21 g32 ttRp33 (dp34 g9 g10 ((ltRp35 sg12 g22 sbsg9 g10 ((lp36 g2 (g3 g4 (g21 S'Special' p37 ttRp38 (dp39 g9 g10 ((ltRp40 sg12 g22 sbag25 ag29 ag33 atRp41 sg37 g38 sbsS'Name' p42 g2 (g3 g4 (g42 ttRp43 (dp44 S'Function' p45 g2 (g3 g4 (g42 g45 ttRp46 (dp47 g9 g10 ((ltRp48 sg12 g43 sbsS'Exception' p49 g2 (g3 g4 (g42 g49 ttRp50 (dp51 g9 g10 ((ltRp52 sg12 g43 sbsS'Tag' p53 g2 (g3 g4 (g42 g53 ttRp54 (dp55 g9 g10 ((ltRp56 sg12 g43 sbsS'Constant' p57 g2 (g3 g4 (g42 g57 ttRp58 (dp59 g9 g10 ((ltRp60 sg12 g43 sbsg12 g19 sS'Pseudo' p61 g2 (g3 g4 (g42 g61 ttRp62 (dp63 g9 g10 ((ltRp64 sg12 g43 sbsS'Attribute' p65 g2 (g3 g4 (g42 g65 ttRp66 (dp67 g9 g10 ((ltRp68 sg12 g43 sbsS'Label' p69 g2 (g3 g4 (g42 g69 ttRp70 (dp71 g9 g10 ((ltRp72 sg12 g43 sbsS'Blubb' p73 g2 (g3 g4 (g42 g73 ttRp74 (dp75 g9 g10 ((ltRp76 sg12 g43 sbsS'Entity' p77 g2 (g3 g4 (g42 g77 ttRp78 (dp79 g9 g10 ((ltRp80 sg12 g43 sbsS'Builtin' p81 g2 (g3 g4 (g42 g81 ttRp82 (dp83 g9 g10 ((lp84 g2 (g3 g4 (g42 g81 g61 ttRp85 (dp86 g9 g10 ((ltRp87 sg12 g82 sbatRp88 sg61 g85 sg12 g43 sbsS'Other' p89 g2 (g3 g4 (g42 g89 ttRp90 (dp91 g9 g10 ((ltRp92 sg12 g43 sbsS'Identifier' p93 g2 (g3 g4 (g42 g93 ttRp94 (dp95 g9 g10 ((ltRp96 sg12 g43 sbsS'Variable' p97 g2 (g3 g4 (g42 g97 ttRp98 (dp99 g12 g43 sS'Global' p100 g2 (g3 g4 (g42 g97 g100 ttRp101 (dp102 g9 g10 ((ltRp103 sg12 g98 sbsS'Instance' p104 g2 (g3 g4 (g42 g97 g104 ttRp105 (dp106 g9 g10 ((ltRp107 sg12 g98 sbsS'Anonymous' p108 g2 (g3 g4 (g42 g97 g108 ttRp109 (dp110 g9 g10 ((ltRp111 sg12 g98 sbsg9 g10 ((lp112 g109 ag105 ag101 ag2 (g3 g4 (g42 g97 S'Class' p113 ttRp114 (dp115 g9 g10 ((ltRp116 sg12 g98 sbatRp117 sg113 g114 sbsg9 g10 ((lp118 g2 (g3 g4 (g42 S'Decorator' p119 ttRp120 (dp121 g9 g10 ((ltRp122 sg12 g43 sbag66 ag58 ag62 ag2 (g3 g4 (g42 S'Namespace' p123 ttRp124 (dp125 g9 g10 ((ltRp126 sg12 g43 sbag94 ag82 ag98 ag90 ag74 ag78 ag46 ag2 (g3 g4 (g42 S'Property' p127 ttRp128 (dp129 g9 g10 ((ltRp130 sg12 g43 sbag70 ag54 ag50 ag2 (g3 g4 (g42 g113 ttRp131 (dp132 g9 g10 ((ltRp133 sg12 g43 sbatRp134 sg127 g128 sg113 g131 sg119 g120 sg123 g124 sbsS'Keyword' p135 g2 (g3 g4 (g135 ttRp136 (dp137 g57 g2 (g3 g4 (g135 g57 ttRp138 (dp139 g9 g10 ((ltRp140 sg12 g136 sbsg12 g19 sg123 g2 (g3 g4 (g135 g123 ttRp141 (dp142 g9 g10 ((ltRp143 sg12 g136 sbsg61 g2 (g3 g4 (g135 g61 ttRp144 (dp145 g9 g10 ((ltRp146 sg12 g136 sbsS'Reserved' p147 g2 (g3 g4 (g135 g147 ttRp148 (dp149 g9 g10 ((ltRp150 sg12 g136 sbsS'Declaration' p151 g2 (g3 g4 (g135 g151 ttRp152 (dp153 g9 g10 ((ltRp154 sg12 g136 sbsg97 g2 (g3 g4 (g135 g97 ttRp155 (dp156 g9 g10 ((ltRp157 sg12 g136 sbsg9 g10 ((lp158 g138 ag148 ag2 (g3 g4 (g135 S'Type' p159 ttRp160 (dp161 g9 g10 ((ltRp162 sg12 g136 sbag152 ag155 ag141 ag144 atRp163 sg159 g160 sbsg5 g13 sS'Text' p164 g2 (g3 g4 (g164 ttRp165 (dp166 g9 g10 ((lp167 g2 (g3 g4 (g164 S'Symbol' p168 ttRp169 (dp170 g9 g10 ((ltRp171 sg12 g165 sbag2 (g3 g4 (g164 S'Whitespace' p172 ttRp173 (dp174 g9 g10 ((ltRp175 sg12 g165 sbatRp176 sg168 g169 sg172 g173 sg12 g19 sbsS'Punctuation' p177 g2 (g3 g4 (g177 ttRp178 (dp179 g9 g10 ((lp180 g2 (g3 g4 (g177 S'Indicator' p181 ttRp182 (dp183 g9 g10 ((ltRp184 sg12 g178 sbatRp185 sg181 g182 sg12 g19 sbsS'Token' p186 g19 sS'Number' p187 g2 (g3 g4 (S'Literal' p188 g187 ttRp189 (dp190 S'Bin' p191 g2 (g3 g4 (g188 g187 g191 ttRp192 (dp193 g9 g10 ((ltRp194 sg12 g189 sbsS'Binary' p195 g2 (g3 g4 (g188 g187 g195 ttRp196 (dp197 g9 g10 ((ltRp198 sg12 g189 sbsg12 g2 (g3 g4 (g188 ttRp199 (dp200 S'String' p201 g2 (g3 g4 (g188 g201 ttRp202 (dp203 S'Regex' p204 g2 (g3 g4 (g188 g201 g204 ttRp205 (dp206 g9 g10 ((ltRp207 sg12 g202 sbsS'Interpol' p208 g2 (g3 g4 (g188 g201 g208 ttRp209 (dp210 g9 g10 ((ltRp211 sg12 g202 sbsS'Regexp' p212 g2 (g3 g4 (g188 g201 g212 ttRp213 (dp214 g9 g10 ((ltRp215 sg12 g202 sbsg12 g199 sS'Heredoc' p216 g2 (g3 g4 (g188 g201 g216 ttRp217 (dp218 g9 g10 ((ltRp219 sg12 g202 sbsS'Double' p220 g2 (g3 g4 (g188 g201 g220 ttRp221 (dp222 g9 g10 ((ltRp223 sg12 g202 sbsg168 g2 (g3 g4 (g188 g201 g168 ttRp224 (dp225 g9 g10 ((ltRp226 sg12 g202 sbsS'Escape' p227 g2 (g3 g4 (g188 g201 g227 ttRp228 (dp229 g9 g10 ((ltRp230 sg12 g202 sbsS'Character' p231 g2 (g3 g4 (g188 g201 g231 ttRp232 (dp233 g9 g10 ((ltRp234 sg12 g202 sbsS'Interp' p235 g2 (g3 g4 (g188 g201 g235 ttRp236 (dp237 g9 g10 ((ltRp238 sg12 g202 sbsS'Backtick' p239 g2 (g3 g4 (g188 g201 g239 ttRp240 (dp241 g9 g10 ((ltRp242 sg12 g202 sbsS'Char' p243 g2 (g3 g4 (g188 g201 g243 ttRp244 (dp245 g9 g10 ((ltRp246 sg12 g202 sbsg28 g2 (g3 g4 (g188 g201 g28 ttRp247 (dp248 g9 g10 ((ltRp249 sg12 g202 sbsg89 g2 (g3 g4 (g188 g201 g89 ttRp250 (dp251 g9 g10 ((ltRp252 sg12 g202 sbsS'Doc' p253 g2 (g3 g4 (g188 g201 g253 ttRp254 (dp255 g9 g10 ((ltRp256 sg12 g202 sbsg9 g10 ((lp257 g250 ag2 (g3 g4 (g188 g201 S'Atom' p258 ttRp259 (dp260 g9 g10 ((ltRp261 sg12 g202 sbag221 ag244 ag236 ag254 ag217 ag240 ag209 ag224 ag213 ag205 ag247 ag232 ag228 atRp262 sg258 g259 sbsg12 g19 sg187 g189 sS'Scalar' p263 g2 (g3 g4 (g188 g263 ttRp264 (dp265 g9 g10 ((lp266 g2 (g3 g4 (g188 g263 S'Plain' p267 ttRp268 (dp269 g9 g10 ((ltRp270 sg12 g264 sbatRp271 sg12 g199 sg267 g268 sbsg89 g2 (g3 g4 (g188 g89 ttRp272 (dp273 g9 g10 ((ltRp274 sg12 g199 sbsS'Date' p275 g2 (g3 g4 (g188 g275 ttRp276 (dp277 g9 g10 ((ltRp278 sg12 g199 sbsg9 g10 ((lp279 g276 ag202 ag272 ag189 ag264 atRp280 sbsS'Decimal' p281 g2 (g3 g4 (g188 g187 g281 ttRp282 (dp283 g9 g10 ((ltRp284 sg12 g189 sbsS'Float' p285 g2 (g3 g4 (g188 g187 g285 ttRp286 (dp287 g9 g10 ((ltRp288 sg12 g189 sbsS'Hex' p289 g2 (g3 g4 (g188 g187 g289 ttRp290 (dp291 g9 g10 ((ltRp292 sg12 g189 sbsS'Integer' p293 g2 (g3 g4 (g188 g187 g293 ttRp294 (dp295 g9 g10 ((lp296 g2 (g3 g4 (g188 g187 g293 S'Long' p297 ttRp298 (dp299 g9 g10 ((ltRp300 sg12 g294 sbatRp301 sg297 g298 sg12 g189 sbsS'Octal' p302 g2 (g3 g4 (g188 g187 g302 ttRp303 (dp304 g9 g10 ((ltRp305 sg12 g189 sbsg9 g10 ((lp306 g192 ag196 ag303 ag282 ag2 (g3 g4 (g188 g187 S'Oct' p307 ttRp308 (dp309 g9 g10 ((ltRp310 sg12 g189 sbag294 ag286 ag290 atRp311 sg307 g308 sbsg188 g199 sg89 g2 (g3 g4 (g89 ttRp312 (dp313 g9 g10 ((ltRp314 sg12 g19 sbsS'Error' p315 g2 (g3 g4 (g315 ttRp316 (dp317 g9 g10 ((ltRp318 sg12 g19 sbsS'Operator' p319 g2 (g3 g4 (g319 ttRp320 (dp321 g9 g10 ((lp322 g2 (g3 g4 (g319 S'Word' p323 ttRp324 (dp325 g9 g10 ((ltRp326 sg12 g320 sbatRp327 sg323 g324 sg12 g19 sbsg9 g10 ((lp328 g22 ag316 ag13 ag165 ag43 ag178 ag136 ag199 ag320 ag312 atRp329 sg201 g202 sbsS'Deleted' p330 g2 (g3 g4 (g5 g330 ttRp331 (dp332 g9 g10 ((ltRp333 sg12 g13 sbsS'Traceback' p334 g2 (g3 g4 (g5 g334 ttRp335 (dp336 g9 g10 ((ltRp337 sg12 g13 sbsS'Emph' p338 g2 (g3 g4 (g5 g338 ttRp339 (dp340 g9 g10 ((ltRp341 sg12 g13 sbsS'Output' p342 g2 (g3 g4 (g5 g342 ttRp343 (dp344 g9 g10 ((ltRp345 sg12 g13 sbsS'Subheading' p346 g2 (g3 g4 (g5 g346 ttRp347 (dp348 g9 g10 ((ltRp349 sg12 g13 sbsg315 g2 (g3 g4 (g5 g315 ttRp350 (dp351 g9 g10 ((ltRp352 sg12 g13 sbsg9 g10 ((lp353 g343 ag339 ag350 ag347 ag335 ag331 ag7 ag2 (g3 g4 (g5 S'Inserted' p354 ttRp355 (dp356 g9 g10 ((ltRp357 sg12 g13 sbag2 (g3 g4 (g5 S'Strong' p358 ttRp359 (dp360 g9 g10 ((ltRp361 sg12 g13 sbag16 atRp362 sg358 g359 sg354 g355 sg6 g7 sbsbVFunctional Programming HOWTO p363 tp364 a(g165 V\u000a tp365 a(g7 V================================ p366 tp367 a(g165 V\u000a tp368 a(g165 V\u000a tp369 a(g359 V**Version 0.30** p370 tp371 a(g165 V\u000a tp372 a(g165 V\u000a tp373 a(g165 V(This is a first draft. Please send comments/error p374 tp375 a(g165 V\u000a tp376 a(g165 Vreports/suggestions to amk@amk.ca. This URL is probably not going to p377 tp378 a(g165 V\u000a tp379 a(g165 Vbe the final location of the document, so be careful about linking to p380 tp381 a(g165 V\u000a tp382 a(g165 Vit -- you may want to add a disclaimer.) p383 tp384 a(g165 V\u000a tp385 a(g165 V\u000a tp386 a(g165 VIn this document, we'll take a tour of Python's features suitable for p387 tp388 a(g165 V\u000a tp389 a(g165 Vimplementing programs in a functional style. After an introduction to p390 tp391 a(g165 V\u000a tp392 a(g165 Vthe concepts of functional programming, we'll look at language p393 tp394 a(g165 V\u000a tp395 a(g165 Vfeatures such as iterators and generators and relevant library modules p396 tp397 a(g165 V\u000a tp398 a(g165 Vsuch as p399 tp400 a(g202 V`` p401 tp402 a(g202 Vitertools p403 tp404 a(g202 V`` p405 tp406 a(g165 V and p407 tp408 a(g202 V`` p409 tp410 a(g202 Vfunctools p411 tp412 a(g202 V`` p413 tp414 a(g165 V. tp415 a(g165 V\u000a tp416 a(g165 V\u000a tp417 a(g165 V\u000a tp418 a(g178 V.. p419 tp420 a(g165 V tp421 a(g324 Vcontents p422 tp423 a(g178 V:: p424 tp425 a(g165 V\u000a tp426 a(g165 V\u000a tp427 a(g7 VIntroduction p428 tp429 a(g165 V\u000a tp430 a(g7 V---------------------- p431 tp432 a(g165 V\u000a tp433 a(g165 V\u000a tp434 a(g165 VThis section explains the basic concept of functional programming; if p435 tp436 a(g165 V\u000a tp437 a(g165 Vyou're just interested in learning about Python language features, p438 tp439 a(g165 V\u000a tp440 a(g165 Vskip to the next section. p441 tp442 a(g165 V\u000a tp443 a(g165 V\u000a tp444 a(g165 VProgramming languages support decomposing problems in several different p445 tp446 a(g165 V\u000a tp447 a(g165 Vways p448 tp449 a(g165 V: tp450 a(g165 V\u000a tp451 a(g165 V\u000a tp452 a(g189 V* tp453 a(g165 V Most programming languages are p454 tp455 a(g359 V**procedural** p456 tp457 a(g165 V: tp458 a(g165 V tp459 a(g165 V\u000a tp460 a(g165 V programs are lists of instructions that tell the computer what to p461 tp462 a(g165 V\u000a tp463 a(g165 V do with the program's input. p464 tp465 a(g165 V\u000a tp466 a(g165 V C, Pascal, and even Unix shells are procedural languages. p467 tp468 a(g165 V\u000a tp469 a(g165 V\u000a tp470 a(g189 V* tp471 a(g165 V In p472 tp473 a(g359 V**declarative** p474 tp475 a(g165 V languages, you write a specification that describes p476 tp477 a(g165 V\u000a tp478 a(g165 V the problem to be solved, and the language implementation figures out p479 tp480 a(g165 V\u000a tp481 a(g165 V how to perform the computation efficiently. SQL is the declarative p482 tp483 a(g165 V\u000a tp484 a(g165 V language you're most likely to be familiar with; a SQL query describes p485 tp486 a(g165 V\u000a tp487 a(g165 V the data set you want to retrieve, and the SQL engine decides whether to p488 tp489 a(g165 V\u000a tp490 a(g165 V scan tables or use indexes, which subclauses should be performed first, p491 tp492 a(g165 V\u000a tp493 a(g165 V etc. p494 tp495 a(g165 V\u000a tp496 a(g165 V\u000a tp497 a(g189 V* tp498 a(g165 V tp499 a(g359 V**Object-oriented** p500 tp501 a(g165 V programs manipulate collections of objects. p502 tp503 a(g165 V\u000a tp504 a(g165 V Objects have internal state and support methods that query or modify p505 tp506 a(g165 V\u000a tp507 a(g165 V this internal state in some way. Smalltalk and Java are p508 tp509 a(g165 V\u000a tp510 a(g165 V object-oriented languages. C++ and Python are languages that p511 tp512 a(g165 V\u000a tp513 a(g165 V support object-oriented programming, but don't force the use p514 tp515 a(g165 V\u000a tp516 a(g165 V of object-oriented features. p517 tp518 a(g165 V\u000a tp519 a(g165 V\u000a tp520 a(g189 V* tp521 a(g165 V tp522 a(g359 V**Functional** p523 tp524 a(g165 V programming decomposes a problem into a set of functions. p525 tp526 a(g165 V\u000a tp527 a(g165 V Ideally, functions only take inputs and produce outputs, and don't have any p528 tp529 a(g165 V\u000a tp530 a(g165 V internal state that affects the output produced for a given input. p531 tp532 a(g165 V\u000a tp533 a(g165 V Well-known functional languages include the ML family (Standard ML, p534 tp535 a(g165 V\u000a tp536 a(g165 V OCaml, and other variants) and Haskell. p537 tp538 a(g165 V\u000a tp539 a(g165 V\u000a tp540 a(g165 VThe designers of some computer languages have chosen one approach to p541 tp542 a(g165 V\u000a tp543 a(g165 Vprogramming that's emphasized. This often makes it difficult to p544 tp545 a(g165 V\u000a tp546 a(g165 Vwrite programs that use a different approach. Other languages are p547 tp548 a(g165 V\u000a tp549 a(g165 Vmulti-paradigm languages that support several different approaches. Lisp, p550 tp551 a(g165 V\u000a tp552 a(g165 VC++, and Python are multi-paradigm; you can write programs or p553 tp554 a(g165 V\u000a tp555 a(g165 Vlibraries that are largely procedural, object-oriented, or functional p556 tp557 a(g165 V\u000a tp558 a(g165 Vin all of these languages. In a large program, different sections p559 tp560 a(g165 V\u000a tp561 a(g165 Vmight be written using different approaches; the GUI might be object-oriented p562 tp563 a(g165 V\u000a tp564 a(g165 Vwhile the processing logic is procedural or functional, for example. p565 tp566 a(g165 V\u000a tp567 a(g165 V\u000a tp568 a(g165 VIn a functional program, input flows through a set of functions. Each p569 tp570 a(g165 V\u000a tp571 a(g165 Vfunction operates on its input and produces some output. Functional p572 tp573 a(g165 V\u000a tp574 a(g165 Vstyle frowns upon functions with side effects that modify internal p575 tp576 a(g165 V\u000a tp577 a(g165 Vstate or make other changes that aren't visible in the function's p578 tp579 a(g165 V\u000a tp580 a(g165 Vreturn value. Functions that have no side effects at all are p581 tp582 a(g165 V\u000a tp583 a(g165 Vcalled p584 tp585 a(g359 V**purely functional** p586 tp587 a(g165 V. tp588 a(g165 V\u000a tp589 a(g165 VAvoiding side effects means not using data structures p590 tp591 a(g165 V\u000a tp592 a(g165 Vthat get updated as a program runs; every function's output p593 tp594 a(g165 V\u000a tp595 a(g165 Vmust only depend on its input. p596 tp597 a(g165 V\u000a tp598 a(g165 V\u000a tp599 a(g165 VSome languages are very strict about purity and don't even have p600 tp601 a(g165 V\u000a tp602 a(g165 Vassignment statements such as p603 tp604 a(g202 V`` p605 tp606 a(g202 Va=3 p607 tp608 a(g202 V`` p609 tp610 a(g165 V or p611 tp612 a(g202 V`` p613 tp614 a(g202 Vc = a + b p615 tp616 a(g202 V`` p617 tp618 a(g165 V, but it's p619 tp620 a(g165 V\u000a tp621 a(g165 Vdifficult to avoid all side effects. Printing to the screen or p622 tp623 a(g165 V\u000a tp624 a(g165 Vwriting to a disk file are side effects, for example. For example, in p625 tp626 a(g165 V\u000a tp627 a(g165 VPython a p628 tp629 a(g202 V`` p630 tp631 a(g202 Vprint p632 tp633 a(g202 V`` p634 tp635 a(g165 V statement or a p636 tp637 a(g202 V`` p638 tp639 a(g202 Vtime.sleep(1) p640 tp641 a(g202 V`` p642 tp643 a(g165 V both return no p644 tp645 a(g165 V\u000a tp646 a(g165 Vuseful value; they're only called for their side effects of sending p647 tp648 a(g165 V\u000a tp649 a(g165 Vsome text to the screen or pausing execution for a second. p650 tp651 a(g165 V\u000a tp652 a(g165 V\u000a tp653 a(g165 VPython programs written in functional style usually won't go to the p654 tp655 a(g165 V\u000a tp656 a(g165 Vextreme of avoiding all I/O or all assignments; instead, they'll p657 tp658 a(g165 V\u000a tp659 a(g165 Vprovide a functional-appearing interface but will use non-functional p660 tp661 a(g165 V\u000a tp662 a(g165 Vfeatures internally. For example, the implementation of a function p663 tp664 a(g165 V\u000a tp665 a(g165 Vwill still use assignments to local variables, but won't modify global p666 tp667 a(g165 V\u000a tp668 a(g165 Vvariables or have other side effects. p669 tp670 a(g165 V\u000a tp671 a(g165 V\u000a tp672 a(g165 VFunctional programming can be considered the opposite of p673 tp674 a(g165 V\u000a tp675 a(g165 Vobject-oriented programming. Objects are little capsules containing p676 tp677 a(g165 V\u000a tp678 a(g165 Vsome internal state along with a collection of method calls that let p679 tp680 a(g165 V\u000a tp681 a(g165 Vyou modify this state, and programs consist of making the right set of p682 tp683 a(g165 V\u000a tp684 a(g165 Vstate changes. Functional programming wants to avoid state changes as p685 tp686 a(g165 V\u000a tp687 a(g165 Vmuch as possible and works with data flowing between functions. In p688 tp689 a(g165 V\u000a tp690 a(g165 VPython you might combine the two approaches by writing functions that p691 tp692 a(g165 V\u000a tp693 a(g165 Vtake and return instances representing objects in your application p694 tp695 a(g165 V\u000a tp696 a(g165 V(e-mail messages, transactions, etc.). p697 tp698 a(g165 V\u000a tp699 a(g165 V\u000a tp700 a(g165 VFunctional design may seem like an odd constraint to work under. Why p701 tp702 a(g165 V\u000a tp703 a(g165 Vshould you avoid objects and side effects? There are theoretical and p704 tp705 a(g165 V\u000a tp706 a(g165 Vpractical advantages to the functional style p707 tp708 a(g165 V: tp709 a(g165 V\u000a tp710 a(g165 V\u000a tp711 a(g189 V* tp712 a(g165 V Formal provability. p713 tp714 a(g165 V\u000a tp715 a(g189 V* tp716 a(g165 V Modularity. p717 tp718 a(g165 V\u000a tp719 a(g189 V* tp720 a(g165 V Composability. p721 tp722 a(g165 V\u000a tp723 a(g189 V* tp724 a(g165 V Ease of debugging and testing. p725 tp726 a(g165 V\u000a tp727 a(g165 V\u000a tp728 a(g7 VFormal provability p729 tp730 a(g165 V\u000a tp731 a(g7 V'''''''''''''''''''''' p732 tp733 a(g165 V\u000a tp734 a(g165 V\u000a tp735 a(g165 VA theoretical benefit is that it's easier to construct a mathematical proof p736 tp737 a(g165 V\u000a tp738 a(g165 Vthat a functional program is correct. p739 tp740 a(g165 V\u000a tp741 a(g165 V\u000a tp742 a(g165 VFor a long time researchers have been interested in finding ways to p743 tp744 a(g165 V\u000a tp745 a(g165 Vmathematically prove programs correct. This is different from testing p746 tp747 a(g165 V\u000a tp748 a(g165 Va program on numerous inputs and concluding that its output is usually p749 tp750 a(g165 V\u000a tp751 a(g165 Vcorrect, or reading a program's source code and concluding that the p752 tp753 a(g165 V\u000a tp754 a(g165 Vcode looks right; the goal is instead a rigorous proof that a program p755 tp756 a(g165 V\u000a tp757 a(g165 Vproduces the right result for all possible inputs. p758 tp759 a(g165 V\u000a tp760 a(g165 V\u000a tp761 a(g165 VThe technique used to prove programs correct is to write down p762 tp763 a(g165 V\u000a tp764 a(g359 V**invariants** p765 tp766 a(g165 V, properties of the input data and of the program's p767 tp768 a(g165 V\u000a tp769 a(g165 Vvariables that are always true. For each line of code, you then show p770 tp771 a(g165 V\u000a tp772 a(g165 Vthat if invariants X and Y are true p773 tp774 a(g359 V**before** p775 tp776 a(g165 V the line is executed, p777 tp778 a(g165 V\u000a tp779 a(g165 Vthe slightly different invariants X' and Y' are true p780 tp781 a(g359 V**after** p782 tp783 a(g165 V\u000a tp784 a(g165 Vthe line is executed. This continues until you reach the end of the p785 tp786 a(g165 V\u000a tp787 a(g165 Vprogram, at which point the invariants should match the desired p788 tp789 a(g165 V\u000a tp790 a(g165 Vconditions on the program's output. p791 tp792 a(g165 V\u000a tp793 a(g165 V\u000a tp794 a(g165 VFunctional programming's avoidance of assignments arose because p795 tp796 a(g165 V\u000a tp797 a(g165 Vassignments are difficult to handle with this technique; p798 tp799 a(g165 V\u000a tp800 a(g165 Vassignments can break invariants that were true before the assignment p801 tp802 a(g165 V\u000a tp803 a(g165 Vwithout producing any new invariants that can be propagated onward. p804 tp805 a(g165 V\u000a tp806 a(g165 V\u000a tp807 a(g165 VUnfortunately, proving programs correct is largely impractical and not p808 tp809 a(g165 V\u000a tp810 a(g165 Vrelevant to Python software. Even trivial programs require proofs that p811 tp812 a(g165 V\u000a tp813 a(g165 Vare several pages long; the proof of correctness for a moderately p814 tp815 a(g165 V\u000a tp816 a(g165 Vcomplicated program would be enormous, and few or none of the programs p817 tp818 a(g165 V\u000a tp819 a(g165 Vyou use daily (the Python interpreter, your XML parser, your web p820 tp821 a(g165 V\u000a tp822 a(g165 Vbrowser) could be proven correct. Even if you wrote down or generated p823 tp824 a(g165 V\u000a tp825 a(g165 Va proof, there would then be the question of verifying the proof; p826 tp827 a(g165 V\u000a tp828 a(g165 Vmaybe there's an error in it, and you wrongly believe you've proved p829 tp830 a(g165 V\u000a tp831 a(g165 Vthe program correct. p832 tp833 a(g165 V\u000a tp834 a(g165 V\u000a tp835 a(g7 VModularity p836 tp837 a(g165 V\u000a tp838 a(g7 V'''''''''''''''''''''' p839 tp840 a(g165 V\u000a tp841 a(g165 V\u000a tp842 a(g165 VA more practical benefit of functional programming is that it forces p843 tp844 a(g165 V\u000a tp845 a(g165 Vyou to break apart your problem into small pieces. Programs are more p846 tp847 a(g165 V\u000a tp848 a(g165 Vmodular as a result. It's easier to specify and write a small p849 tp850 a(g165 V\u000a tp851 a(g165 Vfunction that does one thing than a large function that performs a p852 tp853 a(g165 V\u000a tp854 a(g165 Vcomplicated transformation. Small functions are also easier to read p855 tp856 a(g165 V\u000a tp857 a(g165 Vand to check for errors. p858 tp859 a(g165 V\u000a tp860 a(g165 V\u000a tp861 a(g165 V\u000a tp862 a(g7 VEase of debugging and testing p863 tp864 a(g165 V\u000a tp865 a(g7 V'''''''''''''''''''''''''''''''''' p866 tp867 a(g165 V\u000a tp868 a(g165 V\u000a tp869 a(g165 VTesting and debugging a functional-style program is easier. p870 tp871 a(g165 V\u000a tp872 a(g165 V\u000a tp873 a(g165 VDebugging is simplified because functions are generally small and p874 tp875 a(g165 V\u000a tp876 a(g165 Vclearly specified. When a program doesn't work, each function is an p877 tp878 a(g165 V\u000a tp879 a(g165 Vinterface point where you can check that the data are correct. You p880 tp881 a(g165 V\u000a tp882 a(g165 Vcan look at the intermediate inputs and outputs to quickly isolate the p883 tp884 a(g165 V\u000a tp885 a(g165 Vfunction that's responsible for a bug. p886 tp887 a(g165 V\u000a tp888 a(g165 V\u000a tp889 a(g165 VTesting is easier because each function is a potential subject for a p890 tp891 a(g165 V\u000a tp892 a(g165 Vunit test. Functions don't depend on system state that needs to be p893 tp894 a(g165 V\u000a tp895 a(g165 Vreplicated before running a test; instead you only have to synthesize p896 tp897 a(g165 V\u000a tp898 a(g165 Vthe right input and then check that the output matches expectations. p899 tp900 a(g165 V\u000a tp901 a(g165 V\u000a tp902 a(g165 V\u000a tp903 a(g165 V\u000a tp904 a(g7 VComposability p905 tp906 a(g165 V\u000a tp907 a(g7 V'''''''''''''''''''''' p908 tp909 a(g165 V\u000a tp910 a(g165 V\u000a tp911 a(g165 VAs you work on a functional-style program, you'll write a number of p912 tp913 a(g165 V\u000a tp914 a(g165 Vfunctions with varying inputs and outputs. Some of these functions p915 tp916 a(g165 V\u000a tp917 a(g165 Vwill be unavoidably specialized to a particular application, but p918 tp919 a(g165 V\u000a tp920 a(g165 Vothers will be useful in a wide variety of programs. For example, a p921 tp922 a(g165 V\u000a tp923 a(g165 Vfunction that takes a directory path and returns all the XML files in p924 tp925 a(g165 V\u000a tp926 a(g165 Vthe directory, or a function that takes a filename and returns its p927 tp928 a(g165 V\u000a tp929 a(g165 Vcontents, can be applied to many different situations. p930 tp931 a(g165 V\u000a tp932 a(g165 V\u000a tp933 a(g165 VOver time you'll form a personal library of utilities. Often you'll p934 tp935 a(g165 V\u000a tp936 a(g165 Vassemble new programs by arranging existing functions in a new p937 tp938 a(g165 V\u000a tp939 a(g165 Vconfiguration and writing a few functions specialized for the current p940 tp941 a(g165 V\u000a tp942 a(g165 Vtask. p943 tp944 a(g165 V\u000a tp945 a(g165 V\u000a tp946 a(g165 V\u000a tp947 a(g165 V\u000a tp948 a(g7 VIterators p949 tp950 a(g165 V\u000a tp951 a(g7 V----------------------- p952 tp953 a(g165 V\u000a tp954 a(g165 V\u000a tp955 a(g165 VI'll start by looking at a Python language feature that's an important p956 tp957 a(g165 V\u000a tp958 a(g165 Vfoundation for writing functional-style programs p959 tp960 a(g165 V: tp961 a(g165 V iterators. p962 tp963 a(g165 V\u000a tp964 a(g165 V\u000a tp965 a(g165 VAn iterator is an object representing a stream of data; this object p966 tp967 a(g165 V\u000a tp968 a(g165 Vreturns the data one element at a time. A Python iterator must p969 tp970 a(g165 V\u000a tp971 a(g165 Vsupport a method called p972 tp973 a(g202 V`` p974 tp975 a(g202 Vnext() p976 tp977 a(g202 V`` p978 tp979 a(g165 V that takes no arguments and always p980 tp981 a(g165 V\u000a tp982 a(g165 Vreturns the next element of the stream. If there are no more elements p983 tp984 a(g165 V\u000a tp985 a(g165 Vin the stream, p986 tp987 a(g202 V`` p988 tp989 a(g202 Vnext() p990 tp991 a(g202 V`` p992 tp993 a(g165 V must raise the p994 tp995 a(g202 V`` p996 tp997 a(g202 VStopIteration p998 tp999 a(g202 V`` p1000 tp1001 a(g165 V exception. p1002 tp1003 a(g165 V\u000a tp1004 a(g165 VIterators don't have to be finite, though; it's perfectly reasonable p1005 tp1006 a(g165 V\u000a tp1007 a(g165 Vto write an iterator that produces an infinite stream of data. p1008 tp1009 a(g165 V\u000a tp1010 a(g165 V\u000a tp1011 a(g165 VThe built-in p1012 tp1013 a(g202 V`` p1014 tp1015 a(g202 Viter() p1016 tp1017 a(g202 V`` p1018 tp1019 a(g165 V function takes an arbitrary object and tries p1020 tp1021 a(g165 V\u000a tp1022 a(g165 Vto return an iterator that will return the object's contents or p1023 tp1024 a(g165 V\u000a tp1025 a(g165 Velements, raising p1026 tp1027 a(g202 V`` p1028 tp1029 a(g202 VTypeError p1030 tp1031 a(g202 V`` p1032 tp1033 a(g165 V if the object doesn't support p1034 tp1035 a(g165 V\u000a tp1036 a(g165 Viteration. Several of Python's built-in data types support iteration, p1037 tp1038 a(g165 V\u000a tp1039 a(g165 Vthe most common being lists and dictionaries. An object is called p1040 tp1041 a(g165 V\u000a tp1042 a(g165 Van p1043 tp1044 a(g359 V**iterable** p1045 tp1046 a(g165 V object if you can get an iterator for it. p1047 tp1048 a(g165 V\u000a tp1049 a(g165 V\u000a tp1050 a(g165 VYou can experiment with the iteration interface manually p1051 tp1052 a(g228 V:: p1053 tp1054 a(g165 V\u000a\u000a p1055 tp1056 a(g202 V p1057 tp1058 a(g202 V>>> L = [1,2,3] p1059 tp1060 a(g165 V\u000a tp1061 a(g202 V >>> it = iter(L)\u000a >>> print it\u000a \u000a >>> it.next()\u000a 1\u000a >>> it.next()\u000a 2\u000a >>> it.next()\u000a 3\u000a >>> it.next()\u000a Traceback (most recent call last):\u000a File "", line 1, in ?\u000a StopIteration\u000a >>> \u000a\u000a p1062 tp1063 a(g165 VPython expects iterable objects in several different contexts, the p1064 tp1065 a(g165 V\u000a tp1066 a(g165 Vmost important being the p1067 tp1068 a(g202 V`` p1069 tp1070 a(g202 Vfor p1071 tp1072 a(g202 V`` p1073 tp1074 a(g165 V statement. In the statement p1075 tp1076 a(g202 V`` p1077 tp1078 a(g202 Vfor X in Y p1079 tp1080 a(g202 V`` p1081 tp1082 a(g165 V, tp1083 a(g165 V\u000a tp1084 a(g165 VY must be an iterator or some object for which p1085 tp1086 a(g202 V`` p1087 tp1088 a(g202 Viter() p1089 tp1090 a(g202 V`` p1091 tp1092 a(g165 V can create p1093 tp1094 a(g165 V\u000a tp1095 a(g165 Van iterator. These two statements are equivalent p1096 tp1097 a(g228 V:: p1098 tp1099 a(g165 V\u000a\u000a p1100 tp1101 a(g202 V p1102 tp1103 a(g202 Vfor i in iter(obj): p1104 tp1105 a(g165 V\u000a tp1106 a(g202 V print i\u000a\u000a for i in obj:\u000a print i\u000a\u000a p1107 tp1108 a(g165 VIterators can be materialized as lists or tuples by using the p1109 tp1110 a(g165 V\u000a tp1111 a(g202 V`` p1112 tp1113 a(g202 Vlist() p1114 tp1115 a(g202 V`` p1116 tp1117 a(g165 V or p1118 tp1119 a(g202 V`` p1120 tp1121 a(g202 Vtuple() p1122 tp1123 a(g202 V`` p1124 tp1125 a(g165 V constructor functions p1126 tp1127 a(g228 V:: p1128 tp1129 a(g165 V\u000a\u000a p1130 tp1131 a(g202 V p1132 tp1133 a(g202 V>>> L = [1,2,3] p1134 tp1135 a(g165 V\u000a tp1136 a(g202 V >>> iterator = iter(L)\u000a >>> t = tuple(iterator)\u000a >>> t\u000a (1, 2, 3)\u000a\u000a p1137 tp1138 a(g165 VSequence unpacking also supports iterators p1139 tp1140 a(g165 V: tp1141 a(g165 V if you know an iterator p1142 tp1143 a(g165 V\u000a tp1144 a(g165 Vwill return N elements, you can unpack them into an N-tuple p1145 tp1146 a(g228 V:: p1147 tp1148 a(g165 V\u000a\u000a p1149 tp1150 a(g202 V p1151 tp1152 a(g202 V>>> L = [1,2,3] p1153 tp1154 a(g165 V\u000a tp1155 a(g202 V >>> iterator = iter(L)\u000a >>> a,b,c = iterator\u000a >>> a,b,c\u000a (1, 2, 3)\u000a\u000a p1156 tp1157 a(g165 VBuilt-in functions such as p1158 tp1159 a(g202 V`` p1160 tp1161 a(g202 Vmax() p1162 tp1163 a(g202 V`` p1164 tp1165 a(g165 V and p1166 tp1167 a(g202 V`` p1168 tp1169 a(g202 Vmin() p1170 tp1171 a(g202 V`` p1172 tp1173 a(g165 V can take a single p1174 tp1175 a(g165 V\u000a tp1176 a(g165 Viterator argument and will return the largest or smallest element. p1177 tp1178 a(g165 V\u000a tp1179 a(g165 VThe p1180 tp1181 a(g202 V`` p1182 tp1183 a(g202 V"in" p1184 tp1185 a(g202 V`` p1186 tp1187 a(g165 V and p1188 tp1189 a(g202 V`` p1190 tp1191 a(g202 V"not in" p1192 tp1193 a(g202 V`` p1194 tp1195 a(g165 V operators also support iterators p1196 tp1197 a(g165 V: tp1198 a(g165 V tp1199 a(g202 V`` p1200 tp1201 a(g202 VX in\u000aiterator p1202 tp1203 a(g202 V`` p1204 tp1205 a(g165 V is true if X is found in the stream returned by the p1206 tp1207 a(g165 V\u000a tp1208 a(g165 Viterator. You'll run into obvious problems if the iterator is p1209 tp1210 a(g165 V\u000a tp1211 a(g165 Vinfinite; p1212 tp1213 a(g202 V`` p1214 tp1215 a(g202 Vmax() p1216 tp1217 a(g202 V`` p1218 tp1219 a(g165 V, p1220 tp1221 a(g202 V`` p1222 tp1223 a(g202 Vmin() p1224 tp1225 a(g202 V`` p1226 tp1227 a(g165 V, and p1228 tp1229 a(g202 V`` p1230 tp1231 a(g202 V"not in" p1232 tp1233 a(g202 V`` p1234 tp1235 a(g165 V will never return, and p1236 tp1237 a(g165 V\u000a tp1238 a(g165 Vif the element X never appears in the stream, the p1239 tp1240 a(g202 V`` p1241 tp1242 a(g202 V"in" p1243 tp1244 a(g202 V`` p1245 tp1246 a(g165 V operator p1247 tp1248 a(g165 V\u000a tp1249 a(g165 Vwon't return either. p1250 tp1251 a(g165 V\u000a tp1252 a(g165 V\u000a tp1253 a(g165 VNote that you can only go forward in an iterator; there's no way to p1254 tp1255 a(g165 V\u000a tp1256 a(g165 Vget the previous element, reset the iterator, or make a copy of it. p1257 tp1258 a(g165 V\u000a tp1259 a(g165 VIterator objects can optionally provide these additional capabilities, p1260 tp1261 a(g165 V\u000a tp1262 a(g165 Vbut the iterator protocol only specifies the p1263 tp1264 a(g202 V`` p1265 tp1266 a(g202 Vnext() p1267 tp1268 a(g202 V`` p1269 tp1270 a(g165 V method. p1271 tp1272 a(g165 V\u000a tp1273 a(g165 VFunctions may therefore consume all of the iterator's output, and if p1274 tp1275 a(g165 V\u000a tp1276 a(g165 Vyou need to do something different with the same stream, you'll have p1277 tp1278 a(g165 V\u000a tp1279 a(g165 Vto create a new iterator. p1280 tp1281 a(g165 V\u000a tp1282 a(g165 V\u000a tp1283 a(g165 V\u000a tp1284 a(g165 V\u000a tp1285 a(g7 VData Types That Support Iterators p1286 tp1287 a(g165 V\u000a tp1288 a(g7 V''''''''''''''''''''''''''''''''''' p1289 tp1290 a(g165 V\u000a tp1291 a(g165 V\u000a tp1292 a(g165 VWe've already seen how lists and tuples support iterators. In fact, p1293 tp1294 a(g165 V\u000a tp1295 a(g165 Vany Python sequence type, such as strings, will automatically support p1296 tp1297 a(g165 V\u000a tp1298 a(g165 Vcreation of an iterator. p1299 tp1300 a(g165 V\u000a tp1301 a(g165 V\u000a tp1302 a(g165 VCalling p1303 tp1304 a(g202 V`` p1305 tp1306 a(g202 Viter() p1307 tp1308 a(g202 V`` p1309 tp1310 a(g165 V on a dictionary returns an iterator that will loop p1311 tp1312 a(g165 V\u000a tp1313 a(g165 Vover the dictionary's keys p1314 tp1315 a(g228 V:: p1316 tp1317 a(g165 V\u000a\u000a p1318 tp1319 a(g202 V p1320 tp1321 a(g202 V>>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, p1322 tp1323 a(g165 V\u000a tp1324 a(g202 V ... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}\u000a >>> for key in m:\u000a ... print key, m[key]\u000a Mar 3\u000a Feb 2\u000a Aug 8\u000a Sep 9\u000a May 5\u000a Jun 6\u000a Jul 7\u000a Jan 1\u000a Apr 4\u000a Nov 11\u000a Dec 12\u000a Oct 10\u000a\u000a p1325 tp1326 a(g165 VNote that the order is essentially random, because it's based on the p1327 tp1328 a(g165 V\u000a tp1329 a(g165 Vhash ordering of the objects in the dictionary. p1330 tp1331 a(g165 V\u000a tp1332 a(g165 V\u000a tp1333 a(g165 VApplying p1334 tp1335 a(g202 V`` p1336 tp1337 a(g202 Viter() p1338 tp1339 a(g202 V`` p1340 tp1341 a(g165 V to a dictionary always loops over the keys, but p1342 tp1343 a(g165 V\u000a tp1344 a(g165 Vdictionaries have methods that return other iterators. If you want to p1345 tp1346 a(g165 V\u000a tp1347 a(g165 Viterate over keys, values, or key/value pairs, you can explicitly call p1348 tp1349 a(g165 V\u000a tp1350 a(g165 Vthe p1351 tp1352 a(g202 V`` p1353 tp1354 a(g202 Viterkeys() p1355 tp1356 a(g202 V`` p1357 tp1358 a(g165 V, p1359 tp1360 a(g202 V`` p1361 tp1362 a(g202 Vitervalues() p1363 tp1364 a(g202 V`` p1365 tp1366 a(g165 V, or p1367 tp1368 a(g202 V`` p1369 tp1370 a(g202 Viteritems() p1371 tp1372 a(g202 V`` p1373 tp1374 a(g165 V methods to p1375 tp1376 a(g165 V\u000a tp1377 a(g165 Vget an appropriate iterator. p1378 tp1379 a(g165 V\u000a tp1380 a(g165 V\u000a tp1381 a(g165 VThe p1382 tp1383 a(g202 V`` p1384 tp1385 a(g202 Vdict() p1386 tp1387 a(g202 V`` p1388 tp1389 a(g165 V constructor can accept an iterator that returns a p1390 tp1391 a(g165 V\u000a tp1392 a(g165 Vfinite stream of p1393 tp1394 a(g202 V`` p1395 tp1396 a(g202 V(key, value) p1397 tp1398 a(g202 V`` p1399 tp1400 a(g165 V tuples p1401 tp1402 a(g228 V:: p1403 tp1404 a(g165 V\u000a\u000a p1405 tp1406 a(g202 V p1407 tp1408 a(g202 V>>> L = [('Italy', 'Rome'), ('France', 'Paris'), ('US', 'Washington DC')] p1409 tp1410 a(g165 V\u000a tp1411 a(g202 V >>> dict(iter(L))\u000a {'Italy': 'Rome', 'US': 'Washington DC', 'France': 'Paris'}\u000a\u000a p1412 tp1413 a(g165 VFiles also support iteration by calling the p1414 tp1415 a(g202 V`` p1416 tp1417 a(g202 Vreadline() p1418 tp1419 a(g202 V`` p1420 tp1421 a(g165 V\u000a tp1422 a(g165 Vmethod until there are no more lines in the file. This means you can p1423 tp1424 a(g165 V\u000a tp1425 a(g165 Vread each line of a file like this p1426 tp1427 a(g228 V:: p1428 tp1429 a(g165 V\u000a\u000a p1430 tp1431 a(g202 V p1432 tp1433 a(g202 Vfor line in file: p1434 tp1435 a(g165 V\u000a tp1436 a(g202 V # do something for each line\u000a ...\u000a\u000a p1437 tp1438 a(g165 VSets can take their contents from an iterable and let you iterate over p1439 tp1440 a(g165 V\u000a tp1441 a(g165 Vthe set's elements p1442 tp1443 a(g228 V:: p1444 tp1445 a(g165 V\u000a\u000a p1446 tp1447 a(g202 V p1448 tp1449 a(g202 VS = set((2, 3, 5, 7, 11, 13)) p1450 tp1451 a(g165 V\u000a tp1452 a(g202 V for i in S:\u000a print i\u000a\u000a\u000a\u000a p1453 tp1454 a(g7 VGenerator expressions and list comprehensions p1455 tp1456 a(g165 V\u000a tp1457 a(g7 V---------------------------------------------------- p1458 tp1459 a(g165 V\u000a tp1460 a(g165 V\u000a tp1461 a(g165 VTwo common operations on an iterator's output are 1) performing some p1462 tp1463 a(g165 V\u000a tp1464 a(g165 Voperation for every element, 2) selecting a subset of elements that p1465 tp1466 a(g165 V\u000a tp1467 a(g165 Vmeet some condition. For example, given a list of strings, you might p1468 tp1469 a(g165 V\u000a tp1470 a(g165 Vwant to strip off trailing whitespace from each line or extract all p1471 tp1472 a(g165 V\u000a tp1473 a(g165 Vthe strings containing a given substring. p1474 tp1475 a(g165 V\u000a tp1476 a(g165 V\u000a tp1477 a(g165 VList comprehensions and generator expressions (short form p1478 tp1479 a(g165 V: tp1480 a(g165 V "listcomps" p1481 tp1482 a(g165 V\u000a tp1483 a(g165 Vand "genexps") are a concise notation for such operations, borrowed p1484 tp1485 a(g165 V\u000a tp1486 a(g165 Vfrom the functional programming language Haskell p1487 tp1488 a(g165 V\u000a tp1489 a(g165 V(http p1490 tp1491 a(g165 V: tp1492 a(g165 V//www.haskell.org). You can strip all the whitespace from a p1493 tp1494 a(g165 V\u000a tp1495 a(g165 Vstream of strings with the following code p1496 tp1497 a(g228 V:: p1498 tp1499 a(g165 V\u000a\u000a p1500 tp1501 a(g202 V p1502 tp1503 a(g202 Vline_list = [' line 1\u005cn', 'line 2 \u005cn', ...] p1504 tp1505 a(g165 V\u000a tp1506 a(g202 V\u000a # Generator expression -- returns iterator\u000a stripped_iter = (line.strip() for line in line_list)\u000a\u000a # List comprehension -- returns list\u000a stripped_list = [line.strip() for line in line_list]\u000a\u000a p1507 tp1508 a(g165 VYou can select only certain elements by adding an p1509 tp1510 a(g202 V`` p1511 tp1512 a(g202 V"if" p1513 tp1514 a(g202 V`` p1515 tp1516 a(g165 V condition p1517 tp1518 a(g228 V:: p1519 tp1520 a(g165 V\u000a\u000a p1521 tp1522 a(g202 V p1523 tp1524 a(g202 Vstripped_list = [line.strip() for line in line_list p1525 tp1526 a(g165 V\u000a tp1527 a(g202 V if line != ""]\u000a\u000a p1528 tp1529 a(g165 VWith a list comprehension, you get back a Python list; p1530 tp1531 a(g165 V\u000a tp1532 a(g202 V`` p1533 tp1534 a(g202 Vstripped_list p1535 tp1536 a(g202 V`` p1537 tp1538 a(g165 V is a list containing the resulting lines, not an p1539 tp1540 a(g165 V\u000a tp1541 a(g165 Viterator. Generator expressions return an iterator that computes the p1542 tp1543 a(g165 V\u000a tp1544 a(g165 Vvalues as necessary, not needing to materialize all the values at p1545 tp1546 a(g165 V\u000a tp1547 a(g165 Vonce. This means that list comprehensions aren't useful if you're p1548 tp1549 a(g165 V\u000a tp1550 a(g165 Vworking with iterators that return an infinite stream or a very large p1551 tp1552 a(g165 V\u000a tp1553 a(g165 Vamount of data. Generator expressions are preferable in these p1554 tp1555 a(g165 V\u000a tp1556 a(g165 Vsituations. p1557 tp1558 a(g165 V\u000a tp1559 a(g165 V\u000a tp1560 a(g165 VGenerator expressions are surrounded by parentheses ("()") and list p1561 tp1562 a(g165 V\u000a tp1563 a(g165 Vcomprehensions are surrounded by square brackets (" p1564 tp1565 a(g165 V[ tp1566 a(g165 V]"). Generator p1567 tp1568 a(g165 V\u000a tp1569 a(g165 Vexpressions have the form p1570 tp1571 a(g228 V:: p1572 tp1573 a(g165 V\u000a\u000a p1574 tp1575 a(g202 V p1576 tp1577 a(g202 V( expression for expr in sequence1 p1578 tp1579 a(g165 V\u000a tp1580 a(g202 V if condition1\u000a for expr2 in sequence2\u000a if condition2\u000a for expr3 in sequence3 ...\u000a if condition3\u000a for exprN in sequenceN\u000a if conditionN )\u000a\u000a p1581 tp1582 a(g165 VAgain, for a list comprehension only the outside brackets are p1583 tp1584 a(g165 V\u000a tp1585 a(g165 Vdifferent (square brackets instead of parentheses). p1586 tp1587 a(g165 V\u000a tp1588 a(g165 V\u000a tp1589 a(g165 VThe elements of the generated output will be the successive values of p1590 tp1591 a(g165 V\u000a tp1592 a(g202 V`` p1593 tp1594 a(g202 Vexpression p1595 tp1596 a(g202 V`` p1597 tp1598 a(g165 V. The p1599 tp1600 a(g202 V`` p1601 tp1602 a(g202 Vif p1603 tp1604 a(g202 V`` p1605 tp1606 a(g165 V clauses are all optional; if present, p1607 tp1608 a(g165 V\u000a tp1609 a(g202 V`` p1610 tp1611 a(g202 Vexpression p1612 tp1613 a(g202 V`` p1614 tp1615 a(g165 V is only evaluated and added to the result when p1616 tp1617 a(g165 V\u000a tp1618 a(g202 V`` p1619 tp1620 a(g202 Vcondition p1621 tp1622 a(g202 V`` p1623 tp1624 a(g165 V is true. p1625 tp1626 a(g165 V\u000a tp1627 a(g165 V\u000a tp1628 a(g165 VGenerator expressions always have to be written inside parentheses, p1629 tp1630 a(g165 V\u000a tp1631 a(g165 Vbut the parentheses signalling a function call also count. If you p1632 tp1633 a(g165 V\u000a tp1634 a(g165 Vwant to create an iterator that will be immediately passed to a p1635 tp1636 a(g165 V\u000a tp1637 a(g165 Vfunction you can write p1638 tp1639 a(g228 V:: p1640 tp1641 a(g165 V\u000a\u000a p1642 tp1643 a(g202 V p1644 tp1645 a(g202 Vobj_total = sum(obj.count for obj in list_all_objects()) p1646 tp1647 a(g165 V\u000a tp1648 a(g202 V\u000a tp1649 a(g165 VThe p1650 tp1651 a(g202 V`` p1652 tp1653 a(g202 Vfor...in p1654 tp1655 a(g202 V`` p1656 tp1657 a(g165 V clauses contain the sequences to be iterated over. p1658 tp1659 a(g165 V\u000a tp1660 a(g165 VThe sequences do not have to be the same length, because they are p1661 tp1662 a(g165 V\u000a tp1663 a(g165 Viterated over from left to right, p1664 tp1665 a(g359 V**not** p1666 tp1667 a(g165 V in parallel. For each p1668 tp1669 a(g165 V\u000a tp1670 a(g165 Velement in p1671 tp1672 a(g202 V`` p1673 tp1674 a(g202 Vsequence1 p1675 tp1676 a(g202 V`` p1677 tp1678 a(g165 V, p1679 tp1680 a(g202 V`` p1681 tp1682 a(g202 Vsequence2 p1683 tp1684 a(g202 V`` p1685 tp1686 a(g165 V is looped over from the p1687 tp1688 a(g165 V\u000a tp1689 a(g165 Vbeginning. p1690 tp1691 a(g202 V`` p1692 tp1693 a(g202 Vsequence3 p1694 tp1695 a(g202 V`` p1696 tp1697 a(g165 V is then looped over for each p1698 tp1699 a(g165 V\u000a tp1700 a(g165 Vresulting pair of elements from p1701 tp1702 a(g202 V`` p1703 tp1704 a(g202 Vsequence1 p1705 tp1706 a(g202 V`` p1707 tp1708 a(g165 V and p1709 tp1710 a(g202 V`` p1711 tp1712 a(g202 Vsequence2 p1713 tp1714 a(g202 V`` p1715 tp1716 a(g165 V. tp1717 a(g165 V\u000a tp1718 a(g165 V\u000a tp1719 a(g165 VTo put it another way, a list comprehension or generator expression is p1720 tp1721 a(g165 V\u000a tp1722 a(g165 Vequivalent to the following Python code p1723 tp1724 a(g228 V:: p1725 tp1726 a(g165 V\u000a\u000a p1727 tp1728 a(g202 V p1729 tp1730 a(g202 Vfor expr1 in sequence1: p1731 tp1732 a(g165 V\u000a tp1733 a(g202 V if not (condition1):\u000a continue # Skip this element\u000a for expr2 in sequence2:\u000a if not (condition2):\u000a continue # Skip this element\u000a ...\u000a for exprN in sequenceN:\u000a if not (conditionN):\u000a continue # Skip this element\u000a\u000a # Output the value of \u000a # the expression.\u000a\u000a p1734 tp1735 a(g165 VThis means that when there are multiple p1736 tp1737 a(g202 V`` p1738 tp1739 a(g202 Vfor...in p1740 tp1741 a(g202 V`` p1742 tp1743 a(g165 V clauses but no p1744 tp1745 a(g165 V\u000a tp1746 a(g202 V`` p1747 tp1748 a(g202 Vif p1749 tp1750 a(g202 V`` p1751 tp1752 a(g165 V clauses, the length of the resulting output will be equal to p1753 tp1754 a(g165 V\u000a tp1755 a(g165 Vthe product of the lengths of all the sequences. If you have two p1756 tp1757 a(g165 V\u000a tp1758 a(g165 Vlists of length 3, the output list is 9 elements long p1759 tp1760 a(g228 V:: p1761 tp1762 a(g165 V\u000a\u000a p1763 tp1764 a(g202 V p1765 tp1766 a(g202 Vseq1 = 'abc' p1767 tp1768 a(g165 V\u000a tp1769 a(g202 V seq2 = (1,2,3)\u000a >>> [ (x,y) for x in seq1 for y in seq2]\u000a [('a', 1), ('a', 2), ('a', 3), \u000a ('b', 1), ('b', 2), ('b', 3), \u000a ('c', 1), ('c', 2), ('c', 3)]\u000a\u000a p1770 tp1771 a(g165 VTo avoid introducing an ambiguity into Python's grammar, if p1772 tp1773 a(g165 V\u000a tp1774 a(g202 V`` p1775 tp1776 a(g202 Vexpression p1777 tp1778 a(g202 V`` p1779 tp1780 a(g165 V is creating a tuple, it must be surrounded with p1781 tp1782 a(g165 V\u000a tp1783 a(g165 Vparentheses. The first list comprehension below is a syntax error, p1784 tp1785 a(g165 V\u000a tp1786 a(g165 Vwhile the second one is correct p1787 tp1788 a(g228 V:: p1789 tp1790 a(g165 V\u000a\u000a p1791 tp1792 a(g202 V p1793 tp1794 a(g202 V# Syntax error p1795 tp1796 a(g165 V\u000a tp1797 a(g202 V [ x,y for x in seq1 for y in seq2]\u000a # Correct\u000a [ (x,y) for x in seq1 for y in seq2]\u000a\u000a\u000a p1798 tp1799 a(g7 VGenerators p1800 tp1801 a(g165 V\u000a tp1802 a(g7 V----------------------- p1803 tp1804 a(g165 V\u000a tp1805 a(g165 V\u000a tp1806 a(g165 VGenerators are a special class of functions that simplify the task of p1807 tp1808 a(g165 V\u000a tp1809 a(g165 Vwriting iterators. Regular functions compute a value and return it, p1810 tp1811 a(g165 V\u000a tp1812 a(g165 Vbut generators return an iterator that returns a stream of values. p1813 tp1814 a(g165 V\u000a tp1815 a(g165 V\u000a tp1816 a(g165 VYou're doubtless familiar with how regular function calls work in p1817 tp1818 a(g165 V\u000a tp1819 a(g165 VPython or C. When you call a function, it gets a private namespace p1820 tp1821 a(g165 V\u000a tp1822 a(g165 Vwhere its local variables are created. When the function reaches a p1823 tp1824 a(g165 V\u000a tp1825 a(g202 V`` p1826 tp1827 a(g202 Vreturn p1828 tp1829 a(g202 V`` p1830 tp1831 a(g165 V statement, the local variables are destroyed and the p1832 tp1833 a(g165 V\u000a tp1834 a(g165 Vvalue is returned to the caller. A later call to the same function p1835 tp1836 a(g165 V\u000a tp1837 a(g165 Vcreates a new private namespace and a fresh set of local p1838 tp1839 a(g165 V\u000a tp1840 a(g165 Vvariables. But, what if the local variables weren't thrown away on p1841 tp1842 a(g165 V\u000a tp1843 a(g165 Vexiting a function? What if you could later resume the function where p1844 tp1845 a(g165 V\u000a tp1846 a(g165 Vit left off? This is what generators provide; they can be thought of p1847 tp1848 a(g165 V\u000a tp1849 a(g165 Vas resumable functions. p1850 tp1851 a(g165 V\u000a tp1852 a(g165 V\u000a tp1853 a(g165 VHere's the simplest example of a generator function p1854 tp1855 a(g228 V:: p1856 tp1857 a(g165 V\u000a\u000a p1858 tp1859 a(g202 V p1860 tp1861 a(g202 Vdef generate_ints(N): p1862 tp1863 a(g165 V\u000a tp1864 a(g202 V for i in range(N):\u000a yield i\u000a\u000a p1865 tp1866 a(g165 VAny function containing a p1867 tp1868 a(g202 V`` p1869 tp1870 a(g202 Vyield p1871 tp1872 a(g202 V`` p1873 tp1874 a(g165 V keyword is a generator function; p1875 tp1876 a(g165 V\u000a tp1877 a(g165 Vthis is detected by Python's bytecode compiler which compiles the p1878 tp1879 a(g165 V\u000a tp1880 a(g165 Vfunction specially as a result. p1881 tp1882 a(g165 V\u000a tp1883 a(g165 V\u000a tp1884 a(g165 VWhen you call a generator function, it doesn't return a single value; p1885 tp1886 a(g165 V\u000a tp1887 a(g165 Vinstead it returns a generator object that supports the iterator p1888 tp1889 a(g165 V\u000a tp1890 a(g165 Vprotocol. On executing the p1891 tp1892 a(g202 V`` p1893 tp1894 a(g202 Vyield p1895 tp1896 a(g202 V`` p1897 tp1898 a(g165 V expression, the generator p1899 tp1900 a(g165 V\u000a tp1901 a(g165 Voutputs the value of p1902 tp1903 a(g202 V`` p1904 tp1905 a(g202 Vi tp1906 a(g202 V`` p1907 tp1908 a(g165 V, similar to a p1909 tp1910 a(g202 V`` p1911 tp1912 a(g202 Vreturn p1913 tp1914 a(g202 V`` p1915 tp1916 a(g165 V\u000a tp1917 a(g165 Vstatement. The big difference between p1918 tp1919 a(g202 V`` p1920 tp1921 a(g202 Vyield p1922 tp1923 a(g202 V`` p1924 tp1925 a(g165 V and a p1926 tp1927 a(g165 V\u000a tp1928 a(g202 V`` p1929 tp1930 a(g202 Vreturn p1931 tp1932 a(g202 V`` p1933 tp1934 a(g165 V statement is that on reaching a p1935 tp1936 a(g202 V`` p1937 tp1938 a(g202 Vyield p1939 tp1940 a(g202 V`` p1941 tp1942 a(g165 V the p1943 tp1944 a(g165 V\u000a tp1945 a(g165 Vgenerator's state of execution is suspended and local variables are p1946 tp1947 a(g165 V\u000a tp1948 a(g165 Vpreserved. On the next call to the generator's p1949 tp1950 a(g202 V`` p1951 tp1952 a(g202 V.next() p1953 tp1954 a(g202 V`` p1955 tp1956 a(g165 V method, p1957 tp1958 a(g165 V\u000a tp1959 a(g165 Vthe function will resume executing. p1960 tp1961 a(g165 V\u000a tp1962 a(g165 V\u000a tp1963 a(g165 VHere's a sample usage of the p1964 tp1965 a(g202 V`` p1966 tp1967 a(g202 Vgenerate_ints() p1968 tp1969 a(g202 V`` p1970 tp1971 a(g165 V generator p1972 tp1973 a(g228 V:: p1974 tp1975 a(g165 V\u000a\u000a p1976 tp1977 a(g202 V p1978 tp1979 a(g202 V>>> gen = generate_ints(3) p1980 tp1981 a(g165 V\u000a tp1982 a(g202 V >>> gen\u000a \u000a >>> gen.next()\u000a 0\u000a >>> gen.next()\u000a 1\u000a >>> gen.next()\u000a 2\u000a >>> gen.next()\u000a Traceback (most recent call last):\u000a File "stdin", line 1, in ?\u000a File "stdin", line 2, in generate_ints\u000a StopIteration\u000a\u000a p1983 tp1984 a(g165 VYou could equally write p1985 tp1986 a(g202 V`` p1987 tp1988 a(g202 Vfor i in generate_ints(5) p1989 tp1990 a(g202 V`` p1991 tp1992 a(g165 V, or p1993 tp1994 a(g165 V\u000a tp1995 a(g202 V`` p1996 tp1997 a(g202 Va,b,c = generate_ints(3) p1998 tp1999 a(g202 V`` p2000 tp2001 a(g165 V. tp2002 a(g165 V\u000a tp2003 a(g165 V\u000a tp2004 a(g165 VInside a generator function, the p2005 tp2006 a(g202 V`` p2007 tp2008 a(g202 Vreturn p2009 tp2010 a(g202 V`` p2011 tp2012 a(g165 V statement can only be used p2013 tp2014 a(g165 V\u000a tp2015 a(g165 Vwithout a value, and signals the end of the procession of values; p2016 tp2017 a(g165 V\u000a tp2018 a(g165 Vafter executing a p2019 tp2020 a(g202 V`` p2021 tp2022 a(g202 Vreturn p2023 tp2024 a(g202 V`` p2025 tp2026 a(g165 V the generator cannot return any further p2027 tp2028 a(g165 V\u000a tp2029 a(g165 Vvalues. p2030 tp2031 a(g202 V`` p2032 tp2033 a(g202 Vreturn p2034 tp2035 a(g202 V`` p2036 tp2037 a(g165 V with a value, such as p2038 tp2039 a(g202 V`` p2040 tp2041 a(g202 Vreturn 5 p2042 tp2043 a(g202 V`` p2044 tp2045 a(g165 V, is a syntax p2046 tp2047 a(g165 V\u000a tp2048 a(g165 Verror inside a generator function. The end of the generator's results p2049 tp2050 a(g165 V\u000a tp2051 a(g165 Vcan also be indicated by raising p2052 tp2053 a(g202 V`` p2054 tp2055 a(g202 VStopIteration p2056 tp2057 a(g202 V`` p2058 tp2059 a(g165 V manually, or by p2060 tp2061 a(g165 V\u000a tp2062 a(g165 Vjust letting the flow of execution fall off the bottom of the p2063 tp2064 a(g165 V\u000a tp2065 a(g165 Vfunction. p2066 tp2067 a(g165 V\u000a tp2068 a(g165 V\u000a tp2069 a(g165 VYou could achieve the effect of generators manually by writing your p2070 tp2071 a(g165 V\u000a tp2072 a(g165 Vown class and storing all the local variables of the generator as p2073 tp2074 a(g165 V\u000a tp2075 a(g165 Vinstance variables. For example, returning a list of integers could p2076 tp2077 a(g165 V\u000a tp2078 a(g165 Vbe done by setting p2079 tp2080 a(g202 V`` p2081 tp2082 a(g202 Vself.count p2083 tp2084 a(g202 V`` p2085 tp2086 a(g165 V to 0, and having the p2087 tp2088 a(g165 V\u000a tp2089 a(g202 V`` p2090 tp2091 a(g202 Vnext() p2092 tp2093 a(g202 V`` p2094 tp2095 a(g165 V method increment p2096 tp2097 a(g202 V`` p2098 tp2099 a(g202 Vself.count p2100 tp2101 a(g202 V`` p2102 tp2103 a(g165 V and return it. p2104 tp2105 a(g165 V\u000a tp2106 a(g165 VHowever, for a moderately complicated generator, writing a p2107 tp2108 a(g165 V\u000a tp2109 a(g165 Vcorresponding class can be much messier. p2110 tp2111 a(g165 V\u000a tp2112 a(g165 V\u000a tp2113 a(g165 VThe test suite included with Python's library, p2114 tp2115 a(g202 V`` p2116 tp2117 a(g202 Vtest_generators.py p2118 tp2119 a(g202 V`` p2120 tp2121 a(g165 V, tp2122 a(g165 V\u000a tp2123 a(g165 Vcontains a number of more interesting examples. Here's one generator p2124 tp2125 a(g165 V\u000a tp2126 a(g165 Vthat implements an in-order traversal of a tree using generators p2127 tp2128 a(g165 V\u000a tp2129 a(g165 Vrecursively. p2130 tp2131 a(g165 V\u000a tp2132 a(g165 V\u000a tp2133 a(g228 V:: p2134 tp2135 a(g165 V\u000a\u000a p2136 tp2137 a(g202 V p2138 tp2139 a(g202 V# A recursive generator that generates Tree leaves in in-order. p2140 tp2141 a(g165 V\u000a tp2142 a(g202 V def inorder(t):\u000a if t:\u000a for x in inorder(t.left):\u000a yield x\u000a\u000a yield t.label\u000a\u000a for x in inorder(t.right):\u000a yield x\u000a\u000a p2143 tp2144 a(g165 VTwo other examples in p2145 tp2146 a(g202 V`` p2147 tp2148 a(g202 Vtest_generators.py p2149 tp2150 a(g202 V`` p2151 tp2152 a(g165 V produce p2153 tp2154 a(g165 V\u000a tp2155 a(g165 Vsolutions for the N-Queens problem (placing N queens on an NxN p2156 tp2157 a(g165 V\u000a tp2158 a(g165 Vchess board so that no queen threatens another) and the Knight's Tour p2159 tp2160 a(g165 V\u000a tp2161 a(g165 V(finding a route that takes a knight to every square of an NxN chessboard p2162 tp2163 a(g165 V\u000a tp2164 a(g165 Vwithout visiting any square twice). p2165 tp2166 a(g165 V\u000a tp2167 a(g165 V\u000a tp2168 a(g165 V\u000a tp2169 a(g165 V\u000a tp2170 a(g7 VPassing values into a generator p2171 tp2172 a(g165 V\u000a tp2173 a(g7 V'''''''''''''''''''''''''''''''''''''''''''''' p2174 tp2175 a(g165 V\u000a tp2176 a(g165 V\u000a tp2177 a(g165 VIn Python 2.4 and earlier, generators only produced output. Once a p2178 tp2179 a(g165 V\u000a tp2180 a(g165 Vgenerator's code was invoked to create an iterator, there was no way to p2181 tp2182 a(g165 V\u000a tp2183 a(g165 Vpass any new information into the function when its execution is p2184 tp2185 a(g165 V\u000a tp2186 a(g165 Vresumed. You could hack together this ability by making the p2187 tp2188 a(g165 V\u000a tp2189 a(g165 Vgenerator look at a global variable or by passing in some mutable object p2190 tp2191 a(g165 V\u000a tp2192 a(g165 Vthat callers then modify, but these approaches are messy. p2193 tp2194 a(g165 V\u000a tp2195 a(g165 V\u000a tp2196 a(g165 VIn Python 2.5 there's a simple way to pass values into a generator. p2197 tp2198 a(g165 V\u000a tp2199 a(g202 V`` p2200 tp2201 a(g202 Vyield p2202 tp2203 a(g202 V`` p2204 tp2205 a(g165 V became an expression, returning a value that can be assigned p2206 tp2207 a(g165 V\u000a tp2208 a(g165 Vto a variable or otherwise operated on p2209 tp2210 a(g228 V:: p2211 tp2212 a(g165 V\u000a\u000a p2213 tp2214 a(g202 V p2215 tp2216 a(g202 Vval = (yield i) p2217 tp2218 a(g165 V\u000a tp2219 a(g202 V\u000a tp2220 a(g165 VI recommend that you p2221 tp2222 a(g359 V**always** p2223 tp2224 a(g165 V put parentheses around a p2225 tp2226 a(g202 V`` p2227 tp2228 a(g202 Vyield p2229 tp2230 a(g202 V`` p2231 tp2232 a(g165 V\u000a tp2233 a(g165 Vexpression when you're doing something with the returned value, as in p2234 tp2235 a(g165 V\u000a tp2236 a(g165 Vthe above example. The parentheses aren't always necessary, but it's p2237 tp2238 a(g165 V\u000a tp2239 a(g165 Veasier to always add them instead of having to remember when they're p2240 tp2241 a(g165 V\u000a tp2242 a(g165 Vneeded. p2243 tp2244 a(g165 V\u000a tp2245 a(g165 V\u000a tp2246 a(g165 V(PEP 342 explains the exact rules, which are that a p2247 tp2248 a(g165 V\u000a tp2249 a(g202 V`` p2250 tp2251 a(g202 Vyield p2252 tp2253 a(g202 V`` p2254 tp2255 a(g165 V-expression must always be parenthesized except when it p2256 tp2257 a(g165 V\u000a tp2258 a(g165 Voccurs at the top-level expression on the right-hand side of an p2259 tp2260 a(g165 V\u000a tp2261 a(g165 Vassignment. This means you can write p2262 tp2263 a(g202 V`` p2264 tp2265 a(g202 Vval = yield i p2266 tp2267 a(g202 V`` p2268 tp2269 a(g165 V but have to p2270 tp2271 a(g165 V\u000a tp2272 a(g165 Vuse parentheses when there's an operation, as in p2273 tp2274 a(g202 V`` p2275 tp2276 a(g202 Vval = (yield i)\u000a+ 12 p2277 tp2278 a(g202 V`` p2279 tp2280 a(g165 V.) p2281 tp2282 a(g165 V\u000a tp2283 a(g165 V\u000a tp2284 a(g165 VValues are sent into a generator by calling its p2285 tp2286 a(g165 V\u000a tp2287 a(g202 V`` p2288 tp2289 a(g202 Vsend(value) p2290 tp2291 a(g202 V`` p2292 tp2293 a(g165 V method. This method resumes the p2294 tp2295 a(g165 V\u000a tp2296 a(g165 Vgenerator's code and the p2297 tp2298 a(g202 V`` p2299 tp2300 a(g202 Vyield p2301 tp2302 a(g202 V`` p2303 tp2304 a(g165 V expression returns the specified p2305 tp2306 a(g165 V\u000a tp2307 a(g165 Vvalue. If the regular p2308 tp2309 a(g202 V`` p2310 tp2311 a(g202 Vnext() p2312 tp2313 a(g202 V`` p2314 tp2315 a(g165 V method is called, the p2316 tp2317 a(g165 V\u000a tp2318 a(g202 V`` p2319 tp2320 a(g202 Vyield p2321 tp2322 a(g202 V`` p2323 tp2324 a(g165 V returns p2325 tp2326 a(g202 V`` p2327 tp2328 a(g202 VNone p2329 tp2330 a(g202 V`` p2331 tp2332 a(g165 V. tp2333 a(g165 V\u000a tp2334 a(g165 V\u000a tp2335 a(g165 VHere's a simple counter that increments by 1 and allows changing the p2336 tp2337 a(g165 V\u000a tp2338 a(g165 Vvalue of the internal counter. p2339 tp2340 a(g165 V\u000a tp2341 a(g165 V\u000a tp2342 a(g228 V:: p2343 tp2344 a(g165 V\u000a\u000a p2345 tp2346 a(g202 V p2347 tp2348 a(g202 Vdef counter (maximum): p2349 tp2350 a(g165 V\u000a tp2351 a(g202 V i = 0\u000a while i < maximum:\u000a val = (yield i)\u000a # If value provided, change counter\u000a if val is not None:\u000a i = val\u000a else:\u000a i += 1\u000a\u000a p2352 tp2353 a(g165 VAnd here's an example of changing the counter p2354 tp2355 a(g165 V: tp2356 a(g165 V\u000a tp2357 a(g165 V\u000a tp2358 a(g165 V >>> it = counter(10) p2359 tp2360 a(g165 V\u000a tp2361 a(g165 V >>> print it.next() p2362 tp2363 a(g165 V\u000a tp2364 a(g165 V 0 p2365 tp2366 a(g165 V\u000a tp2367 a(g165 V >>> print it.next() p2368 tp2369 a(g165 V\u000a tp2370 a(g165 V 1 p2371 tp2372 a(g165 V\u000a tp2373 a(g165 V >>> print it.send(8) p2374 tp2375 a(g165 V\u000a tp2376 a(g165 V 8 p2377 tp2378 a(g165 V\u000a tp2379 a(g165 V >>> print it.next() p2380 tp2381 a(g165 V\u000a tp2382 a(g165 V 9 p2383 tp2384 a(g165 V\u000a tp2385 a(g165 V >>> print it.next() p2386 tp2387 a(g165 V\u000a tp2388 a(g165 V Traceback (most recent call last) p2389 tp2390 a(g165 V: tp2391 a(g165 V\u000a tp2392 a(g165 V File p2393 tp2394 a(g202 V`` p2395 tp2396 a(g202 Vt.py'', line 15, in ?\u000a print it.next()\u000a StopIteration\u000a p2397 tp2398 a(g165 V\u000a tp2399 a(g165 VBecause p2400 tp2401 a(g202 V`` p2402 tp2403 a(g202 Vyield p2404 tp2405 a(g202 V`` p2406 tp2407 a(g165 V will often be returning p2408 tp2409 a(g202 V`` p2410 tp2411 a(g202 VNone p2412 tp2413 a(g202 V`` p2414 tp2415 a(g165 V, you p2416 tp2417 a(g165 V\u000a tp2418 a(g165 Vshould always check for this case. Don't just use its value in p2419 tp2420 a(g165 V\u000a tp2421 a(g165 Vexpressions unless you're sure that the p2422 tp2423 a(g202 V`` p2424 tp2425 a(g202 Vsend() p2426 tp2427 a(g202 V`` p2428 tp2429 a(g165 V method p2430 tp2431 a(g165 V\u000a tp2432 a(g165 Vwill be the only method used resume your generator function. p2433 tp2434 a(g165 V\u000a tp2435 a(g165 V\u000a tp2436 a(g165 VIn addition to p2437 tp2438 a(g202 V`` p2439 tp2440 a(g202 Vsend() p2441 tp2442 a(g202 V`` p2443 tp2444 a(g165 V, there are two other new methods on p2445 tp2446 a(g165 V\u000a tp2447 a(g165 Vgenerators p2448 tp2449 a(g165 V: tp2450 a(g165 V\u000a tp2451 a(g165 V\u000a tp2452 a(g189 V* tp2453 a(g165 V tp2454 a(g202 V`` p2455 tp2456 a(g202 Vthrow(type, value=None, traceback=None) p2457 tp2458 a(g202 V`` p2459 tp2460 a(g165 V is used to raise an exception inside the p2461 tp2462 a(g165 V\u000a tp2463 a(g165 V generator; the exception is raised by the p2464 tp2465 a(g202 V`` p2466 tp2467 a(g202 Vyield p2468 tp2469 a(g202 V`` p2470 tp2471 a(g165 V expression p2472 tp2473 a(g165 V\u000a tp2474 a(g165 V where the generator's execution is paused. p2475 tp2476 a(g165 V\u000a tp2477 a(g165 V\u000a tp2478 a(g189 V* tp2479 a(g165 V tp2480 a(g202 V`` p2481 tp2482 a(g202 Vclose() p2483 tp2484 a(g202 V`` p2485 tp2486 a(g165 V raises a p2487 tp2488 a(g202 V`` p2489 tp2490 a(g202 VGeneratorExit p2491 tp2492 a(g202 V`` p2493 tp2494 a(g165 V\u000a tp2495 a(g165 V exception inside the generator to terminate the iteration. p2496 tp2497 a(g165 V\u000a tp2498 a(g165 V On receiving this p2499 tp2500 a(g165 V\u000a tp2501 a(g165 V exception, the generator's code must either raise p2502 tp2503 a(g165 V\u000a tp2504 a(g165 V p2505 tp2506 a(g202 V`` p2507 tp2508 a(g202 VGeneratorExit p2509 tp2510 a(g202 V`` p2511 tp2512 a(g165 V or p2513 tp2514 a(g202 V`` p2515 tp2516 a(g202 VStopIteration p2517 tp2518 a(g202 V`` p2519 tp2520 a(g165 V; catching the p2521 tp2522 a(g165 V\u000a tp2523 a(g165 V exception and doing anything else is illegal and will trigger p2524 tp2525 a(g165 V\u000a tp2526 a(g165 V a p2527 tp2528 a(g202 V`` p2529 tp2530 a(g202 VRuntimeError p2531 tp2532 a(g202 V`` p2533 tp2534 a(g165 V. p2535 tp2536 a(g202 V`` p2537 tp2538 a(g202 Vclose() p2539 tp2540 a(g202 V`` p2541 tp2542 a(g165 V will also be called by p2543 tp2544 a(g165 V\u000a tp2545 a(g165 V Python's garbage collector when the generator is garbage-collected. p2546 tp2547 a(g165 V\u000a tp2548 a(g165 V\u000a tp2549 a(g165 V If you need to run cleanup code when a p2550 tp2551 a(g202 V`` p2552 tp2553 a(g202 VGeneratorExit p2554 tp2555 a(g202 V`` p2556 tp2557 a(g165 V occurs, p2558 tp2559 a(g165 V\u000a tp2560 a(g165 V I suggest using a p2561 tp2562 a(g202 V`` p2563 tp2564 a(g202 Vtry: ... finally: p2565 tp2566 a(g202 V`` p2567 tp2568 a(g165 V suite instead of p2569 tp2570 a(g165 V\u000a tp2571 a(g165 V catching p2572 tp2573 a(g202 V`` p2574 tp2575 a(g202 VGeneratorExit p2576 tp2577 a(g202 V`` p2578 tp2579 a(g165 V. tp2580 a(g165 V\u000a tp2581 a(g165 V\u000a tp2582 a(g165 VThe cumulative effect of these changes is to turn generators from p2583 tp2584 a(g165 V\u000a tp2585 a(g165 Vone-way producers of information into both producers and consumers. p2586 tp2587 a(g165 V\u000a tp2588 a(g165 V\u000a tp2589 a(g165 VGenerators also become p2590 tp2591 a(g359 V**coroutines** p2592 tp2593 a(g165 V, a more generalized form of p2594 tp2595 a(g165 V\u000a tp2596 a(g165 Vsubroutines. Subroutines are entered at one point and exited at p2597 tp2598 a(g165 V\u000a tp2599 a(g165 Vanother point (the top of the function, and a p2600 tp2601 a(g202 V`` p2602 tp2603 a(g202 Vreturn p2604 tp2605 a(g202 V`` p2606 tp2607 a(g165 V\u000a tp2608 a(g165 Vstatement), but coroutines can be entered, exited, and resumed at p2609 tp2610 a(g165 V\u000a tp2611 a(g165 Vmany different points (the p2612 tp2613 a(g202 V`` p2614 tp2615 a(g202 Vyield p2616 tp2617 a(g202 V`` p2618 tp2619 a(g165 V statements). p2620 tp2621 a(g165 V\u000a tp2622 a(g165 V\u000a tp2623 a(g165 V\u000a tp2624 a(g7 VBuilt-in functions p2625 tp2626 a(g165 V\u000a tp2627 a(g7 V---------------------------------------------- p2628 tp2629 a(g165 V\u000a tp2630 a(g165 V\u000a tp2631 a(g165 VLet's look in more detail at built-in functions often used with iterators. p2632 tp2633 a(g165 V\u000a tp2634 a(g165 V\u000a tp2635 a(g165 VTwo Python's built-in functions, p2636 tp2637 a(g202 V`` p2638 tp2639 a(g202 Vmap() p2640 tp2641 a(g202 V`` p2642 tp2643 a(g165 V and p2644 tp2645 a(g202 V`` p2646 tp2647 a(g202 Vfilter() p2648 tp2649 a(g202 V`` p2650 tp2651 a(g165 V, are p2652 tp2653 a(g165 V\u000a tp2654 a(g165 Vsomewhat obsolete; they duplicate the features of list comprehensions p2655 tp2656 a(g165 V\u000a tp2657 a(g165 Vbut return actual lists instead of iterators. p2658 tp2659 a(g165 V\u000a tp2660 a(g165 V\u000a tp2661 a(g202 V`` p2662 tp2663 a(g202 Vmap(f, iterA, iterB, ...) p2664 tp2665 a(g202 V`` p2666 tp2667 a(g165 V returns a list containing p2668 tp2669 a(g202 V`` p2670 tp2671 a(g202 Vf(iterA[0],\u000aiterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), ... p2672 tp2673 a(g202 V`` p2674 tp2675 a(g165 V. p2676 tp2677 a(g165 V\u000a tp2678 a(g165 V\u000a tp2679 a(g228 V:: p2680 tp2681 a(g165 V\u000a\u000a p2682 tp2683 a(g202 V p2684 tp2685 a(g202 Vdef upper(s): p2686 tp2687 a(g165 V\u000a tp2688 a(g202 V return s.upper()\u000a map(upper, ['sentence', 'fragment']) =>\u000a ['SENTENCE', 'FRAGMENT']\u000a\u000a [upper(s) for s in ['sentence', 'fragment']] =>\u000a ['SENTENCE', 'FRAGMENT']\u000a\u000a p2689 tp2690 a(g165 VAs shown above, you can achieve the same effect with a list p2691 tp2692 a(g165 V\u000a tp2693 a(g165 Vcomprehension. The p2694 tp2695 a(g202 V`` p2696 tp2697 a(g202 Vitertools.imap() p2698 tp2699 a(g202 V`` p2700 tp2701 a(g165 V function does the same thing p2702 tp2703 a(g165 V\u000a tp2704 a(g165 Vbut can handle infinite iterators; it'll be discussed later, in the section on p2705 tp2706 a(g165 V\u000a tp2707 a(g165 Vthe p2708 tp2709 a(g202 V`` p2710 tp2711 a(g202 Vitertools p2712 tp2713 a(g202 V`` p2714 tp2715 a(g165 V module. p2716 tp2717 a(g165 V\u000a tp2718 a(g165 V\u000a tp2719 a(g202 V`` p2720 tp2721 a(g202 Vfilter(predicate, iter) p2722 tp2723 a(g202 V`` p2724 tp2725 a(g165 V returns a list p2726 tp2727 a(g165 V\u000a tp2728 a(g165 Vthat contains all the sequence elements that meet a certain condition, p2729 tp2730 a(g165 V\u000a tp2731 a(g165 Vand is similarly duplicated by list comprehensions. p2732 tp2733 a(g165 V\u000a tp2734 a(g165 VA p2735 tp2736 a(g359 V**predicate** p2737 tp2738 a(g165 V is a function that returns the truth value of p2739 tp2740 a(g165 V\u000a tp2741 a(g165 Vsome condition; for use with p2742 tp2743 a(g202 V`` p2744 tp2745 a(g202 Vfilter() p2746 tp2747 a(g202 V`` p2748 tp2749 a(g165 V, the predicate must take a p2750 tp2751 a(g165 V\u000a tp2752 a(g165 Vsingle value. p2753 tp2754 a(g165 V\u000a tp2755 a(g165 V\u000a tp2756 a(g228 V:: p2757 tp2758 a(g165 V\u000a\u000a p2759 tp2760 a(g202 V p2761 tp2762 a(g202 Vdef is_even(x): p2763 tp2764 a(g165 V\u000a tp2765 a(g202 V return (x % 2) == 0\u000a\u000a filter(is_even, range(10)) =>\u000a [0, 2, 4, 6, 8]\u000a\u000a p2766 tp2767 a(g165 VThis can also be written as a list comprehension p2768 tp2769 a(g228 V:: p2770 tp2771 a(g165 V\u000a\u000a p2772 tp2773 a(g202 V p2774 tp2775 a(g202 V>>> [x for x in range(10) if is_even(x)] p2776 tp2777 a(g165 V\u000a tp2778 a(g202 V [0, 2, 4, 6, 8]\u000a\u000a p2779 tp2780 a(g202 V`` p2781 tp2782 a(g202 Vfilter() p2783 tp2784 a(g202 V`` p2785 tp2786 a(g165 V also has a counterpart in the p2787 tp2788 a(g202 V`` p2789 tp2790 a(g202 Vitertools p2791 tp2792 a(g202 V`` p2793 tp2794 a(g165 V module, p2795 tp2796 a(g165 V\u000a tp2797 a(g202 V`` p2798 tp2799 a(g202 Vitertools.ifilter() p2800 tp2801 a(g202 V`` p2802 tp2803 a(g165 V, that returns an iterator and p2804 tp2805 a(g165 V\u000a tp2806 a(g165 Vcan therefore handle infinite sequences just as p2807 tp2808 a(g202 V`` p2809 tp2810 a(g202 Vitertools.imap() p2811 tp2812 a(g202 V`` p2813 tp2814 a(g165 V can. p2815 tp2816 a(g165 V\u000a tp2817 a(g165 V\u000a tp2818 a(g202 V`` p2819 tp2820 a(g202 Vreduce(func, iter, [initial_value]) p2821 tp2822 a(g202 V`` p2823 tp2824 a(g165 V doesn't have a counterpart in p2825 tp2826 a(g165 V\u000a tp2827 a(g165 Vthe p2828 tp2829 a(g202 V`` p2830 tp2831 a(g202 Vitertools p2832 tp2833 a(g202 V`` p2834 tp2835 a(g165 V module because it cumulatively performs an operation p2836 tp2837 a(g165 V\u000a tp2838 a(g165 Von all the iterable's elements and therefore can't be applied to p2839 tp2840 a(g165 V\u000a tp2841 a(g165 Vinfinite iterables. p2842 tp2843 a(g202 V`` p2844 tp2845 a(g202 Vfunc p2846 tp2847 a(g202 V`` p2848 tp2849 a(g165 V must be a function that takes two elements p2850 tp2851 a(g165 V\u000a tp2852 a(g165 Vand returns a single value. p2853 tp2854 a(g202 V`` p2855 tp2856 a(g202 Vreduce() p2857 tp2858 a(g202 V`` p2859 tp2860 a(g165 V takes the first two elements p2861 tp2862 a(g165 V\u000a tp2863 a(g165 VA and B returned by the iterator and calculates p2864 tp2865 a(g202 V`` p2866 tp2867 a(g202 Vfunc(A, B) p2868 tp2869 a(g202 V`` p2870 tp2871 a(g165 V. It p2872 tp2873 a(g165 V\u000a tp2874 a(g165 Vthen requests the third element, C, calculates p2875 tp2876 a(g202 V`` p2877 tp2878 a(g202 Vfunc(func(A, B),\u000aC) p2879 tp2880 a(g202 V`` p2881 tp2882 a(g165 V, combines this result with the fourth element returned, and p2883 tp2884 a(g165 V\u000a tp2885 a(g165 Vcontinues until the iterable is exhausted. If the iterable returns no p2886 tp2887 a(g165 V\u000a tp2888 a(g165 Vvalues at all, a p2889 tp2890 a(g202 V`` p2891 tp2892 a(g202 VTypeError p2893 tp2894 a(g202 V`` p2895 tp2896 a(g165 V exception is raised. If the initial p2897 tp2898 a(g165 V\u000a tp2899 a(g165 Vvalue is supplied, it's used as a starting point and p2900 tp2901 a(g165 V\u000a tp2902 a(g202 V`` p2903 tp2904 a(g202 Vfunc(initial_value, A) p2905 tp2906 a(g202 V`` p2907 tp2908 a(g165 V is the first calculation. p2909 tp2910 a(g165 V\u000a tp2911 a(g165 V\u000a tp2912 a(g228 V:: p2913 tp2914 a(g165 V\u000a\u000a p2915 tp2916 a(g202 V p2917 tp2918 a(g202 Vimport operator p2919 tp2920 a(g165 V\u000a tp2921 a(g202 V reduce(operator.concat, ['A', 'BB', 'C']) =>\u000a 'ABBC'\u000a reduce(operator.concat, []) =>\u000a TypeError: reduce() of empty sequence with no initial value\u000a reduce(operator.mul, [1,2,3], 1) =>\u000a 6\u000a reduce(operator.mul, [], 1) =>\u000a 1\u000a\u000a p2922 tp2923 a(g165 VIf you use p2924 tp2925 a(g202 V`` p2926 tp2927 a(g202 Voperator.add p2928 tp2929 a(g202 V`` p2930 tp2931 a(g165 V with p2932 tp2933 a(g202 V`` p2934 tp2935 a(g202 Vreduce() p2936 tp2937 a(g202 V`` p2938 tp2939 a(g165 V, you'll add up all the p2940 tp2941 a(g165 V\u000a tp2942 a(g165 Velements of the iterable. This case is so common that there's a special p2943 tp2944 a(g165 V\u000a tp2945 a(g165 Vbuilt-in called p2946 tp2947 a(g202 V`` p2948 tp2949 a(g202 Vsum() p2950 tp2951 a(g202 V`` p2952 tp2953 a(g165 V to compute it p2954 tp2955 a(g228 V:: p2956 tp2957 a(g165 V\u000a\u000a p2958 tp2959 a(g202 V p2960 tp2961 a(g202 Vreduce(operator.add, [1,2,3,4], 0) => p2962 tp2963 a(g165 V\u000a tp2964 a(g202 V 10\u000a sum([1,2,3,4]) =>\u000a 10\u000a sum([]) =>\u000a 0\u000a\u000a p2965 tp2966 a(g165 VFor many uses of p2967 tp2968 a(g202 V`` p2969 tp2970 a(g202 Vreduce() p2971 tp2972 a(g202 V`` p2973 tp2974 a(g165 V, though, it can be clearer to just write p2975 tp2976 a(g165 V\u000a tp2977 a(g165 Vthe obvious p2978 tp2979 a(g202 V`` p2980 tp2981 a(g202 Vfor p2982 tp2983 a(g202 V`` p2984 tp2985 a(g165 V loop p2986 tp2987 a(g228 V:: p2988 tp2989 a(g165 V\u000a\u000a p2990 tp2991 a(g202 V p2992 tp2993 a(g202 V# Instead of: p2994 tp2995 a(g165 V\u000a tp2996 a(g202 V product = reduce(operator.mul, [1,2,3], 1)\u000a\u000a # You can write:\u000a product = 1\u000a for i in [1,2,3]:\u000a product *= i\u000a\u000a\u000a p2997 tp2998 a(g202 V`` p2999 tp3000 a(g202 Venumerate(iter) p3001 tp3002 a(g202 V`` p3003 tp3004 a(g165 V counts off the elements in the iterable, returning p3005 tp3006 a(g165 V\u000a tp3007 a(g165 V2-tuples containing the count and each element. p3008 tp3009 a(g165 V\u000a tp3010 a(g165 V\u000a tp3011 a(g228 V:: p3012 tp3013 a(g165 V\u000a\u000a p3014 tp3015 a(g202 V p3016 tp3017 a(g202 Venumerate(['subject', 'verb', 'object']) => p3018 tp3019 a(g165 V\u000a tp3020 a(g202 V (0, 'subject'), (1, 'verb'), (2, 'object')\u000a\u000a p3021 tp3022 a(g202 V`` p3023 tp3024 a(g202 Venumerate() p3025 tp3026 a(g202 V`` p3027 tp3028 a(g165 V is often used when looping through a list p3029 tp3030 a(g165 V\u000a tp3031 a(g165 Vand recording the indexes at which certain conditions are met p3032 tp3033 a(g228 V:: p3034 tp3035 a(g165 V\u000a\u000a p3036 tp3037 a(g202 V p3038 tp3039 a(g202 Vf = open('data.txt', 'r') p3040 tp3041 a(g165 V\u000a tp3042 a(g202 V for i, line in enumerate(f):\u000a if line.strip() == '':\u000a print 'Blank line at line #%i' % i\u000a\u000a p3043 tp3044 a(g202 V`` p3045 tp3046 a(g202 Vsorted(iterable, [cmp=None], [key=None], [reverse=False) p3047 tp3048 a(g202 V`` p3049 tp3050 a(g165 V tp3051 a(g165 V\u000a tp3052 a(g165 Vcollects all the elements of the iterable into a list, sorts p3053 tp3054 a(g165 V\u000a tp3055 a(g165 Vthe list, and returns the sorted result. The p3056 tp3057 a(g202 V`` p3058 tp3059 a(g202 Vcmp p3060 tp3061 a(g202 V`` p3062 tp3063 a(g165 V, p3064 tp3065 a(g202 V`` p3066 tp3067 a(g202 Vkey p3068 tp3069 a(g202 V`` p3070 tp3071 a(g165 V, p3072 tp3073 a(g165 V\u000a tp3074 a(g165 Vand p3075 tp3076 a(g202 V`` p3077 tp3078 a(g202 Vreverse p3079 tp3080 a(g202 V`` p3081 tp3082 a(g165 V arguments are passed through to the p3083 tp3084 a(g165 V\u000a tp3085 a(g165 Vconstructed list's p3086 tp3087 a(g202 V`` p3088 tp3089 a(g202 V.sort() p3090 tp3091 a(g202 V`` p3092 tp3093 a(g165 V method. p3094 tp3095 a(g165 V\u000a tp3096 a(g165 V\u000a tp3097 a(g228 V:: p3098 tp3099 a(g165 V\u000a\u000a p3100 tp3101 a(g202 V p3102 tp3103 a(g202 Vimport random p3104 tp3105 a(g165 V\u000a tp3106 a(g202 V # Generate 8 random numbers between [0, 10000)\u000a rand_list = random.sample(range(10000), 8)\u000a rand_list =>\u000a [769, 7953, 9828, 6431, 8442, 9878, 6213, 2207]\u000a sorted(rand_list) =>\u000a [769, 2207, 6213, 6431, 7953, 8442, 9828, 9878]\u000a sorted(rand_list, reverse=True) =>\u000a [9878, 9828, 8442, 7953, 6431, 6213, 2207, 769]\u000a\u000a p3107 tp3108 a(g165 V(For a more detailed discussion of sorting, see the Sorting mini-HOWTO p3109 tp3110 a(g165 V\u000a tp3111 a(g165 Vin the Python wiki at http p3112 tp3113 a(g165 V: tp3114 a(g165 V//wiki.python.org/moin/HowTo/Sorting.) p3115 tp3116 a(g165 V\u000a tp3117 a(g165 V\u000a tp3118 a(g165 VThe p3119 tp3120 a(g202 V`` p3121 tp3122 a(g202 Vany(iter) p3123 tp3124 a(g202 V`` p3125 tp3126 a(g165 V and p3127 tp3128 a(g202 V`` p3129 tp3130 a(g202 Vall(iter) p3131 tp3132 a(g202 V`` p3133 tp3134 a(g165 V built-ins look at p3135 tp3136 a(g165 V\u000a tp3137 a(g165 Vthe truth values of an iterable's contents. p3138 tp3139 a(g202 V`` p3140 tp3141 a(g202 Vany() p3142 tp3143 a(g202 V`` p3144 tp3145 a(g165 V returns p3146 tp3147 a(g165 V\u000a tp3148 a(g165 VTrue if any element in the iterable is a true value, and p3149 tp3150 a(g202 V`` p3151 tp3152 a(g202 Vall() p3153 tp3154 a(g202 V`` p3155 tp3156 a(g165 V tp3157 a(g165 V\u000a tp3158 a(g165 Vreturns True if all of the elements are true values p3159 tp3160 a(g228 V:: p3161 tp3162 a(g165 V\u000a\u000a p3163 tp3164 a(g202 V p3165 tp3166 a(g202 Vany([0,1,0]) => p3167 tp3168 a(g165 V\u000a tp3169 a(g202 V True\u000a any([0,0,0]) =>\u000a False\u000a any([1,1,1]) =>\u000a True\u000a all([0,1,0]) =>\u000a False\u000a all([0,0,0]) => \u000a False\u000a all([1,1,1]) =>\u000a True\u000a\u000a\u000a p3170 tp3171 a(g7 VSmall functions and the lambda statement p3172 tp3173 a(g165 V\u000a tp3174 a(g7 V---------------------------------------------- p3175 tp3176 a(g165 V\u000a tp3177 a(g165 V\u000a tp3178 a(g165 VWhen writing functional-style programs, you'll often need little p3179 tp3180 a(g165 V\u000a tp3181 a(g165 Vfunctions that act as predicates or that combine elements in some way. p3182 tp3183 a(g165 V\u000a tp3184 a(g165 V\u000a tp3185 a(g165 VIf there's a Python built-in or a module function that's suitable, you p3186 tp3187 a(g165 V\u000a tp3188 a(g165 Vdon't need to define a new function at all p3189 tp3190 a(g228 V:: p3191 tp3192 a(g165 V\u000a\u000a p3193 tp3194 a(g202 V p3195 tp3196 a(g202 Vstripped_lines = [line.strip() for line in lines] p3197 tp3198 a(g165 V\u000a tp3199 a(g202 V existing_files = filter(os.path.exists, file_list)\u000a\u000a p3200 tp3201 a(g165 VIf the function you need doesn't exist, you need to write it. One way p3202 tp3203 a(g165 V\u000a tp3204 a(g165 Vto write small functions is to use the p3205 tp3206 a(g202 V`` p3207 tp3208 a(g202 Vlambda p3209 tp3210 a(g202 V`` p3211 tp3212 a(g165 V statement. p3213 tp3214 a(g202 V`` p3215 tp3216 a(g202 Vlambda p3217 tp3218 a(g202 V`` p3219 tp3220 a(g165 V\u000a tp3221 a(g165 Vtakes a number of parameters and an expression combining these parameters, p3222 tp3223 a(g165 V\u000a tp3224 a(g165 Vand creates a small function that returns the value of the expression p3225 tp3226 a(g228 V:: p3227 tp3228 a(g165 V\u000a\u000a p3229 tp3230 a(g202 V p3231 tp3232 a(g202 Vlowercase = lambda x: x.lower() p3233 tp3234 a(g165 V\u000a tp3235 a(g202 V\u000a print_assign = lambda name, value: name + '=' + str(value)\u000a\u000a adder = lambda x, y: x+y\u000a\u000a p3236 tp3237 a(g165 VAn alternative is to just use the p3238 tp3239 a(g202 V`` p3240 tp3241 a(g202 Vdef p3242 tp3243 a(g202 V`` p3244 tp3245 a(g165 V statement and define a p3246 tp3247 a(g165 V\u000a tp3248 a(g165 Vfunction in the usual way p3249 tp3250 a(g228 V:: p3251 tp3252 a(g165 V\u000a\u000a p3253 tp3254 a(g202 V p3255 tp3256 a(g202 Vdef lowercase(x): p3257 tp3258 a(g165 V\u000a tp3259 a(g202 V return x.lower()\u000a\u000a def print_assign(name, value):\u000a return name + '=' + str(value)\u000a\u000a def adder(x,y):\u000a return x + y\u000a\u000a p3260 tp3261 a(g165 VWhich alternative is preferable? That's a style question; my usual p3262 tp3263 a(g165 V\u000a tp3264 a(g165 Vcourse is to avoid using p3265 tp3266 a(g202 V`` p3267 tp3268 a(g202 Vlambda p3269 tp3270 a(g202 V`` p3271 tp3272 a(g165 V. tp3273 a(g165 V\u000a tp3274 a(g165 V\u000a tp3275 a(g165 VOne reason for my preference is that p3276 tp3277 a(g202 V`` p3278 tp3279 a(g202 Vlambda p3280 tp3281 a(g202 V`` p3282 tp3283 a(g165 V is quite limited in p3284 tp3285 a(g165 V\u000a tp3286 a(g165 Vthe functions it can define. The result has to be computable as a p3287 tp3288 a(g165 V\u000a tp3289 a(g165 Vsingle expression, which means you can't have multiway p3290 tp3291 a(g165 V\u000a tp3292 a(g202 V`` p3293 tp3294 a(g202 Vif... elif... else p3295 tp3296 a(g202 V`` p3297 tp3298 a(g165 V comparisons or p3299 tp3300 a(g202 V`` p3301 tp3302 a(g202 Vtry... except p3303 tp3304 a(g202 V`` p3305 tp3306 a(g165 V statements. p3307 tp3308 a(g165 V\u000a tp3309 a(g165 VIf you try to do too much in a p3310 tp3311 a(g202 V`` p3312 tp3313 a(g202 Vlambda p3314 tp3315 a(g202 V`` p3316 tp3317 a(g165 V statement, you'll end up p3318 tp3319 a(g165 V\u000a tp3320 a(g165 Vwith an overly complicated expression that's hard to read. Quick, p3321 tp3322 a(g165 V\u000a tp3323 a(g165 Vwhat's the following code doing? p3324 tp3325 a(g165 V\u000a tp3326 a(g165 V\u000a tp3327 a(g228 V:: p3328 tp3329 a(g165 V\u000a\u000a p3330 tp3331 a(g202 V p3332 tp3333 a(g202 Vtotal = reduce(lambda a, b: (0, a[1] + b[1]), items)[1] p3334 tp3335 a(g165 V\u000a tp3336 a(g202 V\u000a tp3337 a(g165 VYou can figure it out, but it takes time to disentangle the expression p3338 tp3339 a(g165 V\u000a tp3340 a(g165 Vto figure out what's going on. Using a short nested p3341 tp3342 a(g165 V\u000a tp3343 a(g202 V`` p3344 tp3345 a(g202 Vdef p3346 tp3347 a(g202 V`` p3348 tp3349 a(g165 V statements makes things a little bit better p3350 tp3351 a(g228 V:: p3352 tp3353 a(g165 V\u000a\u000a p3354 tp3355 a(g202 V p3356 tp3357 a(g202 Vdef combine (a, b): p3358 tp3359 a(g165 V\u000a tp3360 a(g202 V return 0, a[1] + b[1]\u000a\u000a total = reduce(combine, items)[1]\u000a\u000a p3361 tp3362 a(g165 VBut it would be best of all if I had simply used a p3363 tp3364 a(g202 V`` p3365 tp3366 a(g202 Vfor p3367 tp3368 a(g202 V`` p3369 tp3370 a(g165 V loop p3371 tp3372 a(g228 V:: p3373 tp3374 a(g165 V\u000a\u000a p3375 tp3376 a(g202 V p3377 tp3378 a(g202 Vtotal = 0 p3379 tp3380 a(g165 V\u000a tp3381 a(g202 V for a, b in items:\u000a total += b\u000a\u000a p3382 tp3383 a(g165 VOr the p3384 tp3385 a(g202 V`` p3386 tp3387 a(g202 Vsum() p3388 tp3389 a(g202 V`` p3390 tp3391 a(g165 V built-in and a generator expression p3392 tp3393 a(g228 V:: p3394 tp3395 a(g165 V\u000a\u000a p3396 tp3397 a(g202 V p3398 tp3399 a(g202 Vtotal = sum(b for a,b in items) p3400 tp3401 a(g165 V\u000a tp3402 a(g202 V\u000a tp3403 a(g165 VMany uses of p3404 tp3405 a(g202 V`` p3406 tp3407 a(g202 Vreduce() p3408 tp3409 a(g202 V`` p3410 tp3411 a(g165 V are clearer when written as p3412 tp3413 a(g202 V`` p3414 tp3415 a(g202 Vfor p3416 tp3417 a(g202 V`` p3418 tp3419 a(g165 V loops. p3420 tp3421 a(g165 V\u000a tp3422 a(g165 V\u000a tp3423 a(g165 VFredrik Lundh once suggested the following set of rules for refactoring p3424 tp3425 a(g165 V\u000a tp3426 a(g165 Vuses of p3427 tp3428 a(g202 V`` p3429 tp3430 a(g202 Vlambda p3431 tp3432 a(g202 V`` p3433 tp3434 a(g165 V: tp3435 a(g165 V\u000a tp3436 a(g165 V\u000a tp3437 a(g189 V1) p3438 tp3439 a(g165 V Write a lambda function. p3440 tp3441 a(g165 V\u000a tp3442 a(g189 V2) p3443 tp3444 a(g165 V Write a comment explaining what the heck that lambda does. p3445 tp3446 a(g165 V\u000a tp3447 a(g189 V3) p3448 tp3449 a(g165 V Study the comment for a while, and think of a name that captures p3450 tp3451 a(g165 V\u000a tp3452 a(g165 V the essence of the comment. p3453 tp3454 a(g165 V\u000a tp3455 a(g189 V4) p3456 tp3457 a(g165 V Convert the lambda to a def statement, using that name. p3458 tp3459 a(g165 V\u000a tp3460 a(g189 V5) p3461 tp3462 a(g165 V Remove the comment. p3463 tp3464 a(g165 V\u000a tp3465 a(g165 V\u000a tp3466 a(g165 VI really like these rules, but you're free to disagree that this p3467 tp3468 a(g165 V\u000a tp3469 a(g165 Vlambda-free style is better. p3470 tp3471 a(g165 V\u000a tp3472 a(g165 V\u000a tp3473 a(g165 V\u000a tp3474 a(g7 VThe itertools module p3475 tp3476 a(g165 V\u000a tp3477 a(g7 V----------------------- p3478 tp3479 a(g165 V\u000a tp3480 a(g165 V\u000a tp3481 a(g165 VThe p3482 tp3483 a(g202 V`` p3484 tp3485 a(g202 Vitertools p3486 tp3487 a(g202 V`` p3488 tp3489 a(g165 V module contains a number of commonly-used iterators p3490 tp3491 a(g165 V\u000a tp3492 a(g165 Vas well as functions for combining several iterators. This section p3493 tp3494 a(g165 V\u000a tp3495 a(g165 Vwill introduce the module's contents by showing small examples. p3496 tp3497 a(g165 V\u000a tp3498 a(g165 V\u000a tp3499 a(g165 VThe module's functions fall into a few broad classes p3500 tp3501 a(g165 V: tp3502 a(g165 V\u000a tp3503 a(g165 V\u000a tp3504 a(g189 V* tp3505 a(g165 V Functions that create a new iterator based on an existing iterator. p3506 tp3507 a(g165 V\u000a tp3508 a(g189 V* tp3509 a(g165 V Functions for treating an iterator's elements as function arguments. p3510 tp3511 a(g165 V\u000a tp3512 a(g189 V* tp3513 a(g165 V Functions for selecting portions of an iterator's output. p3514 tp3515 a(g165 V\u000a tp3516 a(g189 V* tp3517 a(g165 V A function for grouping an iterator's output. p3518 tp3519 a(g165 V\u000a tp3520 a(g165 V\u000a tp3521 a(g7 VCreating new iterators p3522 tp3523 a(g165 V\u000a tp3524 a(g7 V'''''''''''''''''''''' p3525 tp3526 a(g165 V\u000a tp3527 a(g165 V\u000a tp3528 a(g202 V`` p3529 tp3530 a(g202 Vitertools.count(n) p3531 tp3532 a(g202 V`` p3533 tp3534 a(g165 V returns an infinite stream of p3535 tp3536 a(g165 V\u000a tp3537 a(g165 Vintegers, increasing by 1 each time. You can optionally supply the p3538 tp3539 a(g165 V\u000a tp3540 a(g165 Vstarting number, which defaults to 0 p3541 tp3542 a(g228 V:: p3543 tp3544 a(g165 V\u000a\u000a p3545 tp3546 a(g202 V p3547 tp3548 a(g202 Vitertools.count() => p3549 tp3550 a(g165 V\u000a tp3551 a(g202 V 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...\u000a itertools.count(10) =>\u000a 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ...\u000a\u000a p3552 tp3553 a(g202 V`` p3554 tp3555 a(g202 Vitertools.cycle(iter) p3556 tp3557 a(g202 V`` p3558 tp3559 a(g165 V saves a copy of the contents of a provided p3560 tp3561 a(g165 V\u000a tp3562 a(g165 Viterable and returns a new iterator that returns its elements from p3563 tp3564 a(g165 V\u000a tp3565 a(g165 Vfirst to last. The new iterator will repeat these elements infinitely. p3566 tp3567 a(g165 V\u000a tp3568 a(g165 V\u000a tp3569 a(g228 V:: p3570 tp3571 a(g165 V\u000a\u000a p3572 tp3573 a(g202 V p3574 tp3575 a(g202 Vitertools.cycle([1,2,3,4,5]) => p3576 tp3577 a(g165 V\u000a tp3578 a(g202 V 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...\u000a\u000a p3579 tp3580 a(g202 V`` p3581 tp3582 a(g202 Vitertools.repeat(elem, [n]) p3583 tp3584 a(g202 V`` p3585 tp3586 a(g165 V returns the provided element p3587 tp3588 a(g202 V`` p3589 tp3590 a(g202 Vn tp3591 a(g202 V`` p3592 tp3593 a(g165 V\u000a tp3594 a(g165 Vtimes, or returns the element endlessly if p3595 tp3596 a(g202 V`` p3597 tp3598 a(g202 Vn tp3599 a(g202 V`` p3600 tp3601 a(g165 V is not provided. p3602 tp3603 a(g165 V\u000a tp3604 a(g165 V\u000a tp3605 a(g228 V:: p3606 tp3607 a(g165 V\u000a\u000a p3608 tp3609 a(g202 V p3610 tp3611 a(g202 Vitertools.repeat('abc') => p3612 tp3613 a(g165 V\u000a tp3614 a(g202 V abc, abc, abc, abc, abc, abc, abc, abc, abc, abc, ...\u000a itertools.repeat('abc', 5) =>\u000a abc, abc, abc, abc, abc\u000a\u000a p3615 tp3616 a(g202 V`` p3617 tp3618 a(g202 Vitertools.chain(iterA, iterB, ...) p3619 tp3620 a(g202 V`` p3621 tp3622 a(g165 V takes an arbitrary number of p3623 tp3624 a(g165 V\u000a tp3625 a(g165 Viterables as input, and returns all the elements of the first p3626 tp3627 a(g165 V\u000a tp3628 a(g165 Viterator, then all the elements of the second, and so on, until all of p3629 tp3630 a(g165 V\u000a tp3631 a(g165 Vthe iterables have been exhausted. p3632 tp3633 a(g165 V\u000a tp3634 a(g165 V\u000a tp3635 a(g228 V:: p3636 tp3637 a(g165 V\u000a\u000a p3638 tp3639 a(g202 V p3640 tp3641 a(g202 Vitertools.chain(['a', 'b', 'c'], (1, 2, 3)) => p3642 tp3643 a(g165 V\u000a tp3644 a(g202 V a, b, c, 1, 2, 3\u000a\u000a p3645 tp3646 a(g202 V`` p3647 tp3648 a(g202 Vitertools.izip(iterA, iterB, ...) p3649 tp3650 a(g202 V`` p3651 tp3652 a(g165 V takes one element from each iterable p3653 tp3654 a(g165 V\u000a tp3655 a(g165 Vand returns them in a tuple p3656 tp3657 a(g228 V:: p3658 tp3659 a(g165 V\u000a\u000a p3660 tp3661 a(g202 V p3662 tp3663 a(g202 Vitertools.izip(['a', 'b', 'c'], (1, 2, 3)) => p3664 tp3665 a(g165 V\u000a tp3666 a(g202 V ('a', 1), ('b', 2), ('c', 3)\u000a\u000a p3667 tp3668 a(g165 VIt's similiar to the built-in p3669 tp3670 a(g202 V`` p3671 tp3672 a(g202 Vzip() p3673 tp3674 a(g202 V`` p3675 tp3676 a(g165 V function, but doesn't p3677 tp3678 a(g165 V\u000a tp3679 a(g165 Vconstruct an in-memory list and exhaust all the input iterators before p3680 tp3681 a(g165 V\u000a tp3682 a(g165 Vreturning; instead tuples are constructed and returned only if they're p3683 tp3684 a(g165 V\u000a tp3685 a(g165 Vrequested. (The technical term for this behaviour is p3686 tp3687 a(g165 V\u000a tp3688 a(g202 V`lazy evaluation p3689 tp3690 a(g209 V p3691 tp3692 a(g202 V`__ p3693 tp3694 a(g165 V.) p3695 tp3696 a(g165 V\u000a tp3697 a(g165 V\u000a tp3698 a(g165 VThis iterator is intended to be used with iterables that are all of p3699 tp3700 a(g165 V\u000a tp3701 a(g165 Vthe same length. If the iterables are of different lengths, the p3702 tp3703 a(g165 V\u000a tp3704 a(g165 Vresulting stream will be the same length as the shortest iterable. p3705 tp3706 a(g165 V\u000a tp3707 a(g165 V\u000a tp3708 a(g228 V:: p3709 tp3710 a(g165 V\u000a\u000a p3711 tp3712 a(g202 V p3713 tp3714 a(g202 Vitertools.izip(['a', 'b'], (1, 2, 3)) => p3715 tp3716 a(g165 V\u000a tp3717 a(g202 V ('a', 1), ('b', 2)\u000a\u000a p3718 tp3719 a(g165 VYou should avoid doing this, though, because an element may be taken p3720 tp3721 a(g165 V\u000a tp3722 a(g165 Vfrom the longer iterators and discarded. This means you can't go on p3723 tp3724 a(g165 V\u000a tp3725 a(g165 Vto use the iterators further because you risk skipping a discarded p3726 tp3727 a(g165 V\u000a tp3728 a(g165 Velement. p3729 tp3730 a(g165 V\u000a tp3731 a(g165 V\u000a tp3732 a(g202 V`` p3733 tp3734 a(g202 Vitertools.islice(iter, [start], stop, [step]) p3735 tp3736 a(g202 V`` p3737 tp3738 a(g165 V returns a stream p3739 tp3740 a(g165 V\u000a tp3741 a(g165 Vthat's a slice of the iterator. With a single p3742 tp3743 a(g202 V`` p3744 tp3745 a(g202 Vstop p3746 tp3747 a(g202 V`` p3748 tp3749 a(g165 V argument, p3750 tp3751 a(g165 V\u000a tp3752 a(g165 Vit will return the first p3753 tp3754 a(g202 V`` p3755 tp3756 a(g202 Vstop p3757 tp3758 a(g202 V`` p3759 tp3760 a(g165 V\u000a tp3761 a(g165 Velements. If you supply a starting index, you'll get p3762 tp3763 a(g202 V`` p3764 tp3765 a(g202 Vstop-start p3766 tp3767 a(g202 V`` p3768 tp3769 a(g165 V\u000a tp3770 a(g165 Velements, and if you supply a value for p3771 tp3772 a(g202 V`` p3773 tp3774 a(g202 Vstep p3775 tp3776 a(g202 V`` p3777 tp3778 a(g165 V, elements will be p3779 tp3780 a(g165 V\u000a tp3781 a(g165 Vskipped accordingly. Unlike Python's string and list slicing, you p3782 tp3783 a(g165 V\u000a tp3784 a(g165 Vcan't use negative values for p3785 tp3786 a(g202 V`` p3787 tp3788 a(g202 Vstart p3789 tp3790 a(g202 V`` p3791 tp3792 a(g165 V, p3793 tp3794 a(g202 V`` p3795 tp3796 a(g202 Vstop p3797 tp3798 a(g202 V`` p3799 tp3800 a(g165 V, or p3801 tp3802 a(g202 V`` p3803 tp3804 a(g202 Vstep p3805 tp3806 a(g202 V`` p3807 tp3808 a(g165 V. tp3809 a(g165 V\u000a tp3810 a(g165 V\u000a tp3811 a(g228 V:: p3812 tp3813 a(g165 V\u000a\u000a p3814 tp3815 a(g202 V p3816 tp3817 a(g202 Vitertools.islice(range(10), 8) => p3818 tp3819 a(g165 V\u000a tp3820 a(g202 V 0, 1, 2, 3, 4, 5, 6, 7\u000a itertools.islice(range(10), 2, 8) =>\u000a 2, 3, 4, 5, 6, 7\u000a itertools.islice(range(10), 2, 8, 2) =>\u000a 2, 4, 6\u000a\u000a p3821 tp3822 a(g202 V`` p3823 tp3824 a(g202 Vitertools.tee(iter, [n]) p3825 tp3826 a(g202 V`` p3827 tp3828 a(g165 V replicates an iterator; it returns p3829 tp3830 a(g202 V`` p3831 tp3832 a(g202 Vn tp3833 a(g202 V`` p3834 tp3835 a(g165 V\u000a tp3836 a(g165 Vindependent iterators that will all return the contents of the source p3837 tp3838 a(g165 V\u000a tp3839 a(g165 Viterator. If you don't supply a value for p3840 tp3841 a(g202 V`` p3842 tp3843 a(g202 Vn tp3844 a(g202 V`` p3845 tp3846 a(g165 V, the default is 2. p3847 tp3848 a(g165 V\u000a tp3849 a(g165 VReplicating iterators requires saving some of the contents of the source p3850 tp3851 a(g165 V\u000a tp3852 a(g165 Viterator, so this can consume significant memory if the iterator is large p3853 tp3854 a(g165 V\u000a tp3855 a(g165 Vand one of the new iterators is consumed more than the others. p3856 tp3857 a(g165 V\u000a tp3858 a(g165 V\u000a tp3859 a(g228 V:: p3860 tp3861 a(g165 V\u000a\u000a p3862 tp3863 a(g202 V p3864 tp3865 a(g202 Vitertools.tee( itertools.count() ) => p3866 tp3867 a(g165 V\u000a tp3868 a(g202 V iterA, iterB\u000a\u000a where iterA ->\u000a 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...\u000a\u000a and iterB ->\u000a 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...\u000a\u000a\u000a p3869 tp3870 a(g7 VCalling functions on elements p3871 tp3872 a(g165 V\u000a tp3873 a(g7 V''''''''''''''''''''''''''''' p3874 tp3875 a(g165 V\u000a tp3876 a(g165 V\u000a tp3877 a(g165 VTwo functions are used for calling other functions on the contents of an p3878 tp3879 a(g165 V\u000a tp3880 a(g165 Viterable. p3881 tp3882 a(g165 V\u000a tp3883 a(g165 V\u000a tp3884 a(g202 V`` p3885 tp3886 a(g202 Vitertools.imap(f, iterA, iterB, ...) p3887 tp3888 a(g202 V`` p3889 tp3890 a(g165 V returns p3891 tp3892 a(g165 V\u000a tp3893 a(g165 Va stream containing p3894 tp3895 a(g202 V`` p3896 tp3897 a(g202 Vf(iterA[0], iterB[0]), f(iterA[1], iterB[1]),\u000af(iterA[2], iterB[2]), ... p3898 tp3899 a(g202 V`` p3900 tp3901 a(g228 V:: p3902 tp3903 a(g165 V\u000a\u000a p3904 tp3905 a(g202 V p3906 tp3907 a(g202 Vitertools.imap(operator.add, [5, 6, 5], [1, 2, 3]) => p3908 tp3909 a(g165 V\u000a tp3910 a(g202 V 6, 8, 8\u000a\u000a p3911 tp3912 a(g165 VThe p3913 tp3914 a(g202 V`` p3915 tp3916 a(g202 Voperator p3917 tp3918 a(g202 V`` p3919 tp3920 a(g165 V module contains a set of functions p3921 tp3922 a(g165 V\u000a tp3923 a(g165 Vcorresponding to Python's operators. Some examples are p3924 tp3925 a(g165 V\u000a tp3926 a(g202 V`` p3927 tp3928 a(g202 Voperator.add(a, b) p3929 tp3930 a(g202 V`` p3931 tp3932 a(g165 V (adds two values), p3933 tp3934 a(g165 V\u000a tp3935 a(g202 V`` p3936 tp3937 a(g202 Voperator.ne(a, b) p3938 tp3939 a(g202 V`` p3940 tp3941 a(g165 V (same as p3942 tp3943 a(g202 V`` p3944 tp3945 a(g202 Va!=b p3946 tp3947 a(g202 V`` p3948 tp3949 a(g165 V), p3950 tp3951 a(g165 V\u000a tp3952 a(g165 Vand p3953 tp3954 a(g165 V\u000a tp3955 a(g202 V`` p3956 tp3957 a(g202 Voperator.attrgetter('id') p3958 tp3959 a(g202 V`` p3960 tp3961 a(g165 V (returns a callable that p3962 tp3963 a(g165 V\u000a tp3964 a(g165 Vfetches the p3965 tp3966 a(g202 V`` p3967 tp3968 a(g202 V"id" p3969 tp3970 a(g202 V`` p3971 tp3972 a(g165 V attribute). p3973 tp3974 a(g165 V\u000a tp3975 a(g165 V\u000a tp3976 a(g202 V`` p3977 tp3978 a(g202 Vitertools.starmap(func, iter) p3979 tp3980 a(g202 V`` p3981 tp3982 a(g165 V assumes that the iterable will p3983 tp3984 a(g165 V\u000a tp3985 a(g165 Vreturn a stream of tuples, and calls p3986 tp3987 a(g202 V`` p3988 tp3989 a(g202 Vf() p3990 tp3991 a(g202 V`` p3992 tp3993 a(g165 V using these tuples as the p3994 tp3995 a(g165 V\u000a tp3996 a(g165 Varguments p3997 tp3998 a(g228 V:: p3999 tp4000 a(g165 V\u000a\u000a p4001 tp4002 a(g202 V p4003 tp4004 a(g202 Vitertools.starmap(os.path.join, p4005 tp4006 a(g165 V\u000a tp4007 a(g202 V [('/usr', 'bin', 'java'), ('/bin', 'python'),\u000a ('/usr', 'bin', 'perl'),('/usr', 'bin', 'ruby')])\u000a =>\u000a /usr/bin/java, /bin/python, /usr/bin/perl, /usr/bin/ruby\u000a\u000a\u000a p4008 tp4009 a(g7 VSelecting elements p4010 tp4011 a(g165 V\u000a tp4012 a(g7 V'''''''''''''''''' p4013 tp4014 a(g165 V\u000a tp4015 a(g165 V\u000a tp4016 a(g165 VAnother group of functions chooses a subset of an iterator's elements p4017 tp4018 a(g165 V\u000a tp4019 a(g165 Vbased on a predicate. p4020 tp4021 a(g165 V\u000a tp4022 a(g165 V\u000a tp4023 a(g202 V`` p4024 tp4025 a(g202 Vitertools.ifilter(predicate, iter) p4026 tp4027 a(g202 V`` p4028 tp4029 a(g165 V returns all the elements for p4030 tp4031 a(g165 V\u000a tp4032 a(g165 Vwhich the predicate returns true p4033 tp4034 a(g228 V:: p4035 tp4036 a(g165 V\u000a\u000a p4037 tp4038 a(g202 V p4039 tp4040 a(g202 Vdef is_even(x): p4041 tp4042 a(g165 V\u000a tp4043 a(g202 V return (x % 2) == 0\u000a\u000a itertools.ifilter(is_even, itertools.count()) =>\u000a 0, 2, 4, 6, 8, 10, 12, 14, ...\u000a\u000a p4044 tp4045 a(g202 V`` p4046 tp4047 a(g202 Vitertools.ifilterfalse(predicate, iter) p4048 tp4049 a(g202 V`` p4050 tp4051 a(g165 V is the opposite, p4052 tp4053 a(g165 V\u000a tp4054 a(g165 Vreturning all elements for which the predicate returns false p4055 tp4056 a(g228 V:: p4057 tp4058 a(g165 V\u000a\u000a p4059 tp4060 a(g202 V p4061 tp4062 a(g202 Vitertools.ifilterfalse(is_even, itertools.count()) => p4063 tp4064 a(g165 V\u000a tp4065 a(g202 V 1, 3, 5, 7, 9, 11, 13, 15, ...\u000a\u000a p4066 tp4067 a(g202 V`` p4068 tp4069 a(g202 Vitertools.takewhile(predicate, iter) p4070 tp4071 a(g202 V`` p4072 tp4073 a(g165 V returns elements for as long p4074 tp4075 a(g165 V\u000a tp4076 a(g165 Vas the predicate returns true. Once the predicate returns false, p4077 tp4078 a(g165 V\u000a tp4079 a(g165 Vthe iterator will signal the end of its results. p4080 tp4081 a(g165 V\u000a tp4082 a(g165 V\u000a tp4083 a(g228 V:: p4084 tp4085 a(g165 V\u000a\u000a p4086 tp4087 a(g202 V p4088 tp4089 a(g202 Vdef less_than_10(x): p4090 tp4091 a(g165 V\u000a tp4092 a(g202 V return (x < 10)\u000a\u000a itertools.takewhile(less_than_10, itertools.count()) =>\u000a 0, 1, 2, 3, 4, 5, 6, 7, 8, 9\u000a\u000a itertools.takewhile(is_even, itertools.count()) =>\u000a 0\u000a\u000a p4093 tp4094 a(g202 V`` p4095 tp4096 a(g202 Vitertools.dropwhile(predicate, iter) p4097 tp4098 a(g202 V`` p4099 tp4100 a(g165 V discards elements while the p4101 tp4102 a(g165 V\u000a tp4103 a(g165 Vpredicate returns true, and then returns the rest of the iterable's p4104 tp4105 a(g165 V\u000a tp4106 a(g165 Vresults. p4107 tp4108 a(g165 V\u000a tp4109 a(g165 V\u000a tp4110 a(g228 V:: p4111 tp4112 a(g165 V\u000a\u000a p4113 tp4114 a(g202 V p4115 tp4116 a(g202 Vitertools.dropwhile(less_than_10, itertools.count()) => p4117 tp4118 a(g165 V\u000a tp4119 a(g202 V 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ...\u000a\u000a itertools.dropwhile(is_even, itertools.count()) =>\u000a 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...\u000a\u000a\u000a p4120 tp4121 a(g7 VGrouping elements p4122 tp4123 a(g165 V\u000a tp4124 a(g7 V''''''''''''''''' p4125 tp4126 a(g165 V\u000a tp4127 a(g165 V\u000a tp4128 a(g165 VThe last function I'll discuss, p4129 tp4130 a(g202 V`` p4131 tp4132 a(g202 Vitertools.groupby(iter,\u000akey_func=None) p4133 tp4134 a(g202 V`` p4135 tp4136 a(g165 V, is the most complicated. p4137 tp4138 a(g202 V`` p4139 tp4140 a(g202 Vkey_func(elem) p4141 tp4142 a(g202 V`` p4143 tp4144 a(g165 V is a p4145 tp4146 a(g165 V\u000a tp4147 a(g165 Vfunction that can compute a key value for each element returned by the p4148 tp4149 a(g165 V\u000a tp4150 a(g165 Viterable. If you don't supply a key function, the key is simply each p4151 tp4152 a(g165 V\u000a tp4153 a(g165 Velement itself. p4154 tp4155 a(g165 V\u000a tp4156 a(g165 V\u000a tp4157 a(g202 V`` p4158 tp4159 a(g202 Vgroupby() p4160 tp4161 a(g202 V`` p4162 tp4163 a(g165 V collects all the consecutive elements from the p4164 tp4165 a(g165 V\u000a tp4166 a(g165 Vunderlying iterable that have the same key value, and returns a stream p4167 tp4168 a(g165 V\u000a tp4169 a(g165 Vof 2-tuples containing a key value and an iterator for the elements p4170 tp4171 a(g165 V\u000a tp4172 a(g165 Vwith that key. p4173 tp4174 a(g165 V\u000a tp4175 a(g165 V\u000a tp4176 a(g228 V:: p4177 tp4178 a(g165 V\u000a\u000a p4179 tp4180 a(g202 V p4181 tp4182 a(g202 Vcity_list = [('Decatur', 'AL'), ('Huntsville', 'AL'), ('Selma', 'AL'), p4183 tp4184 a(g165 V\u000a tp4185 a(g202 V ('Anchorage', 'AK'), ('Nome', 'AK'),\u000a ('Flagstaff', 'AZ'), ('Phoenix', 'AZ'), ('Tucson', 'AZ'), \u000a ...\u000a ]\u000a\u000a def get_state ((city, state)):\u000a return state\u000a\u000a itertools.groupby(city_list, get_state) =>\u000a ('AL', iterator-1),\u000a ('AK', iterator-2),\u000a ('AZ', iterator-3), ...\u000a\u000a where\u000a iterator-1 =>\u000a ('Decatur', 'AL'), ('Huntsville', 'AL'), ('Selma', 'AL')\u000a iterator-2 => \u000a ('Anchorage', 'AK'), ('Nome', 'AK')\u000a iterator-3 =>\u000a ('Flagstaff', 'AZ'), ('Phoenix', 'AZ'), ('Tucson', 'AZ')\u000a\u000a p4186 tp4187 a(g202 V`` p4188 tp4189 a(g202 Vgroupby() p4190 tp4191 a(g202 V`` p4192 tp4193 a(g165 V assumes that the underlying iterable's contents will p4194 tp4195 a(g165 V\u000a tp4196 a(g165 Valready be sorted based on the key. Note that the returned iterators p4197 tp4198 a(g165 V\u000a tp4199 a(g165 Valso use the underlying iterable, so you have to consume the results p4200 tp4201 a(g165 V\u000a tp4202 a(g165 Vof iterator-1 before requesting iterator-2 and its corresponding key. p4203 tp4204 a(g165 V\u000a tp4205 a(g165 V\u000a tp4206 a(g165 V\u000a tp4207 a(g7 VThe functools module p4208 tp4209 a(g165 V\u000a tp4210 a(g7 V---------------------------------------------- p4211 tp4212 a(g165 V\u000a tp4213 a(g165 V\u000a tp4214 a(g165 VThe p4215 tp4216 a(g202 V`` p4217 tp4218 a(g202 Vfunctools p4219 tp4220 a(g202 V`` p4221 tp4222 a(g165 V module in Python 2.5 contains some higher-order p4223 tp4224 a(g165 V\u000a tp4225 a(g165 Vfunctions. A p4226 tp4227 a(g359 V**higher-order function** p4228 tp4229 a(g165 V takes one or more functions as p4230 tp4231 a(g165 V\u000a tp4232 a(g165 Vinput and returns a new function. The most useful tool in this module p4233 tp4234 a(g165 V\u000a tp4235 a(g165 Vis the p4236 tp4237 a(g202 V`` p4238 tp4239 a(g202 Vpartial() p4240 tp4241 a(g202 V`` p4242 tp4243 a(g165 V function. p4244 tp4245 a(g165 V\u000a tp4246 a(g165 V\u000a tp4247 a(g165 VFor programs written in a functional style, you'll sometimes want to p4248 tp4249 a(g165 V\u000a tp4250 a(g165 Vconstruct variants of existing functions that have some of the p4251 tp4252 a(g165 V\u000a tp4253 a(g165 Vparameters filled in. Consider a Python function p4254 tp4255 a(g202 V`` p4256 tp4257 a(g202 Vf(a, b, c) p4258 tp4259 a(g202 V`` p4260 tp4261 a(g165 V; you p4262 tp4263 a(g165 V\u000a tp4264 a(g165 Vmay wish to create a new function p4265 tp4266 a(g202 V`` p4267 tp4268 a(g202 Vg(b, c) p4269 tp4270 a(g202 V`` p4271 tp4272 a(g165 V that's equivalent to p4273 tp4274 a(g165 V\u000a tp4275 a(g202 V`` p4276 tp4277 a(g202 Vf(1, b, c) p4278 tp4279 a(g202 V`` p4280 tp4281 a(g165 V; you're filling in a value for one of p4282 tp4283 a(g202 V`` p4284 tp4285 a(g202 Vf() p4286 tp4287 a(g202 V`` p4288 tp4289 a(g165 V's parameters. p4290 tp4291 a(g165 V\u000a tp4292 a(g165 VThis is called "partial function application". p4293 tp4294 a(g165 V\u000a tp4295 a(g165 V\u000a tp4296 a(g165 VThe constructor for p4297 tp4298 a(g202 V`` p4299 tp4300 a(g202 Vpartial p4301 tp4302 a(g202 V`` p4303 tp4304 a(g165 V takes the arguments p4305 tp4306 a(g202 V`` p4307 tp4308 a(g202 V(function, arg1,\u000aarg2, ... kwarg1=value1, kwarg2=value2) p4309 tp4310 a(g202 V`` p4311 tp4312 a(g165 V. The resulting object is p4313 tp4314 a(g165 V\u000a tp4315 a(g165 Vcallable, so you can just call it to invoke p4316 tp4317 a(g202 V`` p4318 tp4319 a(g202 Vfunction p4320 tp4321 a(g202 V`` p4322 tp4323 a(g165 V with the p4324 tp4325 a(g165 V\u000a tp4326 a(g165 Vfilled-in arguments. p4327 tp4328 a(g165 V\u000a tp4329 a(g165 V\u000a tp4330 a(g165 VHere's a small but realistic example p4331 tp4332 a(g228 V:: p4333 tp4334 a(g165 V\u000a\u000a p4335 tp4336 a(g202 V p4337 tp4338 a(g202 Vimport functools p4339 tp4340 a(g165 V\u000a tp4341 a(g202 V\u000a def log (message, subsystem):\u000a "Write the contents of 'message' to the specified subsystem."\u000a print '%s: %s' % (subsystem, message)\u000a ...\u000a\u000a server_log = functools.partial(log, subsystem='server')\u000a server_log('Unable to open socket')\u000a\u000a\u000a p4342 tp4343 a(g7 VThe operator module p4344 tp4345 a(g165 V\u000a tp4346 a(g7 V------------------- p4347 tp4348 a(g165 V\u000a tp4349 a(g165 V\u000a tp4350 a(g165 VThe p4351 tp4352 a(g202 V`` p4353 tp4354 a(g202 Voperator p4355 tp4356 a(g202 V`` p4357 tp4358 a(g165 V module was mentioned earlier. It contains a set of p4359 tp4360 a(g165 V\u000a tp4361 a(g165 Vfunctions corresponding to Python's operators. These functions p4362 tp4363 a(g165 V\u000a tp4364 a(g165 Vare often useful in functional-style code because they save you p4365 tp4366 a(g165 V\u000a tp4367 a(g165 Vfrom writing trivial functions that perform a single operation. p4368 tp4369 a(g165 V\u000a tp4370 a(g165 V\u000a tp4371 a(g165 VSome of the functions in this module are p4372 tp4373 a(g165 V: tp4374 a(g165 V\u000a tp4375 a(g165 V\u000a tp4376 a(g189 V* tp4377 a(g165 V Math operations p4378 tp4379 a(g165 V: tp4380 a(g165 V tp4381 a(g202 V`` p4382 tp4383 a(g202 Vadd() p4384 tp4385 a(g202 V`` p4386 tp4387 a(g165 V, p4388 tp4389 a(g202 V`` p4390 tp4391 a(g202 Vsub() p4392 tp4393 a(g202 V`` p4394 tp4395 a(g165 V, p4396 tp4397 a(g202 V`` p4398 tp4399 a(g202 Vmul() p4400 tp4401 a(g202 V`` p4402 tp4403 a(g165 V, p4404 tp4405 a(g202 V`` p4406 tp4407 a(g202 Vdiv() p4408 tp4409 a(g202 V`` p4410 tp4411 a(g165 V, p4412 tp4413 a(g202 V`` p4414 tp4415 a(g202 Vfloordiv() p4416 tp4417 a(g202 V`` p4418 tp4419 a(g165 V, tp4420 a(g165 V\u000a tp4421 a(g165 V p4422 tp4423 a(g202 V`` p4424 tp4425 a(g202 Vabs() p4426 tp4427 a(g202 V`` p4428 tp4429 a(g165 V, ... p4430 tp4431 a(g165 V\u000a tp4432 a(g189 V* tp4433 a(g165 V Logical operations p4434 tp4435 a(g165 V: tp4436 a(g165 V tp4437 a(g202 V`` p4438 tp4439 a(g202 Vnot_() p4440 tp4441 a(g202 V`` p4442 tp4443 a(g165 V, p4444 tp4445 a(g202 V`` p4446 tp4447 a(g202 Vtruth() p4448 tp4449 a(g202 V`` p4450 tp4451 a(g165 V. tp4452 a(g165 V\u000a tp4453 a(g189 V* tp4454 a(g165 V Bitwise operations p4455 tp4456 a(g165 V: tp4457 a(g165 V tp4458 a(g202 V`` p4459 tp4460 a(g202 Vand_() p4461 tp4462 a(g202 V`` p4463 tp4464 a(g165 V, p4465 tp4466 a(g202 V`` p4467 tp4468 a(g202 Vor_() p4469 tp4470 a(g202 V`` p4471 tp4472 a(g165 V, p4473 tp4474 a(g202 V`` p4475 tp4476 a(g202 Vinvert() p4477 tp4478 a(g202 V`` p4479 tp4480 a(g165 V. tp4481 a(g165 V\u000a tp4482 a(g189 V* tp4483 a(g165 V Comparisons p4484 tp4485 a(g165 V: tp4486 a(g165 V tp4487 a(g202 V`` p4488 tp4489 a(g202 Veq() p4490 tp4491 a(g202 V`` p4492 tp4493 a(g165 V, p4494 tp4495 a(g202 V`` p4496 tp4497 a(g202 Vne() p4498 tp4499 a(g202 V`` p4500 tp4501 a(g165 V, p4502 tp4503 a(g202 V`` p4504 tp4505 a(g202 Vlt() p4506 tp4507 a(g202 V`` p4508 tp4509 a(g165 V, p4510 tp4511 a(g202 V`` p4512 tp4513 a(g202 Vle() p4514 tp4515 a(g202 V`` p4516 tp4517 a(g165 V, p4518 tp4519 a(g202 V`` p4520 tp4521 a(g202 Vgt() p4522 tp4523 a(g202 V`` p4524 tp4525 a(g165 V, and p4526 tp4527 a(g202 V`` p4528 tp4529 a(g202 Vge() p4530 tp4531 a(g202 V`` p4532 tp4533 a(g165 V. tp4534 a(g165 V\u000a tp4535 a(g189 V* tp4536 a(g165 V Object identity p4537 tp4538 a(g165 V: tp4539 a(g165 V tp4540 a(g202 V`` p4541 tp4542 a(g202 Vis_() p4543 tp4544 a(g202 V`` p4545 tp4546 a(g165 V, p4547 tp4548 a(g202 V`` p4549 tp4550 a(g202 Vis_not() p4551 tp4552 a(g202 V`` p4553 tp4554 a(g165 V. tp4555 a(g165 V\u000a tp4556 a(g165 V\u000a tp4557 a(g165 VConsult p4558 tp4559 a(g202 V`the operator module's documentation p4560 tp4561 a(g209 V p4562 tp4563 a(g202 V`__ p4564 tp4565 a(g165 V for a complete p4566 tp4567 a(g165 V\u000a tp4568 a(g165 Vlist. p4569 tp4570 a(g165 V\u000a tp4571 a(g165 V\u000a tp4572 a(g165 V\u000a tp4573 a(g165 V\u000a tp4574 a(g7 VThe functional module p4575 tp4576 a(g165 V\u000a tp4577 a(g7 V--------------------- p4578 tp4579 a(g165 V\u000a tp4580 a(g165 V\u000a tp4581 a(g165 VCollin Winter's p4582 tp4583 a(g202 V`functional module p4584 tp4585 a(g209 V p4586 tp4587 a(g202 V`__ p4588 tp4589 a(g165 V tp4590 a(g165 V\u000a tp4591 a(g165 Vprovides a number of more p4592 tp4593 a(g165 V\u000a tp4594 a(g165 Vadvanced tools for functional programming. It also reimplements p4595 tp4596 a(g165 V\u000a tp4597 a(g165 Vseveral Python built-ins, trying to make them more intuitive to those p4598 tp4599 a(g165 V\u000a tp4600 a(g165 Vused to functional programming in other languages. p4601 tp4602 a(g165 V\u000a tp4603 a(g165 V\u000a tp4604 a(g165 VThis section contains an introduction to some of the most important p4605 tp4606 a(g165 V\u000a tp4607 a(g165 Vfunctions in p4608 tp4609 a(g202 V`` p4610 tp4611 a(g202 Vfunctional p4612 tp4613 a(g202 V`` p4614 tp4615 a(g165 V; full documentation can be found at p4616 tp4617 a(g165 V` tp4618 a(g165 Vthe p4619 tp4620 a(g165 V\u000a tp4621 a(g165 Vproject's website p4625 tp4626 a(g165 V` tp4627 a(g165 V__. p4628 tp4629 a(g165 V\u000a tp4630 a(g165 V\u000a tp4631 a(g202 V`` p4632 tp4633 a(g202 Vcompose(outer, inner, unpack=False) p4634 tp4635 a(g202 V`` p4636 tp4637 a(g165 V\u000a tp4638 a(g165 V\u000a tp4639 a(g165 VThe p4640 tp4641 a(g202 V`` p4642 tp4643 a(g202 Vcompose() p4644 tp4645 a(g202 V`` p4646 tp4647 a(g165 V function implements function composition. p4648 tp4649 a(g165 V\u000a tp4650 a(g165 VIn other words, it returns a wrapper around the p4651 tp4652 a(g202 V`` p4653 tp4654 a(g202 Vouter p4655 tp4656 a(g202 V`` p4657 tp4658 a(g165 V and p4659 tp4660 a(g202 V`` p4661 tp4662 a(g202 Vinner p4663 tp4664 a(g202 V`` p4665 tp4666 a(g165 V callables, such p4667 tp4668 a(g165 V\u000a tp4669 a(g165 Vthat the return value from p4670 tp4671 a(g202 V`` p4672 tp4673 a(g202 Vinner p4674 tp4675 a(g202 V`` p4676 tp4677 a(g165 V is fed directly to p4678 tp4679 a(g202 V`` p4680 tp4681 a(g202 Vouter p4682 tp4683 a(g202 V`` p4684 tp4685 a(g165 V. That is, p4686 tp4687 a(g165 V\u000a tp4688 a(g165 V\u000a tp4689 a(g228 V:: p4690 tp4691 a(g165 V\u000a\u000a p4692 tp4693 a(g202 V p4694 tp4695 a(g202 V>>> def add(a, b): p4696 tp4697 a(g165 V\u000a tp4698 a(g202 V ... return a + b\u000a ...\u000a >>> def double(a):\u000a ... return 2 * a\u000a ...\u000a >>> compose(double, add)(5, 6)\u000a 22\u000a\u000a p4699 tp4700 a(g165 Vis equivalent to p4701 tp4702 a(g165 V\u000a tp4703 a(g165 V\u000a tp4704 a(g228 V:: p4705 tp4706 a(g165 V\u000a\u000a p4707 tp4708 a(g202 V p4709 tp4710 a(g202 V>>> double(add(5, 6)) p4711 tp4712 a(g165 V\u000a tp4713 a(g202 V 22\u000a \u000a p4714 tp4715 a(g165 VThe p4716 tp4717 a(g202 V`` p4718 tp4719 a(g202 Vunpack p4720 tp4721 a(g202 V`` p4722 tp4723 a(g165 V keyword is provided to work around the fact that Python functions are not always p4724 tp4725 a(g165 V\u000a tp4726 a(g202 V`fully curried p4727 tp4728 a(g209 V p4729 tp4730 a(g202 V`__ p4731 tp4732 a(g165 V. tp4733 a(g165 V\u000a tp4734 a(g165 VBy default, it is expected that the p4735 tp4736 a(g202 V`` p4737 tp4738 a(g202 Vinner p4739 tp4740 a(g202 V`` p4741 tp4742 a(g165 V function will return a single object and that the p4743 tp4744 a(g202 V`` p4745 tp4746 a(g202 Vouter p4747 tp4748 a(g202 V`` p4749 tp4750 a(g165 V\u000a tp4751 a(g165 Vfunction will take a single argument. Setting the p4752 tp4753 a(g202 V`` p4754 tp4755 a(g202 Vunpack p4756 tp4757 a(g202 V`` p4758 tp4759 a(g165 V argument causes p4760 tp4761 a(g202 V`` p4762 tp4763 a(g202 Vcompose p4764 tp4765 a(g202 V`` p4766 tp4767 a(g165 V to expect a p4768 tp4769 a(g165 V\u000a tp4770 a(g165 Vtuple from p4771 tp4772 a(g202 V`` p4773 tp4774 a(g202 Vinner p4775 tp4776 a(g202 V`` p4777 tp4778 a(g165 V which will be expanded before being passed to p4779 tp4780 a(g202 V`` p4781 tp4782 a(g202 Vouter p4783 tp4784 a(g202 V`` p4785 tp4786 a(g165 V. Put simply, p4787 tp4788 a(g165 V\u000a tp4789 a(g165 V\u000a tp4790 a(g228 V:: p4791 tp4792 a(g165 V\u000a\u000a p4793 tp4794 a(g202 V p4795 tp4796 a(g202 Vcompose(f, g)(5, 6) p4797 tp4798 a(g165 V\u000a tp4799 a(g202 V \u000a p4800 tp4801 a(g165 Vis equivalent to p4802 tp4803 a(g228 V:: p4804 tp4805 a(g165 V\u000a\u000a p4806 tp4807 a(g202 V p4808 tp4809 a(g202 Vf(g(5, 6)) p4810 tp4811 a(g165 V\u000a tp4812 a(g202 V \u000a p4813 tp4814 a(g165 Vwhile p4815 tp4816 a(g165 V\u000a tp4817 a(g165 V\u000a tp4818 a(g228 V:: p4819 tp4820 a(g165 V\u000a\u000a p4821 tp4822 a(g202 V p4823 tp4824 a(g202 Vcompose(f, g, unpack=True)(5, 6) p4825 tp4826 a(g165 V\u000a tp4827 a(g202 V \u000a p4828 tp4829 a(g165 Vis equivalent to p4830 tp4831 a(g228 V:: p4832 tp4833 a(g165 V\u000a\u000a p4834 tp4835 a(g202 V p4836 tp4837 a(g202 Vf(*g(5, 6)) p4838 tp4839 a(g165 V\u000a tp4840 a(g202 V\u000a tp4841 a(g165 VEven though p4842 tp4843 a(g202 V`` p4844 tp4845 a(g202 Vcompose() p4846 tp4847 a(g202 V`` p4848 tp4849 a(g165 V only accepts two functions, it's trivial to p4850 tp4851 a(g165 V\u000a tp4852 a(g165 Vbuild up a version that will compose any number of functions. We'll p4853 tp4854 a(g165 V\u000a tp4855 a(g165 Vuse p4856 tp4857 a(g202 V`` p4858 tp4859 a(g202 Vreduce() p4860 tp4861 a(g202 V`` p4862 tp4863 a(g165 V, p4864 tp4865 a(g202 V`` p4866 tp4867 a(g202 Vcompose() p4868 tp4869 a(g202 V`` p4870 tp4871 a(g165 V and p4872 tp4873 a(g202 V`` p4874 tp4875 a(g202 Vpartial() p4876 tp4877 a(g202 V`` p4878 tp4879 a(g165 V (the last of which p4880 tp4881 a(g165 V\u000a tp4882 a(g165 Vis provided by both p4883 tp4884 a(g202 V`` p4885 tp4886 a(g202 Vfunctional p4887 tp4888 a(g202 V`` p4889 tp4890 a(g165 V and p4891 tp4892 a(g202 V`` p4893 tp4894 a(g202 Vfunctools p4895 tp4896 a(g202 V`` p4897 tp4898 a(g165 V). p4899 tp4900 a(g165 V\u000a tp4901 a(g165 V\u000a tp4902 a(g228 V:: p4903 tp4904 a(g165 V\u000a\u000a p4905 tp4906 a(g202 V p4907 tp4908 a(g202 Vfrom functional import compose, partial p4909 tp4910 a(g165 V\u000a tp4911 a(g202 V \u000a multi_compose = partial(reduce, compose)\u000a \u000a p4912 tp4913 a(g165 V p4914 tp4915 a(g165 V\u000a tp4916 a(g165 VWe can also use p4917 tp4918 a(g202 V`` p4919 tp4920 a(g202 Vmap() p4921 tp4922 a(g202 V`` p4923 tp4924 a(g165 V, p4925 tp4926 a(g202 V`` p4927 tp4928 a(g202 Vcompose() p4929 tp4930 a(g202 V`` p4931 tp4932 a(g165 V and p4933 tp4934 a(g202 V`` p4935 tp4936 a(g202 Vpartial() p4937 tp4938 a(g202 V`` p4939 tp4940 a(g165 V to craft a p4941 tp4942 a(g165 V\u000a tp4943 a(g165 Vversion of p4944 tp4945 a(g202 V`` p4946 tp4947 a(g202 V"".join(...) p4948 tp4949 a(g202 V`` p4950 tp4951 a(g165 V that converts its arguments to string p4952 tp4953 a(g228 V:: p4954 tp4955 a(g165 V\u000a\u000a p4956 tp4957 a(g202 V p4958 tp4959 a(g202 Vfrom functional import compose, partial p4960 tp4961 a(g165 V\u000a tp4962 a(g202 V \u000a join = compose("".join, partial(map, str))\u000a\u000a\u000a p4963 tp4964 a(g202 V`` p4965 tp4966 a(g202 Vflip(func) p4967 tp4968 a(g202 V`` p4969 tp4970 a(g165 V\u000a tp4971 a(g165 V p4972 tp4973 a(g165 V\u000a tp4974 a(g202 V`` p4975 tp4976 a(g202 Vflip() p4977 tp4978 a(g202 V`` p4979 tp4980 a(g165 V wraps the callable in p4981 tp4982 a(g202 V`` p4983 tp4984 a(g202 Vfunc p4985 tp4986 a(g202 V`` p4987 tp4988 a(g165 V and p4989 tp4990 a(g165 V\u000a tp4991 a(g165 Vcauses it to receive its non-keyword arguments in reverse order. p4992 tp4993 a(g165 V\u000a tp4994 a(g165 V\u000a tp4995 a(g228 V:: p4996 tp4997 a(g165 V\u000a\u000a p4998 tp4999 a(g202 V p5000 tp5001 a(g202 V>>> def triple(a, b, c): p5002 tp5003 a(g165 V\u000a tp5004 a(g202 V ... return (a, b, c)\u000a ...\u000a >>> triple(5, 6, 7)\u000a (5, 6, 7)\u000a >>>\u000a >>> flipped_triple = flip(triple)\u000a >>> flipped_triple(5, 6, 7)\u000a (7, 6, 5)\u000a\u000a p5005 tp5006 a(g202 V`` p5007 tp5008 a(g202 Vfoldl(func, start, iterable) p5009 tp5010 a(g202 V`` p5011 tp5012 a(g165 V\u000a tp5013 a(g165 V p5014 tp5015 a(g165 V\u000a tp5016 a(g202 V`` p5017 tp5018 a(g202 Vfoldl() p5019 tp5020 a(g202 V`` p5021 tp5022 a(g165 V takes a binary function, a starting value (usually some kind of 'zero'), and an iterable. p5023 tp5024 a(g165 V\u000a tp5025 a(g165 VThe function is applied to the starting value and the first element of the list, then the result of p5026 tp5027 a(g165 V\u000a tp5028 a(g165 Vthat and the second element of the list, then the result of that and the third element of the list, p5029 tp5030 a(g165 V\u000a tp5031 a(g165 Vand so on. p5032 tp5033 a(g165 V\u000a tp5034 a(g165 V\u000a tp5035 a(g165 VThis means that a call such as p5036 tp5037 a(g228 V:: p5038 tp5039 a(g165 V\u000a\u000a p5040 tp5041 a(g202 V p5042 tp5043 a(g202 Vfoldl(f, 0, [1, 2, 3]) p5044 tp5045 a(g165 V\u000a tp5046 a(g202 V\u000a tp5047 a(g165 Vis equivalent to p5048 tp5049 a(g228 V:: p5050 tp5051 a(g165 V\u000a\u000a p5052 tp5053 a(g202 V p5054 tp5055 a(g202 Vf(f(f(0, 1), 2), 3) p5056 tp5057 a(g165 V\u000a tp5058 a(g202 V\u000a tp5059 a(g165 V p5060 tp5061 a(g165 V\u000a tp5062 a(g202 V`` p5063 tp5064 a(g202 Vfoldl() p5065 tp5066 a(g202 V`` p5067 tp5068 a(g165 V is roughly equivalent to the following recursive function p5069 tp5070 a(g228 V:: p5071 tp5072 a(g165 V\u000a\u000a p5073 tp5074 a(g202 V p5075 tp5076 a(g202 Vdef foldl(func, start, seq): p5077 tp5078 a(g165 V\u000a tp5079 a(g202 V if len(seq) == 0:\u000a return start\u000a\u000a return foldl(func, func(start, seq[0]), seq[1:])\u000a\u000a p5080 tp5081 a(g165 VSpeaking of equivalence, the above p5082 tp5083 a(g202 V`` p5084 tp5085 a(g202 Vfoldl p5086 tp5087 a(g202 V`` p5088 tp5089 a(g165 V call can be expressed in terms of the built-in p5090 tp5091 a(g202 V`` p5092 tp5093 a(g202 Vreduce p5094 tp5095 a(g202 V`` p5096 tp5097 a(g165 V like p5098 tp5099 a(g165 V\u000a tp5100 a(g165 Vso p5101 tp5102 a(g228 V:: p5103 tp5104 a(g165 V\u000a\u000a p5105 tp5106 a(g202 V p5107 tp5108 a(g202 Vreduce(f, [1, 2, 3], 0) p5109 tp5110 a(g165 V\u000a tp5111 a(g202 V\u000a\u000a p5112 tp5113 a(g165 VWe can use p5114 tp5115 a(g202 V`` p5116 tp5117 a(g202 Vfoldl() p5118 tp5119 a(g202 V`` p5120 tp5121 a(g165 V, p5122 tp5123 a(g202 V`` p5124 tp5125 a(g202 Voperator.concat() p5126 tp5127 a(g202 V`` p5128 tp5129 a(g165 V and p5130 tp5131 a(g202 V`` p5132 tp5133 a(g202 Vpartial() p5134 tp5135 a(g202 V`` p5136 tp5137 a(g165 V to p5138 tp5139 a(g165 V\u000a tp5140 a(g165 Vwrite a cleaner, more aesthetically-pleasing version of Python's p5141 tp5142 a(g165 V\u000a tp5143 a(g202 V`` p5144 tp5145 a(g202 V"".join(...) p5146 tp5147 a(g202 V`` p5148 tp5149 a(g165 V idiom p5150 tp5151 a(g228 V:: p5152 tp5153 a(g165 V\u000a\u000a p5154 tp5155 a(g202 V p5156 tp5157 a(g202 Vfrom functional import foldl, partial p5158 tp5159 a(g165 V\u000a tp5160 a(g202 V from operator import concat\u000a \u000a join = partial(foldl, concat, "")\u000a\u000a\u000a p5161 tp5162 a(g7 VRevision History and Acknowledgements p5163 tp5164 a(g165 V\u000a tp5165 a(g7 V------------------------------------------------ p5166 tp5167 a(g165 V\u000a tp5168 a(g165 V\u000a tp5169 a(g165 VThe author would like to thank the following people for offering p5170 tp5171 a(g165 V\u000a tp5172 a(g165 Vsuggestions, corrections and assistance with various drafts of this p5173 tp5174 a(g165 V\u000a tp5175 a(g165 Varticle p5176 tp5177 a(g165 V: tp5178 a(g165 V Ian Bicking, Nick Coghlan, Nick Efford, Raymond Hettinger, p5179 tp5180 a(g165 V\u000a tp5181 a(g165 VJim Jewett, Mike Krell, Leandro Lameiro, Jussi Salmela, p5182 tp5183 a(g165 V\u000a tp5184 a(g165 VCollin Winter, Blake Winton. p5185 tp5186 a(g165 V\u000a tp5187 a(g165 V\u000a tp5188 a(g165 VVersion 0.1 p5189 tp5190 a(g165 V: tp5191 a(g165 V posted June 30 2006. p5192 tp5193 a(g165 V\u000a tp5194 a(g165 V\u000a tp5195 a(g165 VVersion 0.11 p5196 tp5197 a(g165 V: tp5198 a(g165 V posted July 1 2006. Typo fixes. p5199 tp5200 a(g165 V\u000a tp5201 a(g165 V\u000a tp5202 a(g165 VVersion 0.2 p5203 tp5204 a(g165 V: tp5205 a(g165 V posted July 10 2006. Merged genexp and listcomp p5206 tp5207 a(g165 V\u000a tp5208 a(g165 Vsections into one. Typo fixes. p5209 tp5210 a(g165 V\u000a tp5211 a(g165 V\u000a tp5212 a(g165 VVersion 0.21 p5213 tp5214 a(g165 V: tp5215 a(g165 V Added more references suggested on the tutor mailing list. p5216 tp5217 a(g165 V\u000a tp5218 a(g165 V\u000a tp5219 a(g165 VVersion 0.30 p5220 tp5221 a(g165 V: tp5222 a(g165 V Adds a section on the p5223 tp5224 a(g202 V`` p5225 tp5226 a(g202 Vfunctional p5227 tp5228 a(g202 V`` p5229 tp5230 a(g165 V module written by p5231 tp5232 a(g165 V\u000a tp5233 a(g165 VCollin Winter; adds short section on the operator module; a few other p5234 tp5235 a(g165 V\u000a tp5236 a(g165 Vedits. p5237 tp5238 a(g165 V\u000a tp5239 a(g165 V\u000a tp5240 a(g165 V\u000a tp5241 a(g7 VReferences p5242 tp5243 a(g165 V\u000a tp5244 a(g7 V-------------------- p5245 tp5246 a(g165 V\u000a tp5247 a(g165 V\u000a tp5248 a(g7 VGeneral p5249 tp5250 a(g165 V\u000a tp5251 a(g7 V''''''''''''''' p5252 tp5253 a(g165 V\u000a tp5254 a(g165 V\u000a tp5255 a(g359 V**Structure and Interpretation of Computer Programs** p5256 tp5257 a(g165 V, by p5258 tp5259 a(g165 V\u000a tp5260 a(g165 VHarold Abelson and Gerald Jay Sussman with Julie Sussman. p5261 tp5262 a(g165 V\u000a tp5263 a(g165 VFull text at http p5264 tp5265 a(g165 V: tp5266 a(g165 V//mitpress.mit.edu/sicp/. p5267 tp5268 a(g165 V\u000a tp5269 a(g165 VIn this classic textbook of computer science, chapters 2 and 3 discuss the p5270 tp5271 a(g165 V\u000a tp5272 a(g165 Vuse of sequences and streams to organize the data flow inside a p5273 tp5274 a(g165 V\u000a tp5275 a(g165 Vprogram. The book uses Scheme for its examples, but many of the p5276 tp5277 a(g165 V\u000a tp5278 a(g165 Vdesign approaches described in these chapters are applicable to p5279 tp5280 a(g165 V\u000a tp5281 a(g165 Vfunctional-style Python code. p5282 tp5283 a(g165 V\u000a tp5284 a(g165 V\u000a tp5285 a(g165 Vhttp p5286 tp5287 a(g165 V: tp5288 a(g165 V//www.defmacro.org/ramblings/fp.html p5289 tp5290 a(g165 V: tp5291 a(g165 V A general p5292 tp5293 a(g165 V\u000a tp5294 a(g165 Vintroduction to functional programming that uses Java examples p5295 tp5296 a(g165 V\u000a tp5297 a(g165 Vand has a lengthy historical introduction. p5298 tp5299 a(g165 V\u000a tp5300 a(g165 V\u000a tp5301 a(g165 Vhttp p5302 tp5303 a(g165 V: tp5304 a(g165 V//en.wikipedia.org/wiki/Functional_programming p5305 tp5306 a(g165 V: tp5307 a(g165 V\u000a tp5308 a(g165 VGeneral Wikipedia entry describing functional programming. p5309 tp5310 a(g165 V\u000a tp5311 a(g165 V\u000a tp5312 a(g165 Vhttp p5313 tp5314 a(g165 V: tp5315 a(g165 V//en.wikipedia.org/wiki/Coroutine p5316 tp5317 a(g165 V: tp5318 a(g165 V\u000a tp5319 a(g165 VEntry for coroutines. p5320 tp5321 a(g165 V\u000a tp5322 a(g165 V\u000a tp5323 a(g165 Vhttp p5324 tp5325 a(g165 V: tp5326 a(g165 V//en.wikipedia.org/wiki/Currying p5327 tp5328 a(g165 V: tp5329 a(g165 V\u000a tp5330 a(g165 VEntry for the concept of currying. p5331 tp5332 a(g165 V\u000a tp5333 a(g165 V\u000a tp5334 a(g7 VPython-specific p5335 tp5336 a(g165 V\u000a tp5337 a(g7 V''''''''''''''''''''''''''' p5338 tp5339 a(g165 V\u000a tp5340 a(g165 V\u000a tp5341 a(g165 Vhttp p5342 tp5343 a(g165 V: tp5344 a(g165 V//gnosis.cx/TPiP/ p5345 tp5346 a(g165 V: tp5347 a(g165 V\u000a tp5348 a(g165 VThe first chapter of David Mertz's book p5349 tp5350 a(g66 V:title-reference: p5351 tp5352 a(g98 V`Text Processing in Python` p5353 tp5354 a(g165 V tp5355 a(g165 V\u000a tp5356 a(g165 Vdiscusses functional programming for text processing, in the section titled p5357 tp5358 a(g165 V\u000a tp5359 a(g165 V"Utilizing Higher-Order Functions in Text Processing". p5360 tp5361 a(g165 V\u000a tp5362 a(g165 V\u000a tp5363 a(g165 VMertz also wrote a 3-part series of articles on functional programming p5364 tp5365 a(g165 V\u000a tp5366 a(g165 Vfor IBM's DeveloperWorks site; see p5367 tp5368 a(g165 V\u000a tp5369 a(g202 V`part 1 p5370 tp5371 a(g209 V p5372 tp5373 a(g202 V`__ p5374 tp5375 a(g165 V, tp5376 a(g165 V\u000a tp5377 a(g202 V`part 2 p5378 tp5379 a(g209 V p5380 tp5381 a(g202 V`__ p5382 tp5383 a(g165 V, and p5384 tp5385 a(g165 V\u000a tp5386 a(g202 V`part 3 p5387 tp5388 a(g209 V p5389 tp5390 a(g202 V`__ p5391 tp5392 a(g165 V, tp5393 a(g165 V\u000a tp5394 a(g165 V\u000a tp5395 a(g165 V\u000a tp5396 a(g7 VPython documentation p5397 tp5398 a(g165 V\u000a tp5399 a(g7 V''''''''''''''''''''''''''' p5400 tp5401 a(g165 V\u000a tp5402 a(g165 V\u000a tp5403 a(g165 Vhttp p5404 tp5405 a(g165 V: tp5406 a(g165 V//docs.python.org/lib/module-itertools.html p5407 tp5408 a(g165 V: tp5409 a(g165 V\u000a tp5410 a(g165 VDocumentation for the p5411 tp5412 a(g202 V`` p5413 tp5414 a(g202 Vitertools p5415 tp5416 a(g202 V`` p5417 tp5418 a(g165 V module. p5419 tp5420 a(g165 V\u000a tp5421 a(g165 V\u000a tp5422 a(g165 Vhttp p5423 tp5424 a(g165 V: tp5425 a(g165 V//docs.python.org/lib/module-operator.html p5426 tp5427 a(g165 V: tp5428 a(g165 V\u000a tp5429 a(g165 VDocumentation for the p5430 tp5431 a(g202 V`` p5432 tp5433 a(g202 Voperator p5434 tp5435 a(g202 V`` p5436 tp5437 a(g165 V module. p5438 tp5439 a(g165 V\u000a tp5440 a(g165 V\u000a tp5441 a(g165 Vhttp p5442 tp5443 a(g165 V: tp5444 a(g165 V//www.python.org/dev/peps/pep-0289/ p5445 tp5446 a(g165 V: tp5447 a(g165 V\u000a tp5448 a(g165 VPEP 289 p5449 tp5450 a(g165 V: tp5451 a(g165 V "Generator Expressions" p5452 tp5453 a(g165 V\u000a tp5454 a(g165 V\u000a tp5455 a(g165 Vhttp p5456 tp5457 a(g165 V: tp5458 a(g165 V//www.python.org/dev/peps/pep-0342/ p5459 tp5460 a(g165 V\u000a tp5461 a(g165 VPEP 342 p5462 tp5463 a(g165 V: tp5464 a(g165 V "Coroutines via Enhanced Generators" describes the new generator p5465 tp5466 a(g165 V\u000a tp5467 a(g165 Vfeatures in Python 2.5. p5468 tp5469 a(g165 V\u000a tp5470 a(g165 V\u000a tp5471 a(g25 V.. comment\u000a\u000a Topics to place\u000a -----------------------------\u000a\u000a XXX os.walk()\u000a\u000a XXX Need a large example.\u000a\u000a But will an example add much? I'll post a first draft and see\u000a what the comments say.\u000a\u000a p5472 tp5473 a(g25 V.. comment\u000a\u000a Original outline:\u000a Introduction\u000a Idea of FP\u000a Programs built out of functions\u000a Functions are strictly input-output, no internal state\u000a Opposed to OO programming, where objects have state\u000a\u000a Why FP?\u000a Formal provability\u000a Assignment is difficult to reason about\u000a Not very relevant to Python\u000a Modularity\u000a Small functions that do one thing\u000a Debuggability:\u000a Easy to test due to lack of state\u000a Easy to verify output from intermediate steps\u000a Composability\u000a You assemble a toolbox of functions that can be mixed\u000a\u000a Tackling a problem\u000a Need a significant example\u000a\u000a Iterators\u000a Generators\u000a The itertools module\u000a List comprehensions\u000a Small functions and the lambda statement\u000a Built-in functions\u000a map\u000a filter\u000a reduce\u000a\u000a p5474 tp5475 a(g25 V.. comment\u000a\u000a Handy little function for printing part of an iterator -- used\u000a while writing this document.\u000a\u000a import itertools\u000a def print_iter(it):\u000a slice = itertools.islice(it, 10)\u000a for elem in slice[:-1]:\u000a sys.stdout.write(str(elem))\u000a sys.stdout.write(', ')\u000a print elem[-1]\u000a p5476 tp5477 a.