diff options
author | Raymond Hettinger <python@rcn.com> | 2015-05-22 19:29:22 -0700 |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2015-05-22 19:29:22 -0700 |
commit | c90552d46d617427810653ebcb2b0fc063e42c74 (patch) | |
tree | 89b34c144118a2dee1bcf100b498d5c921bdbae8 /Lib/_collections_abc.py | |
parent | 2de94f7b4e5f793a767ffd9fb65cc52339d1f253 (diff) | |
download | cpython-c90552d46d617427810653ebcb2b0fc063e42c74.tar.gz |
Issue #23086: Add start and stop arguments to the Sequence.index() mixin method.
Diffstat (limited to 'Lib/_collections_abc.py')
-rw-r--r-- | Lib/_collections_abc.py | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/Lib/_collections_abc.py b/Lib/_collections_abc.py index 2f83543124..0ca7debddb 100644 --- a/Lib/_collections_abc.py +++ b/Lib/_collections_abc.py @@ -825,13 +825,23 @@ class Sequence(Sized, Iterable, Container): for i in reversed(range(len(self))): yield self[i] - def index(self, value): - '''S.index(value) -> integer -- return first index of value. + def index(self, value, start=0, stop=None): + '''S.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. ''' - for i, v in enumerate(self): - if v == value: - return i + if start is not None and start < 0: + start = max(len(self) + start, 0) + if stop is not None and stop < 0: + stop += len(self) + + i = start + while stop is None or i < stop: + try: + if self[i] == value: + return i + except IndexError: + break + i += 1 raise ValueError def count(self, value): |