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
Possible performance improvement for itertools.product example on Python Docs #85292
Comments
In the documentation the following example is given: def product(*args, repeat=1):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = [tuple(pool) for pool in args] * repeat
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod) The proposed enhancement uses a nested generator so no intermediate results are created. def product2(*args, repeat=1):
def concat(result, pool):
yield from (x+[y] for x in result for y in pool)
pools = [tuple(pool) for pool in args] * repeat
result = [[]]
for pool in pools:
result = concat(result, pool)
for prod in result:
yield (tuple(prod)) |
Thanks for the suggestion, but the purpose of the rough equivalent example is to give a better idea of what product() does. It is not about performance — that job falls to the actual implementation. Nested generators are an intermediate level Python skill, so using them in the example code makes the docs less accessible. |
Good morning Raymond, Thank you for this quick response. As for your statement: "Nested generators are an intermediate level Python skill, so using them in the example code makes the docs less accessible.". Let me point to the facts, that someone looking at itertools and trying to do a Cartesian Product Thus, I hope I am able to convince you to reconsider your decision especially when you look at the given examples from a wider perspective and more so when the suggested improvement is minor and easy to understand. Side note: one of the first tutorial videos I used to learn Python were the ones you prepared for Oreilly. Thank you and thank you for all the work you did and still doing for free for the Python language and the Python community over so many many many years.
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: Thanks for the suggestion, but the purpose of the rough equivalent example is to give a better idea of what product() does. It is not about performance — that job falls to the actual implementation. Nested generators are an intermediate level Python skill, so using them in the example code makes the docs less accessible. ---------- Python tracker <report@bugs.python.org> |
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: