The value stack of generators that yielded

I am doing some weird things with python interpreter and I would like to understand the following.

My first question is: can yielded_generator.gi_frame.f_back return anything but None? Example?

My second question is about the value stack of yielded generators. Can I construct a pure-python code that will have anything on the value stack after the generator yields? Maybe some sort of a lambda expression?

Thanks in advance.

It is not clear what kind of code is ‘yielded_generator’. Some code example would help.

But generally, generators are sometimes used to avoid having to create long lists of values to be returned in the future. For example range(9999) would be implemented as a generator to avoid having to store the 9,999 values for later use. So there may be no value stack depending on how the generator was implemented

Hi Artem Pulkin,

Like Milton, I also do not really understand your question, sorry.
Perhaps it is too advanced and tied to the internal implementation.

If you could explain in more detail, with some sample code and data,
that might help.

Alternatively, you could try asking on the Python-Dev mailing list.

Milton made a comment about range:

“range(9999) would be implemented as a generator”

This is a common myth about range. It is not a generator, it is a lazy
sequence that computes its values only as needed, not in advance.

import inspect
inspect.isgenerator(range(9999))

→ returns False

range works something like this simplified version:

class range(object):
    def __init__(self, start, stop, step=1):
        self.start = start
        self.stop = stop
        self.step = step
    def __getitem__(self, i):
        value = self.start + i*self.step
        if value < self.stop:
            return value
        raise IndexError

So range has nothing to do with generators. Never has, never will :slight_smile: