This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Minor error in the example of filter()
Type: resource usage Stage: resolved
Components: Documentation Versions:
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, enchyisle, georg.brandl, python-dev
Priority: normal Keywords:

Created on 2014-11-14 06:34 by enchyisle, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg231149 - (view) Author: Yi Bai (enchyisle) Date: 2014-11-14 06:34
Hi, I suppose there is a minor error in the example of the filter() function in 5.1.3 part of the document.

filter(function, sequence) returns a sequence consisting of those items from the sequence for which function(item) is true. If sequence is a string or tuple, the result will be of the same type; otherwise, it is always a list. For example, to compute a sequence of numbers not divisible by 2 or 3:

>>> def f(x): return x % 2 != 0 and x % 3 != 0
>>> filter(f, range(2, 25))
[5, 7, 11, 13, 17, 19, 23]

I think what the example does is "to compute a sequence of numbers not divisible by 2 and 3", not "2 or 3".
msg231150 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2014-11-14 07:38
For me the text is correct.  However, it depends on parsing the English sentence in the right way, which can be confusing.  The prose means "not (divisible by 2 or 3)" which is equivalent to "not divisible by 2 and not divisible by 3", therefore the Python code has an "and" operator.

It would probably be best to remove the negation, which also removes the ambiguity: "compute a sequence of numbers divisible by 2 or 3" or such.
msg231152 - (view) Author: Yi Bai (enchyisle) Date: 2014-11-14 10:07
Ah yes. You are right, Georg.
And as you suggested, it might be better to remove this ambiguity, for people with poor parsing skills like me. :)
msg231153 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-11-14 10:13
New changeset 5dd835edde1e by Georg Brandl in branch '2.7':
Closes #22868: make example less ambiguous.
msg231154 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2014-11-14 10:14
Done. Thanks for the report!
Date User Action Args
2022-04-11 14:58:10adminsetgithub: 67057
2014-11-15 05:08:00berker.peksagsetstage: resolved
2014-11-14 10:20:18georg.brandlsetstatus: open -> closed
resolution: fixed
2014-11-14 10:14:35georg.brandlsetstatus: closed -> open
resolution: fixed -> (no value)
messages: + msg231154

stage: resolved -> (no value)
2014-11-14 10:13:50python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg231153

resolution: fixed
stage: resolved
2014-11-14 10:07:20enchyislesetmessages: + msg231152
2014-11-14 07:38:27georg.brandlsetnosy: + georg.brandl
messages: + msg231150
2014-11-14 06:34:23enchyislecreate