-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
range purports to implement the Sequence ABC, but is missing index and count methods #53459
Comments
>>> isinstance(range, collections.Sequence)
True
>>> [method for method in set(dir(collections.Sequence)) - set(dir(range(1))) if not method.startswith('_')]
['index', 'count'] |
In Python 2.6 and 2.7, the same problem applies to xrange objects. |
The attached patch adds the range.count and range.index methods. def count(self, ob):
if ob in self:
return 1
else:
return 0
def index(self, ob, start=0, stop=len(self)):
if ob in self:
idx = (ob - self.start) // self.step
if start < 0:
start = start + len(self)
if stop < 0:
stop = stop + len(self)
if start <= idx < stop:
return idx
raise ValueError("{!r} is not in range".format(ob)) |
Sorry, the previous patch has a reference leak. I'm attaching the fixed patch as issue9213a.diff (I also added a few tests with really big ranges). |
I can’t comment on C code, but the tests look good to me. |
I support adding index() and count() to range objects. |
Thank you for the patch. The patch doesn't handle the case where the object being searched for is !PyLong_CheckExact. For example, the user might pass in a sub-type of int. The existing range_contains supports that case, so it seems like we should support it in range_index and range_count. The Sequence ABC's .index method doesn't include the optional start and stop parameters that are present on list.index. Since it's not part of the ABC, it's not mandatory that we implement those for range.index. The patch includes support for start and stop. Attached is a greatly revised patch, with two significant changes:
Dropping support for the start and stop parameters greatly simplified the code. If we want to support start and stop, then the code will need to get more complicated to handle the !PyLong_CheckExact case (since PySequence_IterSearch doesn't support start and stop). My patch abstracts out most of the code that had originally been in range_contains into a new function range_contains_long so that it can be called by range_contains, range_count, and range_index. The diff makes that part look like a large change, but it's mostly a whitespace change (the refactored code lost one indentation level but is otherwise unchanged). I uploaded Daniel Urban's patch and mine to Rietveld: Any strong feelings on whether range.index should support the start and stop arguments provided by list.index, tuple.index, and str.index, but not by collections.Sequence.index? |
Thanks, indeed, we should support that.
I don't think that this is very important. |
I'd like to have this in 3.2. |
Thanks, Georg, for prodding. As a new committer, I have possibly been erring a little too far on the side of having my patches thoroughly reviewed before committing. I'll commit the patch on Monday if no one raises objections (after re-testing, of course). |
Committed as r84791. Question: should this bugfix be backported to Python 3.1 and to xrange objects in Python 2.7? Since it's a bugfix that adds new methods, it's a gray-area. (same question applies to the closely related Issue bpo-9212) |
I am -1 on adding new methods to builtins in bugfix releases. |
Sounds reasonable to me. I'll close this and the related 9212 (both fixes are already committed to the py3k branch). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: