-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
Conversation
Objects/listobject.c
Outdated
if (!key) | ||
for (i = start; i < stop && i < Py_SIZE(self); i++) { |
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.
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
for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i) { |
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.
that's pretty pointless, also not the way the old code was. anyway, I've now modified it.
Objects/listobject.c
Outdated
else if (cmp < 0) | ||
return NULL; | ||
} | ||
if (!key) |
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.
PEP 7
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.
I was following the style of the cod,e but ok, pep 7 for new code.
Objects/listobject.c
Outdated
if (cmp > 0) | ||
return PyLong_FromSsize_t(i); | ||
else if (cmp < 0) | ||
return NULL; |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is not eq when cmp == 0
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the original code, only indented. See newest version.
Objects/listobject.c
Outdated
if (cmp > 0) | ||
return PyLong_FromSsize_t(i); | ||
else if (cmp < 0) | ||
return NULL; |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
asgain, original code, with indent.
Objects/listobject.c
Outdated
else if (cmp < 0) | ||
return NULL; | ||
} | ||
else |
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.
PEP 7
Objects/listobject.c
Outdated
Py_INCREF(item); | ||
PyObject *obj = PyObject_CallFunctionObjArgs(key, item, NULL); | ||
Py_DECREF(item); | ||
if (!obj) |
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.
PEP 7
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.
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. |
@@ -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 |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The 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
del data[:] | ||
return i[1] | ||
|
||
with self.assertRaises(ValueError): |
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. ValueError
is too broad.
{ | ||
Py_ssize_t i; | ||
|
||
if (start < 0) { | ||
start += Py_SIZE(self); | ||
if (start < 0) | ||
if (start < 0) { |
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.
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.
Choose a reason for hiding this comment
The 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.
Closing; see the issue for details. Adding |
This PR adds a keyword-only
key
argument tolist.index()
.When provided, each list item is transformed by the
key
callable 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