Bugs item #1517509, was opened at 2006-07-05 08:09 Message generated for change (Comment added) made by rhettinger You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1517509&group_id=5470
Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Python Interpreter Core Group: Python 2.5 >Status: Closed >Resolution: Wont Fix Priority: 5 Submitted By: Collin Winter (collinwinter) Assigned to: Nobody/Anonymous (nobody) Summary: filter() implementation does not match docs Initial Comment: The docs for the built-in function filter() claim that "filter(function, list) is equivalent to [item for item in list if function(item)] if function is not None and [item for item in list if item] if function is None". >>> class infinite_str(str): ... def __getitem__(self, index): ... return "a" ... >>> filter(None, infinite_str("1234")) 'aaaa' Now, if we translate this to a listcomp according to the docs: >>> [x for x in infinite_str("1234") if x] The listcomp version proceeds to chew up memory until it exhausts the system resources or is killed by the user. If the docs are to be believed, the filter() version should do the same thing. ---------------------------------------------------------------------- >Comment By: Raymond Hettinger (rhettinger) Date: 2006-07-05 09:58 Message: Logged In: YES user_id=80475 Please take a larger view when reviewing the docs. The listcomp analogy is very helpful in explaining what filter() does and readers would not benefit by its removal. Throughtout the docs, the phrase "is equivalent to" does not mean "is identical to" or "exactly the same as". In this case, you have isolated a non-guaranteed implementation detail that is almost always irrelevant. When an object such as infinite_str lies about its length, the consequent behavior is undefined. It is not hard to produce weird results when objects violate basic invariants such as len(istr)!=len(list(istr)) or the expected relation between __eq__ and __hash__. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1517509&group_id=5470 _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com