Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Why can't I iterate over a list? Is my exception class borked?

I've already looked at this question: Python iterators – how to dynamically assign self.next within a new style class?

but this doesn't help me because I want to iterate of an attribute of the error which is a list (ie, already iterable) without having to use the attribute explicitly. I'm looking to do this:

class SCE(Exception):
    """
    An error while performing SCE functions.
    """
    def __init__(self, value=None):
        """
        Message: A string message or an iterable of strings.
        """
        if value is None:
            self._values = ['A general SCE error has occured.']
        elif isinstance(value, str):
            self._values = [value]
        else:
            self._values = list(value)

    def __iter__(self):
        return self._values

    def __repr__(self):
        return repr(self._values)

However, in the shell I get this:

try:
    raise CSE(['error one', 'error two'])
except CSE, e:
    for i in e:
        print(i)
Traceback (most recent call last):
  File "(stdin)", line 1, in (module)
TypeError: iter() returned non-iterator of type 'list'

I know I could remove the _ from _values and then iterate over e.values but I don't want to do that as it exposes the implementation of my Exception class.

like image 818
orokusaki Avatar asked Feb 01 '10 22:02

orokusaki


2 Answers

The __iter__ method should return an iterator object, but you are returning a list object. Use

def __iter__(self):
    return iter(self._values)

instead to fix this. From the documentation for object.__iter__ (my highlighting):

This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container.

like image 157
Torsten Marek Avatar answered Sep 24 '22 12:09

Torsten Marek


def __iter__(self):
    return iter(self._values)

Or a more generic:

def __iter__(self):
    for x in self._values:
        yield x
like image 27
Lennart Regebro Avatar answered Sep 22 '22 12:09

Lennart Regebro