12.07.2015 Views

Is Python a

Is Python a

Is Python a

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

6. The map call is similar to a list comprehension—both build a new list by collectingthe results of applying an operation to each item in a sequence or otheriterable, one item at a time. The main difference is that map applies a functioncall to each item, and list comprehensions apply arbitrary expressions. Becauseof this, list comprehensions are more general; they can apply a function callexpression like map, but map requires a function to apply other kinds of expressions.List comprehensions also support extended syntax such as nested forloops and if clauses that subsume the filter built-in.7. Both lambda and def create function objects to be called later. Because lambda isan expression, though, it can be used to nest a function definition in placeswhere a def will not work syntactically. Using a lambda is never required—youcan always code a def instead, and reference the function by name. lambdas comein handy, though, to embed small pieces of deferred code that are unlikely to beused elsewhere in a program. Syntactically, a lambda only allows for a singlereturn value expression; because it does not support a block of statements, it isnot ideal for larger functions.378 | Chapter 17: Advanced Function Topics

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

Saved successfully!

Ooh no, something went wrong!