[Python-checkins] r47199 - sandbox/trunk/Doc/functional.rst
andrew.kuchling
python-checkins at python.org
Sun Jul 2 22:02:25 CEST 2006
Author: andrew.kuchling
Date: Sun Jul 2 22:02:24 2006
New Revision: 47199
Modified:
sandbox/trunk/Doc/functional.rst
Log:
Correct explanation of 'if' clauses in listcomps; untabify
Modified: sandbox/trunk/Doc/functional.rst
==============================================================================
--- sandbox/trunk/Doc/functional.rst (original)
+++ sandbox/trunk/Doc/functional.rst Sun Jul 2 22:02:24 2006
@@ -1,7 +1,7 @@
Functional Programming HOWTO
================================
-**Version 0.11**
+**Version 0.12**
(This is a first draft. Please send comments/error
reports/suggestions to amk at amk.ca. This URL is probably not going to
@@ -373,14 +373,17 @@
List comprehensions have the form::
[ expression for expr in sequence1
+ if condition1
for expr2 in sequence2
+ if condition2
for expr3 in sequence3 ...
+ if condition3
for exprN in sequenceN
- if condition ]
+ if conditionN ]
The elements of the generated list will be the successive
-values of ``expression``. The final ``if`` clause is
-optional; if present, ``expression`` is only evaluated and added to
+values of ``expression``. The ``if`` clauses are
+all optional; if present, ``expression`` is only evaluated and added to
the result when ``condition`` is true.
The ``for...in`` clauses contain the sequences to be iterated over.
@@ -394,13 +397,18 @@
Python code::
for expr1 in sequence1:
+ if not (condition1):
+ continue
for expr2 in sequence2:
- ...
+ if not (condition2):
+ continue
+ ...
for exprN in sequenceN:
- if (condition):
- # Append the value of
- # the expression to the
- # resulting list.
+ if not (conditionN):
+ continue
+ # Append the value of
+ # the expression to the
+ # resulting list.
This means that when there are multiple ``for...in``
clauses, the resulting list will be equal to the product of the
@@ -444,7 +452,7 @@
count, so if you want to create an iterator that will be immediately
passed to a function you could write::
- obj_total = sum(obj.count for obj in list_all_objects())
+ obj_total = sum(obj.count for obj in list_all_objects())
Generators
@@ -782,7 +790,7 @@
[9878, 9828, 8442, 7953, 6431, 6213, 2207, 769]
(For a more detailed discussion of sorting, see the Sorting mini-HOWTO
-in the Python wiki at http://wiki.python.org/moin/SortingHowto.)
+in the Python wiki at http://wiki.python.org/moin/HowTo/Sorting.)
The ``any(iter)`` and ``all(iter)`` built-ins look at
the truth values of an iterable's contents. ``any()`` returns
@@ -857,7 +865,7 @@
``def`` statements makes things a little bit better::
def combine (a, b):
- return 0, a[1] + b[1]
+ return 0, a[1] + b[1]
return reduce(combine, items)[1]
@@ -1133,7 +1141,8 @@
The author would like to thank the following people for offering
suggestions, corrections and assistance with various drafts of this
-article: Ian Bicking, Raymond Hettinger, Jim Jewett, Leandro Lameiro.
+article: Ian Bicking, Nick Coghlan, Raymond Hettinger, Jim Jewett,
+Leandro Lameiro.
Version 0.1: posted June 30 2006.
More information about the Python-checkins
mailing list