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.getitem() #45158

Closed
doerwalter opened this issue Jul 8, 2007 · 2 comments
Closed

itertools.getitem() #45158

doerwalter opened this issue Jul 8, 2007 · 2 comments
Assignees
Labels
extension-modules C modules in the Modules dir

Comments

@doerwalter
Copy link
Contributor

BPO 1749857
Nosy @doerwalter, @rhettinger
Files
  • diff.txt
  • 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 2007-07-31.07:03:40.000>
    created_at = <Date 2007-07-08.09:33:32.000>
    labels = ['extension-modules']
    title = 'itertools.getitem()'
    updated_at = <Date 2007-07-31.07:03:40.000>
    user = 'https://github.com/doerwalter'

    bugs.python.org fields:

    activity = <Date 2007-07-31.07:03:40.000>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = None
    closer = None
    components = ['Extension Modules']
    creation = <Date 2007-07-08.09:33:32.000>
    creator = 'doerwalter'
    dependencies = []
    files = ['8073']
    hgrepos = []
    issue_num = 1749857
    keywords = ['patch']
    message_count = 2.0
    messages = ['52812', '52813']
    nosy_count = 2.0
    nosy_names = ['doerwalter', 'rhettinger']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1749857'
    versions = ['Python 2.6']

    @doerwalter
    Copy link
    Contributor Author

    This patch adds itertools.getitem() which is basically equivalent to the following python code:

    _default = object()
    
    def getitem(iterable, index, default=_default):
       try:
          return list(iterable)[index]
       except IndexError:
          if default is _default:
             raise
          return default

    but without materializing the complete list. Negative indexes are supported too.

    @doerwalter doerwalter added the extension-modules C modules in the Modules dir label Jul 8, 2007
    @doerwalter doerwalter added the extension-modules C modules in the Modules dir label Jul 8, 2007
    @rhettinger
    Copy link
    Contributor

    Rejected after an offline discussion with the OP.

    The use case for getitem(n) with n as a negative number depended on an unlikely combination of circumstances:

    • you have an iterable that is not a sequence (otherwise, just use s[-n]).
    • the iterable is somewhat large (otherwise, just list it into memory)
    • the iterable is finite (otherwise, getitem() dies in a infinite loop)
    • you only want one entry (otherwise, you'll have make multiple passes)
    • that entry is located near the end (otherwise getitem() is memory intensive)
    • you know the entry's offset from the end but not from the beginning
    • identifying the record of interest depends only on its position, not its content

    In the common case where the index is zero, the preferred spelling is to use the next() method -- that is its purpose. For cases where the index is a positive integer, uing islice(it, n).next() suffices though it doesn't have the cute feature for a default value.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    extension-modules C modules in the Modules dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants