-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Mandatory Iterator with "yield" #633
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
Comments
Hi! Like you say, 'yield from' check the return type and allow it if it's a subtype of 'Iterable' like you can see here: The 'yield' word only allows 'Iterator' like you can see here: Actually, yield and yield from, explained in the PEPs (225 and 380), return an Iterator (because both have a next function). So, I think that the change should be change 'yield from' to only allow an Iterator as return type more than let 'yield' allow Iterable subclass. |
But the thing is that
In the same way that my first example returns a |
Yes, it's a subclass, and I've rediscovered why I wrote the 'yield from' support to allow Iterable subtype.
Both, 'yield' and 'yield from' have next (iterator) and iter (iterable), so, changing whan I've said in my previous comment, seems correct to allow iterable subtypes. |
Is it intended that the use of
yield
mandates the return typeIterator
?For example, if I want the squares of numbers:
I cannot reimplement it as an iterator:
without getting
Iterator function return type expected for "yield"
(even ifIterator
actually is a subclass ofIterable
).However, it works with
yield from
:What is the reasoning?
The text was updated successfully, but these errors were encountered: