-
-
Notifications
You must be signed in to change notification settings - Fork 35k
gh-113773: add list.index() "key" named argument #113772
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
Changes from all commits
9b5aa09
ffa2ed5
cbe2eaa
bec9b8d
3b46fdb
2398a99
c8ec6bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| :func:`list.index` now supports the *key* keyword-only argument. When provided, each list item is first transformed by it before comparint it with *value*. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2659,38 +2659,56 @@ list.index | |
| start: slice_index(accept={int}) = 0 | ||
| stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize | ||
| / | ||
| * | ||
| key: object=NULL | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AC has problems with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are you saying that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a bunch of these in the code: https://github.com/search?q=repo%3Apython%2Fcpython+%22+object+%3D+null%22&type=code |
||
|
|
||
| Return first index of value. | ||
|
|
||
| Raises ValueError if the value is not present. | ||
| A callable 'key' can be provided to transform each item before comparison with 'value'. | ||
| [clinic start generated code]*/ | ||
|
|
||
| static PyObject * | ||
| list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start, | ||
| Py_ssize_t stop) | ||
| /*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/ | ||
| Py_ssize_t stop, PyObject *key) | ||
| /*[clinic end generated code: output=23a18266d9a3a9b3 input=7115b8d01b704c54]*/ | ||
| { | ||
| Py_ssize_t i; | ||
|
|
||
| if (start < 0) { | ||
| start += Py_SIZE(self); | ||
| if (start < 0) | ||
| if (start < 0) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've seen other comments, but I have an oposite opinion: diff should be as minimal as possible. This is just a comment, no action needed, unless others also agree :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I respectfully disagree, the style should be consistent within the function. Either I convert the entire function to pep7, or stick to the existing style. Which should it be? Also, it is a bit hard to try to follow disagreeing opinions of reviewers. |
||
| start = 0; | ||
| } | ||
| } | ||
| if (stop < 0) { | ||
| stop += Py_SIZE(self); | ||
| if (stop < 0) | ||
| if (stop < 0) { | ||
| stop = 0; | ||
| } | ||
| } | ||
| for (i = start; i < stop && i < Py_SIZE(self); i++) { | ||
| PyObject *obj = self->ob_item[i]; | ||
| Py_INCREF(obj); | ||
| PyObject *obj, *item = self->ob_item[i]; | ||
| /* take reference to item, in case list modified by key or comparison */ | ||
| Py_INCREF(item); | ||
| if (key == NULL) { | ||
| obj = item; | ||
| } | ||
| else { | ||
| obj = PyObject_CallFunctionObjArgs(key, item, NULL); | ||
| Py_DECREF(item); | ||
| if (obj == NULL) { | ||
| return NULL; | ||
| } | ||
| } | ||
| int cmp = PyObject_RichCompareBool(obj, value, Py_EQ); | ||
| Py_DECREF(obj); | ||
| if (cmp > 0) | ||
| if (cmp > 0) { | ||
| return PyLong_FromSsize_t(i); | ||
| else if (cmp < 0) | ||
| } | ||
| else if (cmp < 0) { | ||
| return NULL; | ||
| } | ||
| } | ||
| PyErr_Format(PyExc_ValueError, "%R is not in list", value); | ||
| return NULL; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, assert the error message here.
ValueErroris too broad.