[issue6324] in expression falls back to __iter__ before __getitem__
Anthony Foglia afog...@gmail.com added the comment: I've added Python 2.7 to the list of versions. The development docs have the same issue. Let me try another stab at what the docs should say. Following the suggestion to add it to 3.3.5, perhaps it should be: object.__contains__(self, item) Called to implement membership test operators. Should return true if item is in self, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs. If __contains__ is not provided but __iter__ is provided, the membership test x in y is true if and only if there is a value z reachable by iterating iter(y) before iter(y) throws a StopIteration exception. (If any other exception is raised, it is as if in raised that exception). If neither __contains__ nor __iter__ are provided but __getitem__ is provided, the membership test x in y is true if and only if there is a non-negative integer index i such that x == y[i], and all lower integer indices do not raise IndexError exception. (If any other exception is raised, it is as if in raised that exception). I worry that this is becoming more a description of in rather than __contains__. Either way, the last sentence in paragraph 6 of section 5.9 should refer not just to 3.3.1 (Basic Customization) but also 3.3.5 (Emulating Container Types). The latter is where __contains__, __iter__ and __getitem__ are defined. Perhaps the new wording should be, You can control comparison behavior of objects of non-built-in types by defining rich comparison methods like __gt__(), described in section Basic customization, and the membership test method __contains__(), described in section Emulating container types. -- versions: +Python 2.7 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6324 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue6324] in expression falls back to __iter__ before __getitem__
Georg Brandl ge...@python.org added the comment: Thanks, fixed in r75606. -- resolution: - fixed status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6324 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue6324] in expression falls back to __iter__ before __getitem__
Changes by Raymond Hettinger rhettin...@users.sourceforge.net: -- assignee: rhettinger - georg.brandl ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6324 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue6324] in expression falls back to __iter__ before __getitem__
Terry J. Reedy tjre...@udel.edu added the comment: In 3.1, Section 5.9 has For user-defined classes which define the __contains__() method, x in y is true if and only if y.__contains__(x) is true. For user-defined classes which do not define __contains__() and do define __getitem__(), x in y is true if and only if there is a non-negative integer index i such that x == y[i], and all lower integer indices do not raise IndexError exception. (If any other exception is raised, it is as if in raised that exception). However, discussion of how user-defined classes emulate builtins is mostly in 3.3. Special method names. I think some of the above should be moved, with or without revision, (or copied) to 3.3.5. Emulating container types. The current entry there says only object.__contains__(self, item) Called to implement membership test operators. Should return true if item is in self, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs. which seems to me inadequate, as it does not discuss the alternative, as many other entries in 3.3 do. Regardless of that, I verified that in 3.1, __iter__ is called in preference to __getitem__: class C(): def __iter__(s): print('in iter') return iter([]) def __getitem(s,i): print('in getitem') print(1 in C()) prints in iter False so some change is needed. -- nosy: +tjreedy ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6324 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue6324] in expression falls back to __iter__ before __getitem__
Terry J. Reedy tjre...@udel.edu added the comment: Side note: 2.5, including, I believe, the docs, is frozen except for security fixes. If you find 2.5 doc issues, please check the current 2.x development version, currently http://docs.python.org/dev/ to see if they have been fixed or not. -- versions: +Python 3.0, Python 3.1, Python 3.2 -Python 2.5, Python 2.6, Python 2.7 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6324 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue6324] in expression falls back to __iter__ before __getitem__
Raymond Hettinger rhettin...@users.sourceforge.net added the comment: 3.0 is no longer supported either -- versions: -Python 3.0 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6324 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue6324] in expression falls back to __iter__ before __getitem__
New submission from Anthony Foglia afog...@gmail.com: I was debugging a class where I defined __getitem__ and __iter__, but not __contains__. The documentation describing this case (at the end of section 5.9) is old and hasn't been updated for the iterator protocol. It should read something like: For user-defined classes which do not define __contains__() and do define __iter__() or __getitem__(), x in y is true if and only if there is a value z reachable from iter(y) before iter(y) throws a StopIteration exception. (If any other exception is raised, it is as if in raised that exception). Or something better worded. (I'm using Python 2.5, but I really doubt things have changes in 2.6 or 2.7. I don't know enough about 3.0 to know either way.) -- assignee: georg.brandl components: Documentation messages: 89607 nosy: afoglia, georg.brandl severity: normal status: open title: in expression falls back to __iter__ before __getitem__ versions: Python 2.5, Python 2.6, Python 2.7 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6324 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue6324] in expression falls back to __iter__ before __getitem__
Changes by Raymond Hettinger rhettin...@users.sourceforge.net: -- assignee: georg.brandl - rhettinger nosy: +rhettinger ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6324 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com