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
itertools.permutations/.combinations should have len #71865
Comments
Right now, itertools.permutations and itertools.combinations and itertools.product objects don't have a >>> len(itertools.combinations(range(10), 5))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'itertools.combinations' has no len() I propose that a |
You have to exhausts the iterator to get the actual length. Some iterators have __length_hint__ but it's not required to give the truth. |
Ahh, I see. In that case, if the object passed-in has a length, use that. If not, then raise TypeError. e.g. proposed behavior: >>> len(itertool.combinations(range(10), 2))
45
>>> len(itertool.combinations(iter(range(10)), 2))
...
TypeError: itertools.combinations underlying object has no len() |
__length_hint__ returns an estimated length for the object. As you said, if the object passed-in has a length, we could use it. If not, then raise TypeError. But we will have two different behaviors in function of the parameter. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: