2

I want to loop over a number of rows in a database and I have implemented an object in order to access the rows. Now when using this object I am not sure about the best practice. Implementing an iterator looks nice in the higher-level script but is somewhat unnecessary.

So I wondered, what are the benefits and disadvantages of looping over a list vs. a 'proper' iterator?

Alternatively, would either option make more sense if I changed the structure of my object?

Here is my code using python 2.7:

class process_scope(object):

    __columns = ['id', 'type', 'start', 'end', 'seconds', \
                        'static', 'table_ref']
    def __init__(self, force = False):
        self.force = force
        self._load_timeframes()
        self._reduce_timeframes()

    def __iter__(self):
        return self

    def next(self):
        try:
            return self.timeframes.pop()
        except IndexError:
            raise StopIteration

    def _load_timeframes(self):
        self.timeframes = ((1, 'type_1', 000, 999, 999, 0, 'table_1'),
                           (2, 'type_2', 000, 999, 999, 0, 'table_2'),
                           (3, 'type_3', 000, 999, 999, 1, 'table_3'),
                           (4, 'type_2', 000, 999, 999, 0, 'table_4'),
                           (5, 'type_1', 000, 999, 999, 0, 'table_5'))

    def _reduce_timeframes(self):
        relevant_timeframes = []
        for tf in self.timeframes:
            tf = dict(zip(process_scope.__columns, tf))
            if tf['static'] == 0 or self.force:
                relevant_timeframes.append(tf)

        self.timeframes = relevant_timeframes

Here is the usage option utilizing the iterator...:

for i in process_scope():
    print i

...and the option without implementing the iterator:

iter_ = process_scope().timeframes
for i in iter_:
    print i
6
  • If python 2.7 has generators, those are an easy alternative to iterators that offer the same lazy benefits. Commented Jun 5, 2019 at 12:41
  • @D.BenKnoble There are generators in Python 2.7, however to my knowlege they are more usefull in line or within functions, not when provided by an object. If I'm wrong, how would I integrate one, and what would be the advantage?
    – Lucas
    Commented Jun 5, 2019 at 12:47
  • Mutating self.timeframes isn't a good way to implement the iterable here. Just make __iter__ return iter(self.timeframes).
    – jonrsharpe
    Commented Jun 5, 2019 at 13:00
  • @jonrsharpe, Does this mean, i don't even need the next() method?
    – Lucas
    Commented Jun 5, 2019 at 13:06
  • 1
    Yes; you only need to implement next if the instance itself is the iterator (hence it's required when you return self from __iter__). next is implemented by the object iter returns.
    – jonrsharpe
    Commented Jun 5, 2019 at 13:07

0

Your Answer

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

Browse other questions tagged or ask your own question.