Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
9efdd788
Commit
9efdd788
authored
Nov 08, 2006
by
Andrew M. Kuchling
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add section on operator module; make a few edits
parent
f4dcd1dc
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
8 deletions
+38
-8
Doc/howto/functional.rst
Doc/howto/functional.rst
+38
-8
No files found.
Doc/howto/functional.rst
View file @
9efdd788
...
...
@@ -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 a
n 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 <http://en.wikipedia.org/wiki/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 <http://cheeseshop.python.org/pypi/functional>`__,
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 <http://docs.python.org/lib/module-operator.html>`__ for a complete
list.
The functional module
...
...
@@ -1298,7 +1327,8 @@ 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.
Collin Winter; adds short section on the operator module; a few other
edits.
References
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment