➜

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
标题: itertools.product not lazy
类型: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
状态: closed Resolution: fixed
Dependencies: 后续:
分配给: rhettinger 抄送列表: miss-islington, nougmanoff, remi.lapeyre, rhettinger, tim.peters
优先级: normal 关键字: patch

Created on 2020-05-28 12:55 by nougmanoff, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 20492 merged nougmanoff, 2020-05-28 15:34
PR 20498 merged miss-islington, 2020-05-28 16:46
Messages (5)
msg370201 - (view) Author: Ramil Nugmanov (nougmanoff) * 日期: 2020-05-28 12:55
def x(y):
    while True:
        yield y

p = product(x(1), x(2))

next(p)  # this string will never be reached.
msg370203 - (view) Author: Rémi Lapeyre (remi.lapeyre) * 日期: 2020-05-28 13:05
Hi Ramil, itertools.product() expect its argument to be finite iterables, it needs to keep all their elements in memory anyway at it "cycles around" to produce all possible pairs.

Basically, product(x(1), x(2)) is equivalent to product(tuple(x(1)), tuple(x(2))).


I see that the documentation does not mention that the arguments must be finite, could you open a PR to improve it?
msg370246 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) 日期: 2020-05-28 16:46
New changeset cfc6ce4d40f2f01314b7e283fb972a7bb3ed3faa by Ramil Nugmanov in branch 'master':
bpo-40806: Clarify that itertools.product immediately consumes its inpt (GH-20492)
https://github.com/python/cpython/commit/cfc6ce4d40f2f01314b7e283fb972a7bb3ed3faa
msg370249 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) 日期: 2020-05-28 16:50
Updated the docs to note that inputs are fully consumed before the iterator is run.

FYI, notes on infinite iterable inputs likely belong the FAQs because they aren't specific to product().  Similar effects would be seen with list(x(1)), sorted(x(1)), set(x(1)), tuple(x(1)), etc.
msg370250 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) 日期: 2020-05-28 16:58
New changeset e4cc3a7c1f5ba9ea2c3015a5bf09cb5b93db5d47 by Miss Islington (bot) in branch '3.9':
bpo-40806: itertools.product immediately consumes its inputs (GH-20492) (GH-20498)
https://github.com/python/cpython/commit/e4cc3a7c1f5ba9ea2c3015a5bf09cb5b93db5d47
历史
日期 用户 动作 参数
2022-04-11 14:59:31admin修改github: 84983
2020-05-28 16:58:36rhettinger修改消息: + msg370250
2020-05-28 16:50:13rhettinger修改状态: open -> closed
resolution: fixed
消息: + msg370249

stage: patch review -> resolved
2020-05-28 16:46:36miss-islington修改抄送: + miss-islington
pull_requests: + pull_request19747
2020-05-28 16:46:26rhettinger修改消息: + msg370246
2020-05-28 15:43:27rhettinger修改assignee: rhettinger
2020-05-28 15:34:32nougmanoff修改keywords: + patch
stage: patch review
pull_requests: + pull_request19741
2020-05-28 13:06:10remi.lapeyre修改抄送: + tim.peters, rhettinger
2020-05-28 13:05:53remi.lapeyre修改抄送: + remi.lapeyre
消息: + msg370203
2020-05-28 12:55:46nougmanoff创建