Skip to content
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

Closed
ClaudiuSaftoiu mannequin opened this issue Aug 4, 2016 · 5 comments
Closed

itertools.permutations/.combinations should have len #71865

ClaudiuSaftoiu mannequin opened this issue Aug 4, 2016 · 5 comments
Labels
type-feature A feature request or enhancement

Comments

@ClaudiuSaftoiu
Copy link
Mannequin

ClaudiuSaftoiu mannequin commented Aug 4, 2016

BPO 27678
Nosy @rhettinger, @bitdancer, @matrixise, @zhangyangyu

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:

assignee = None
closed_at = <Date 2016-08-04.13:02:21.376>
created_at = <Date 2016-08-04.07:25:07.728>
labels = ['type-feature']
title = 'itertools.permutations/.combinations should have len'
updated_at = <Date 2016-08-04.13:02:21.365>
user = 'https://bugs.python.org/ClaudiuSaftoiu'

bugs.python.org fields:

activity = <Date 2016-08-04.13:02:21.365>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2016-08-04.13:02:21.376>
closer = 'r.david.murray'
components = []
creation = <Date 2016-08-04.07:25:07.728>
creator = 'Claudiu Saftoiu'
dependencies = []
files = []
hgrepos = []
issue_num = 27678
keywords = []
message_count = 5.0
messages = ['271952', '271953', '271954', '271955', '271970']
nosy_count = 5.0
nosy_names = ['rhettinger', 'r.david.murray', 'matrixise', 'xiang.zhang', 'Claudiu Saftoiu']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue27678'
versions = ['Python 3.6']

@ClaudiuSaftoiu
Copy link
Mannequin Author

ClaudiuSaftoiu mannequin commented Aug 4, 2016

Right now, itertools.permutations and itertools.combinations and itertools.product objects don't have a len.

    >>> 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 len be added to them, which computes (& caches) & returns the length. If the underlying iterator doesn't have a length, then they can raise a TypeError as they do now.

@ClaudiuSaftoiu ClaudiuSaftoiu mannequin added the type-feature A feature request or enhancement label Aug 4, 2016
@zhangyangyu
Copy link
Member

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.

@ClaudiuSaftoiu
Copy link
Mannequin Author

ClaudiuSaftoiu mannequin commented Aug 4, 2016

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()

@matrixise
Copy link
Member

__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.

@bitdancer
Copy link
Member

It may be time to create a faq entry about len and itertools; it seems to be a very popular idea. For various reasons (documented in several issues in this tracker now), we are not going to add len to the iterators in itertools. See bpo-24849, and in particular msg248496.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants