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 7072277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:47:20+00:00 2026-05-28T05:47:20+00:00

Given a generator g = ( <expr> for x in <iter> ), is there

  • 0

Given a generator

g = ( <expr> for x in <iter> ),

is there any way to recover the expression and iterator used to define g?

E.g., a function that would behave like this:

expr, iter = f( ( x*x for x in range(10) ) )
expr(2) # 4
expr(5) # 25
iter[1] # 1
iter[9] # 9
iter[10] # raises IndexError

The reason I want this functionality is that I’ve made my own LazyList class. I want it to essentially behave like a generator, except allow access via getitem without having to iterate through k-1 elements before it can access the k’th element. Thanks.

Edit: Here’s a snapshot of the lazy list class:

class LazyList(object):
  def __init__(self, iter=None, expr=None):
    if expr is None:
      expr = lambda i: i
    if iter is None:
      iter = []
    self._expr = expr
    self._iter = iter

  def __getitem__(self, key):
    if hasattr(self._iter, '__getitem__'):
      return self._expr(self._iter[key])
    else:
      return self._iter_getitem(key)

  def __iter__(self):
    for i in self._iter:
      yield self._expr(i)

I’ve omitted the method _iter_getitem. All this does is iterate through _iter until it reaches the key’th element (or uses itertool’s islice if key is a slice). There’s also the common llmap, llreduce, etc. functions I’ve omitted but you can probably guess how those go.

One of my motivations for wanting to be able to decompose generators is to that I can elegantly initialize this class like

l = LazyList(x*x for x in range(10))

instead of

l = LazyList(range(10), lambda x: x*x)

But the real benefit is that this would be, with polish, a nice generalization of the generator concept and be able to be used in place of any generator (with the same memory saving benefits).

I’m using this with Django a lot because it works well with their querysets. I have a lot of code that is dependent on the list structures being lazy, because it returns multidimensional arrays that, if evaluated, would fetch way more data than I’d need.

  • 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-28T05:47:21+00:00Added an answer on May 28, 2026 at 5:47 am

    The closest I can think of is to disassemble the code object that is inside the generator expression. Something like

    >>> import dis
    >>> g = ( x*x for x in range(10) )
    >>> dis.dis(g.gi_code)
      1           0 LOAD_FAST                0 (.0)
            >>    3 FOR_ITER                15 (to 21)
                  6 STORE_FAST               1 (x)
                  9 LOAD_FAST                1 (x)
                 12 LOAD_FAST                1 (x)
                 15 BINARY_MULTIPLY     
                 16 YIELD_VALUE         
                 17 POP_TOP             
                 18 JUMP_ABSOLUTE            3
            >>   21 LOAD_CONST               0 (None)
                 24 RETURN_VALUE        
    

    That gives a little hint about what is happening, but tt’s not very clear, IMHO.

    There is another Stack Overflow question that deals with converting Python byte code into readable Python — maybe you can use that to get something more human readable back.

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

Sidebar

Related Questions

Is there a website generator that can search for a given URL and output
How would you implement a random number generator that, given an interval, (randomly) generates
Given this function, I want to replace the color with a random color generator.
Given a dictionary, I would like to get a new dictionary that is a
Given an application path (or NSBundle to an application, etc), is there a way
I'm looking for a parser generator that given an EBNF for a LL(k) language
Given an ASP.NET MVC view that generates a table of entries using a for
Given a regular expression, I'm looking for a package which will dynamically generate the
Given an interface or interfaces, what is the best way to generate an class
Is there a customizable documentation generator for C#? As opposed to native xml documentation

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.