-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Two argument form of iter() is not implemented. #5384
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
Sort of related to #4400. |
Yes, this is simply not implemented, mainly because the same thing can be achieved in pure Python. As a workaround the following can be used: def iter2(c, s):
while True:
v = c()
if v == s:
raise StopIteration
yield v (This would be simpler with the walrus operator!) |
While I agree on the fact that this can be worked around in many ways. it is unfortunate that this is not explicitly called out in the documentation as a difference from python's core syntax. This would be a compatibility wrapper needed to make the two argument form of iter work without changing source code.
Where compat.py has something like this. S = some_singleton
_orig_iter = iter
def _compat_iter(c, s=S):
if s is S:
for x in _orig_iter(c):
yield x
else:
while True:
v = c()
if v == s:
raise StopIteration
yield v
iter = _compat_iter Would adding 2 argument form to the builtin iter function be an acceptable patch? |
It's not really a syntax difference, rather the fact that MicroPython implements a subset of the Python builtins. It could be added to the docs via
Implemented in C I think it'd be a large patch and therefore unlikely to be made part of default MicroPython builds. It could be made optional like the 2-arg form of |
This would be a good candidate for #5025. |
Found this not implemented the hard way. TypeError: function takes 1 positional arguments but 2 were given |
Labelling as |
I was also surprised to discover Naively, I would have expected to see a section in the docs on differences in "builtin methods" above/below the section on differences in "builtin types". Again naively, this breaks my assumptions about MicroPython a bit because it seems surprising th 8000 at the logic to support the second parameter would increase the size noticeably, nor present any performance cost. Yes, it's easy to work-around, but I wish I didn't need to consider this among the things constraining code's "portability to MicroPython". |
Note that the 2-argument |
Okay, looking a bit harder, I actually found the relevant documentation: https://docs.micropython.org/en/latest/genrst/modules.html#second-argument-to-next-is-not-implemented So I guess that documentation probably needs updating to clarify that it's now not available on some boards in the default builds and whether it is available is controlled by that compile time flag? |
Yeah I don't think there's a real policy now for 'conditionally different from CPython'. There are multiple other occurrences in these docs like that. |
Looking at this again after many years, I see that the difference in implementation between 1-arg I made a comment long ago:
I think my reasoning back then was because I was thinking that the implementation would need a NLR block to catch the stop iteration. But it doesn't anymore because we have In my opinion the 2-argument form of |
See #16902. |
This is a pretty fundamental built-in and having CPython-compatible behaviour is beneficial. The code size increase is not much, and ports/boards can still disable it if needed to save space. Addresses issue micropython#5384. Signed-off-by: Damien George <damien@micropython.org>
Now implemented. |
This is a pretty fundamental built-in and having CPython-compatible behaviour is beneficial. The code size increase is not much, and ports/boards can still disable it if needed to save space. Addresses issue micropython#5384. Signed-off-by: Damien George <damien@micropython.org>
This is a pretty fundamental built-in and having CPython-compatible behaviour is beneficial. The code size increase is not much, and ports/boards can still disable it if needed to save space. Addresses issue micropython#5384. Signed-off-by: Damien George <damien@micropython.org>
The builtin
iter(callable, sentinel)
from pep 234 is not implemented.The use case is something like this.
The two parameter form allows an easy way to make a callable into an iterable by using a sentinel.
It allows for replacing the do while workarounds with a simple iterator.
Credit to this stack overflow answer from jfs
https://stackoverflow.com/a/20014805
The text was updated successfully, but these errors were encountered: