gh-113773: add list.index() "key" named argument#113772
gh-113773: add list.index() "key" named argument#113772kristjanvalur wants to merge 7 commits intopython:mainfrom
Conversation
| if (!key) | ||
| for (i = start; i < stop && i < Py_SIZE(self); i++) { |
There was a problem hiding this comment.
you can add
int cmp before for if stmt
and modify for-loops like that:
for (i = start, cmp = 0; i < stop && i < Py_SIZE(self) && cmp == 0; i++)
reference:
Line 452 in d36a365
There was a problem hiding this comment.
that's pretty pointless, also not the way the old code was. anyway, I've now modified it.
| else if (cmp < 0) | ||
| return NULL; | ||
| } | ||
| if (!key) |
There was a problem hiding this comment.
I was following the style of the cod,e but ok, pep 7 for new code.
| if (cmp > 0) | ||
| return PyLong_FromSsize_t(i); | ||
| else if (cmp < 0) | ||
| return NULL; |
There was a problem hiding this comment.
| if (cmp > 0) | |
| return PyLong_FromSsize_t(i); | |
| else if (cmp < 0) | |
| return NULL; | |
| if (cmp < 0) { | |
| return NULL; | |
| } | |
| return PyLong_FromSsize_t(i); |
There was a problem hiding this comment.
it is not eq when cmp == 0
There was a problem hiding this comment.
Yep, you're right.
There's should be
if (cmp == -1) {
return NULL;
}
else if (cmp == 1 ) {
return PyLong_FromSsize_t(i);
}There was a problem hiding this comment.
This is the original code, only indented. See newest version.
| if (cmp > 0) | ||
| return PyLong_FromSsize_t(i); | ||
| else if (cmp < 0) | ||
| return NULL; |
There was a problem hiding this comment.
| if (cmp > 0) | |
| return PyLong_FromSsize_t(i); | |
| else if (cmp < 0) | |
| return NULL; | |
| if (cmp < 0) { | |
| return NULL; | |
| } | |
| return PyLong_FromSsize_t(i); |
There was a problem hiding this comment.
asgain, original code, with indent.
| else if (cmp < 0) | ||
| return NULL; | ||
| } | ||
| else |
| Py_INCREF(item); | ||
| PyObject *obj = PyObject_CallFunctionObjArgs(key, item, NULL); | ||
| Py_DECREF(item); | ||
| if (!obj) |
There was a problem hiding this comment.
would be good if you pointed to the particular pep7 violation you have in mind. I don't think constructs such as !obj are forbidden, if that is what you mean.
|
I do not know, would it be supported by others, but this kind of change definitely needs a |
ef051f5 to
3b46fdb
Compare
NEWS added. |
| stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize | ||
| / | ||
| * | ||
| key: object=NULL |
There was a problem hiding this comment.
AC has problems with NULL default. To check this please run inspect.signature(list.index) before and after.
There was a problem hiding this comment.
are you saying that object=NULL is nowhere used in the code despite being a documented convenience way of signifying optional PyObject values? So, either fix AC, or the docs?
I was an active contributor before AC was introduced and I'm only learning it now.
I guess you want me to turn this into object=None and make comparisons with PyNone in the code.
There was a problem hiding this comment.
There is a bunch of these in the code: https://github.com/search?q=repo%3Apython%2Fcpython+%22+object+%3D+null%22&type=code
| del data[:] | ||
| return i[1] | ||
|
|
||
| with self.assertRaises(ValueError): |
There was a problem hiding this comment.
Please, assert the error message here. ValueError is too broad.
| if (start < 0) { | ||
| start += Py_SIZE(self); | ||
| if (start < 0) | ||
| if (start < 0) { |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.
|
Closing; see the issue for details. Adding |
This PR adds a keyword-only
keyargument tolist.index().When provided, each list item is transformed by the
keycallable before being compared tovalue, thus allowinga functional-style search of an item in a list:
This is based on an idea by @rhettinger from the discussion in PR #112498, where a case for finding an object in a list via
keyed lookup is made. This PR applies that idea directly to the list object underlying the heap in question.
list.index()is not explicitly documented anywhere, so there is no doc update.list.index()#113773