diff options
author | Raymond Hettinger <python@rcn.com> | 2015-05-23 02:29:22 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2015-05-23 02:29:22 (GMT) |
commit | ec219ba1c04c4514b8b004239b1a0eac914dde4a (patch) | |
tree | 30abd24e56cc7faec9bb332728253c4a58f57e91 /Lib/_collections_abc.py | |
parent | 256613c605bf207ed77616c04563275cfbaf3ee4 (diff) | |
download | cpython-ec219ba1c04c4514b8b004239b1a0eac914dde4a.zip cpython-ec219ba1c04c4514b8b004239b1a0eac914dde4a.tar.gz cpython-ec219ba1c04c4514b8b004239b1a0eac914dde4a.tar.bz2 |
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 2f83543..0ca7deb 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): |