From 9efdd7880d43f334c1fc62168ad8f1ba8c2bf00b Mon Sep 17 00:00:00 2001 From: "Andrew M. Kuchling" Date: Wed, 8 Nov 2006 14:14:30 +0000 Subject: [PATCH] Add section on operator module; make a few edits --- Doc/howto/functional.rst | 46 +++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/Doc/howto/functional.rst b/Doc/howto/functional.rst index 42aad5d45ee..a56499dc687 100644 --- a/Doc/howto/functional.rst +++ b/Doc/howto/functional.rst @@ -339,11 +339,11 @@ the set's elements:: Generator expressions and list comprehensions ---------------------------------------------------- -Two common operations on a stream are 1) performing some operation for -every element, 2) selecting a subset of elements that meet some -condition. For example, given a list of strings, you might want to -strip off trailing whitespace from each line or extract all the -strings containing a given substring. +Two common operations on an iterator's output are 1) performing some +operation for every element, 2) selecting a subset of elements that +meet some condition. For example, given a list of strings, you might +want to strip off trailing whitespace from each line or extract all +the strings containing a given substring. List comprehensions and generator expressions (short form: "listcomps" and "genexps") are a concise notation for such operations, borrowed @@ -941,6 +941,12 @@ and returns them in a tuple:: itertools.izip(['a', 'b', 'c'], (1, 2, 3)) => ('a', 1), ('b', 2), ('c', 3) +It's similiar to the built-in ``zip()`` function, but doesn't +construct an in-memory list and exhaust all the input iterators before +returning; instead tuples are constructed and returned only if they're +requested. (The technical term for this behaviour is +`lazy evaluation `__.) + This iterator is intended to be used with iterables that are all of the same length. If the iterables are of different lengths, the resulting stream will be the same length as the shortest iterable. @@ -1138,7 +1144,30 @@ Here's a small but realistic example:: There are also third-party modules, such as Collin Winter's `functional package `__, -that are intended for use in functional-style programs. +that are intended for use in functional-style programs. See below +for a section describing the ``functional`` mdoule. + + +The operator module +=================== + +The ``operator`` module was mentioned earlier. It contains a set of +functions corresponding to Python's operators. These functions +are often useful in functional-style code because they save you +from writing trivial functions that perform a single operation. + +Some of the functions in this module are: + +* Math operations: ``add()``, ``sub()``, ``mul()``, ``div()``, ``floordiv()``, + ``abs()``, ... +* Logical operations: ``not_()``, ``truth()``. +* Bitwise operations: ``and_()``, ``or_()``, ``invert()``. +* Comparisons: ``eq()``, ``ne()``, ``lt()``, ``le()``, ``gt()``, and ``ge()``. +* Object identity: ``is_()``, ``is_not()``. + +Consult `the operator module's documentation `__ for a complete +list. + The functional module @@ -1297,8 +1326,9 @@ sections into one. Typo fixes. Version 0.21: Added more references suggested on the tutor mailing list. -Version 0.30: Adds a section on the ``functional`` module written by -Collin Winter. +Version 0.30: Adds a section on the ``functional`` module written by +Collin Winter; adds short section on the operator module; a few other +edits. References