Python Setup and Usage, Release 3.9.5 47 any classes you define with an$_{__iter__()}$ method or with a$_{__getitem__()}$ method that implements Sequence semantics. Iterables can be used in a$_{for}$ loop and in many other places where a sequence is needed ($_{zip()}$ ,$_{map()}$ , …). When an iterable object is passed as an argument to the built-in function iter() , it returns an iterator for the object. This iterator is good for one pass over the set of values. When using iterables, it is usually not necessary to call$_{iter()}$ or deal with iterator objects yourself. The$_{for}$ statement does that automatically for you, creating a temporary unnamed variable to hold the iterator for the duration of the loop. See also iterator , sequence , and generator . There are several ways to create a key function. For example. the$_{str.lower()}$ method can serve as a key function for case insensitive sorts. Alternatively, a key function can be built from a$_{lambda}$expression such as$_{lambda r: (r[0], r[2])}$ . Also, the$_{operator}$ module provides three key function constructors: attrgetter() ,$_{itemgetter()}$ , and$_{methodcaller()}$ . See the Sorting HOW TO for examples of how to create and use key functions. A number of tools in Python accept key functions to control how elements are ordered or grouped. They include min() , max() , sorted() , list.sort() , heapq.merge() , heapq.nsmallest() , heapq. nlargest() , and$_{itertools.groupby()}$ . In a multi-threaded environment, the LBYL approach can risk introducing a race condition between “the looking” and “the leaping”. For example, the code,$_{if key in mapping: return mapping[key]}$ can fail if another thread removes key from mapping after the test, but before the lookup. This issue can be solved with locks or by using the EAFP approach. mapping A container object that supports arbitrary key lookups and implements the methods specified in the Mapping or $_{MutableMapping}$abstract base classes. Examples include $_{dict}$, collections. defaultdict ,$_{collections.OrderedDict}$ and$_{collections.Counter}$ . loader An object that loads a module. It must define a method named$_{load_module()}$ . A loader is typically returned by a finder . See PEP 302 for details and$_{importlib.abc.Loader}$ for an abstract base class . magic method An informal synonym for special method . list comprehension A compact way to process all or part of the elements in a sequence and return a list with the re- sults. gen- erates a list of strings containing even hex numbers (0x..) in the range from 0 to 255. The$_{if}$ clause is optional. If omitted, all elements in$_{range(256)}$ are processed. list A built-in Python sequence . Despite its name it is more akin to an array in other languages than to a linked list since access to elements is O(1). LBYL Look before you leap. This coding style explicitly tests for pre-conditions before making calls or lookups. This style contrasts with the EAFP approach and is characterized by the presence of many$_{if}$ statements. lambda An anonymous inline function consisting of a single expression which is evaluated when the function is called. The syntax to create a lambda function is$_{lambda [parameters]: expression}$ keyword argument See argument . key function A key function or collation function is a callable that returns a value used for sorting or ordering. For example,$_{locale.strxfrm()}$ is used to produce a sort key that is aware of locale specific sort conventions. iterator An object representing a stream of data. Repeated calls to the iterator’s __next__() method (or passing it to the built-in function next() ) return successive items in the stream. When no more data are available a$_{StopIteration}$ exception is raised instead. At this point, the iterator object is exhausted and any fur- ther calls to its$_{__next__()}$ method just raise$_{StopIteration}$ again. Iterators are required to have an __iter__() method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted. One notable exception is code which attempts multiple iteration passes. A container object (such as a list ) produces a fresh new iterator each time you pass it to the iter() function or use it in a$_{for}$ loop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container. More information can be found in typeiter.