-
Notifications
You must be signed in to change notification settings - Fork 792
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
__getitem__ iteration. #1927
Comments
Oops, I definitely skimmed the other issue too quickly. This is indeed a duplicate of #1855. Feel free to mark as such. |
Hmm, thanks for the head-up. I don't think this is a duplicate. Looks like there are four methods with special fallbacks implemented in CPython: https://docs.python.org/3/reference/datamodel.html#id9: We don't implement equivalent fallbacks in PyO3, but should consider doing so. |
Hmmm, I thought that this was not something that PyO3 needs to do, as it was handled automatically by CPython. Because I seem to recall these automatic fallbacks working in Cython for example, so long as I didn't define an (I.e. what I thought was happening is that PyO3 explicitly disables the protocols that aren't defined, thus signalling to CPython that the class it not iterable, and so CPython doesn't try the fallback? But is setting those |
You're right, on further investigation these automatic fallbacks are generated by CPython. The reason that the |
From PyO3 0.16 the sequence slot will now be filled (#2065). |
I have both
__len__()
__getitem__()
which I think is normally enough for Python to do iteration without me needing to create a separate iter object, at least that's how it's worked for me in other bindings.From Python docs:
Does PyO3 do something like that to block the fallback behavior? Is it possible to stop it from doing it?
I tried returning
PyRef<Self>
from__iter__
but then I get the error that a non-iterable was returned. If I define also__next__
to some dummy, of course it just doesn't iterate. I'd like to avoid having to use the__iter__
__next__
if I can, because I have a lot of small types and merely want them to be iterable out of convenience, and the desired behavior in my case is for Python to fall back to using__getitem__[]
iteration.For reference my actual
__getitem__
is as:Possibly related to #1855 or the ongoing migration work?
The text was updated successfully, but these errors were encountered: