Skip to content

itertools.product not lazy #84983

Closed
Closed

Description

@stsouko
mannequin
BPO 40806
Nosy @tim-one, @rhettinger, @miss-islington, @remilapeyre, @stsouko
PRs
  • bpo-40806: itertools.product docs clarify #20492
  • [3.9] bpo-40806: Clarify that itertools.product immediately consumes its inputs (GH-20492) #20498
  • 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 = 'https://github.com/rhettinger'
    closed_at = <Date 2020-05-28.16:50:13.281>
    created_at = <Date 2020-05-28.12:55:46.015>
    labels = ['3.8', 'type-bug', 'library']
    title = 'itertools.product not lazy'
    updated_at = <Date 2020-05-28.16:58:36.995>
    user = 'https://github.com/stsouko'

    bugs.python.org fields:

    activity = <Date 2020-05-28.16:58:36.995>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2020-05-28.16:50:13.281>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2020-05-28.12:55:46.015>
    creator = 'nougmanoff'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40806
    keywords = ['patch']
    message_count = 5.0
    messages = ['370201', '370203', '370246', '370249', '370250']
    nosy_count = 5.0
    nosy_names = ['tim.peters', 'rhettinger', 'miss-islington', 'remi.lapeyre', 'nougmanoff']
    pr_nums = ['20492', '20498']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue40806'
    versions = ['Python 3.8']

    Activity

    1. stsouko commented on May 28, 2020

      stsoukomannequin
      MannequinAuthor
      def x(y):
          while True:
              yield y
      
      p = product(x(1), x(2))
      
      next(p)  # this string will never be reached.
    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on May 28, 2020
    3. remilapeyre commented on May 28, 2020

      remilapeyremannequin
      Mannequin

      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?

    4. rhettinger commented on May 28, 2020

      @rhettinger
      Contributor

      New changeset cfc6ce4 by Ramil Nugmanov in branch 'master':
      bpo-40806: Clarify that itertools.product immediately consumes its inpt (GH-20492)
      cfc6ce4

    5. rhettinger commented on May 28, 2020

      @rhettinger
      Contributor

      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.

    6. rhettinger commented on May 28, 2020

      @rhettinger
      Contributor

      New changeset e4cc3a7 by Miss Islington (bot) in branch '3.9':
      bpo-40806: itertools.product immediately consumes its inputs (GH-20492) (GH-20498)
      e4cc3a7

    7. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    3.8 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions