Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 634949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:21:05+00:00 2026-05-13T20:21:05+00:00

Lets say I have a generator function that looks like this: def fib(): x,y

  • 0

Lets say I have a generator function that looks like this:

def fib():
    x,y = 1,1
    while True:
        x, y = y, x+y
        yield x

Ideally, I could just use fib()[10] or fib()[2:12:2] to get indexes and slices, but currently I have to use itertools for these things. I can’t use a generator for a drop in replacement for lists.

I believe the solution will be to wrap fib() in a class:

class Indexable(object):
    ....

fib_seq = Indexable(fib())

What should Indexable look like to make this work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:21:06+00:00Added an answer on May 13, 2026 at 8:21 pm
    import itertools
    
    class Indexable(object):
        def __init__(self,it):
            self.it = iter(it)
        def __iter__(self):
            return self.it
        def __getitem__(self,index):
            try:
                return next(itertools.islice(self.it,index,index+1))
            except TypeError:
                return list(itertools.islice(self.it,index.start,index.stop,index.step))
    

    You could use it like this:

    it = Indexable(fib())
    print(it[10])
    #144
    print(it[2:12:2])
    #[610, 1597, 4181, 10946, 28657]
    

    Notice that it[2:12:2] does not return [3, 8, 21, 55, 144] since the iterator had already advanced 11 elements because of the call to it[10].

    Edit: If you’d like it[2:12:2] to return [3, 8, 21, 55, 144] then perhaps use this instead:

    class Indexable(object):
    
        def __init__(self, it):
            self.it = iter(it)
            self.already_computed = []
    
        def __iter__(self):
            for elt in self.it:
                self.already_computed.append(elt)
                yield elt
    
        def __getitem__(self, index):
            try:
                max_idx = index.stop
            except AttributeError:
                max_idx = index
            n = max_idx - len(self.already_computed) + 1
            if n > 0:
                self.already_computed.extend(itertools.islice(self.it, n))
            return self.already_computed[index]
    

    This version saves the results in self.already_computed and uses those results
    if possible. Otherwise, it computes more results until it has sufficiently many
    to return the indexed element or slice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have an Erlang function, with spec. -spec foo(integer(), string()) ->
This is a conceptual question involving Hadoop/HDFS. Lets say you have a file containing
I have an echo web service running on lets say http://localhost:8080/axis2/services/Service1 . This service
Lets say I have a String: Go to this page: http://mysite.com/?page=1 , and I
I got a pretty simple parent/child relationship here, which looks like this: Email servers
Let's say I have this simple structure class FooDefinition { public FooDefinition Parent {
Lets say I have heap allocated A* , which I want to pass as
I'd like to call Python's distutils' or setuptools' setup() function in a slightly unconventional
Let's say I have a large HTML file with different kinds of tags, similar
I have a Silverlight application that generates a lot of Google Maps objects on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.