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
Two argument form of iter() is not implemented. #5384
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 |
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: