2

I'm trying to make a class I created into an infinite series generator.

The class is basically like this (I omitted the other methods as it is large):

class Step(object):
    ''' A Step taken through a field. '''
    def __init__(self, step_id, offset, danger,
        danger_limit=None, is_encounter = None, input=None):
        self.step_id = step_id
        self.offset = offset
        self.danger = danger
        self.is_encounter = is_encounter
        self.rnd = self.get_rnd(self.step_id, self.offset)
        self.danger_limit = self.get_danger_limit(self.rnd)

    def __repr__(self):
        return '{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}'.format(
            self.step_id, self.offset, self.danger,
            self.rnd, self.danger_limit, self.is_encounter, self.input)

    def __iter__(self):
        return self

    def advance_step(self, danger_inc=113):
        ''' To help with generator functions. '''
        # Advances the current step, so make a current step object.
        old_Step = Step(step_id=self.step_id, offset=self.offset, danger=self.danger)

        step_id = Step.get_step_id(old_Step.step_id)
        offset = Step.get_offset(step_id, old_Step.offset)
        danger = Step.get_danger(danger_inc, old_Step.danger, old_Step.danger_limit)
        new_step = Step(step_id = step_id, offset = offset, danger = danger)
        new_step.is_encounter = Step.is_encounter(new_step.danger,
            new_step.danger_limit)
        return new_step

    def next(self):
        ''' Yields next step in sequence. '''
        while True:
            yield self.advance_step()

I want to print a sequence of step objects. So I tried this:

def main():
    step = Step(step_id=250, offset=13, danger=0)
    print step.next()
    # Generate next 4 steps in sequence.
    for i in xrange(4):
        print step.next()

However, I get this output:

<generator object next at 0x104364cd0>
<generator object next at 0x104364cd0>
<generator object next at 0x104364cd0>
<generator object next at 0x104364cd0>
<generator object next at 0x104364cd0>

Expected output should look like a sequence of Step objects:

0   250 13  2048    0   74  19200   False   None
1   252 13  2048    2048    216 55552   False   None
2   254 13  2048    4096    163 41984   False   None
3   0   26  2048    6144    151 38912   False   None
4   2   26  2048    8192    212 54528   False   None

I get the expected output if I do the following:

step2 = step.advance_step()
step3 = step2.advance_step()
step4 = step3.advance_step()
print step,'\n', step2,'\n', step3,'\n', step4

How can I print the yielded step objects and not get the generator object back?

I suppose I'm doing something very wrong, but I cannot see what it is.

1 Answer 1

2

A next() method must return one value in the sequence the iterable represents. You returned a generator for each step.

Simply return the next value, there is no need to use a loop here:

def next(self):
    ''' Yields next step in sequence. '''
    return self.advance_step()

You could just rename advance_step() to next() of course.

From the iterator.next() documentation:

Return the next item from the container. If there are no further items, raise the StopIteration exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.