12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Comprehending List ComprehensionsWith such generality, list comprehensions can quickly become, well, incomprehensible,especially when nested. Consequently, my advice is typically to use simple forloops when getting started with <strong>Python</strong>, and map calls in most other cases (unlessthey get too complex). The “keep it simple” rule applies here, as always: code concisenessis a much less important goal than code readability.However, in this case, there is currently a substantial performance advantage to theextra complexity: based on tests run under <strong>Python</strong> today, map calls are roughly twiceas fast as equivalent for loops, and list comprehensions are usually slightly fasterthan map calls. * This speed difference is down to the fact that map and list comprehensionsrun at C language speed inside the interpreter, which is much faster thanstepping through <strong>Python</strong> for loop code within the PVM.Because for loops make logic more explicit, I recommend them in general on thegrounds of simplicity. However, map and list comprehensions are worth knowing andusing for simpler kinds of iterations, and if your application’s speed is an importantconsideration. In addition, because map and list comprehensions are both expressions,they can show up syntactically in places that for loop statements cannot, suchas in the bodies of lambda functions, within list and dictionary literals, and more.Still, you should try to keep your map calls and list comprehensions simple; for morecomplex tasks, use full statements instead.Iterators Revisited: GeneratorsIn this part of the book, we’ve learned about coding normal functions that receiveinput parameters and send back a single result immediately. It is also possible, however,to write functions that may send back a value and later be resumed, picking upwhere they left off. Such functions are known as generators because they generate asequence of values over time.Generator functions are like normal functions in most respects, but in <strong>Python</strong>, theyare automatically made to implement the iteration protocol so that they can appearin iteration contexts. We studied iterators in Chapter 13; here, we’ll revise them tosee how they relate to generators.Unlike normal functions that return a value and exit, generator functions automaticallysuspend and resume their execution and state around the point of value generation.* These performance generalizations can depend on call patterns, as well as changes and optimizations in<strong>Python</strong> itself. Recent <strong>Python</strong> releases have sped up the simple for loop statement, for example. Usually,though, list comprehensions are still substantially faster than for loops and even faster than map (though mapcan still win for built-in functions). To time these alternatives yourself, see the standard library’s timemodule’s time.clock and time.time calls, the newer timeit module added in Release 2.4, or this chapter’supcoming section “Timing Iteration Alternatives.”360 | Chapter 17: Advanced Function Topics

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!