tags:

views:

86

answers:

4

I have written a custom container object.

According to this page, I need to implement this method on my object:

__iter__(self)

However, upon following up the link to Iterator Types in the Python reference manual, there are no examples given of how to implement your own.

Can someone post a snippet (or link to a resource), that shows how to do this?

[Edit]

The container I am writing, is a map (i.e. stores values by unique keys). dicts can be iterated like this:

for k, v in mydict.items()

In this case I need to be able to return two elements (a tuple?) in the iterator. It is still not clear how to implement such an iterator (despite the several answers that have been kindly provided). Could someone please shed some more light on how to implement an iterator for a map-like container object? (i.e. a custom class that acts like a dict)?

+3  A: 

I normally would use a generator function. Each time you use a yield statement, it will add and item to the sequence.

This would create an iterator that returns five, and then every item in some_list.

def __iter__(self):
   yield 5
   for x in some_list:
      yield x
mikerobi
+1  A: 

usually __iter__() just return self if you have already define the next() method (generator object):

here is a Dummy example of a generator :

class Test(object):

    def __init__(self, data):
       self.data = data

    def next(self):
        if not self.data:
           raise StopIteration
        retun self.data.pop()

    def __iter__(self):
        return self

but __iter__() can also be used like this: http://mail.python.org/pipermail/tutor/2006-January/044455.html

singularity
That's what you do for iterator classes, but the question is about container objects.
Glenn Maynard
+2  A: 

If your object contains a set of data you want to bind your object's iter to, you can cheat and do this:

>>> class foo:
    def __init__(self, *params):
           self.data = params
    def __iter__(self):
        if hasattr(self.data[0], "__iter__":
            return self.data[0].__iter__()
        return self.data.__iter__()
>>> d=foo(6,7,3,8, "ads", 6)
>>> for i in d:
    print i
6
7
3
8
ads
6
Legatou
+1  A: 

Another option is to inherit from the appropriate abstract base class from the `collections module as documented here.

In case the container is its own iterator, you can inherit from collections.Iterator. You only need to implement the next method then.

An example is:

>>> from collections import Iterator
>>> class MyContainer(Iterator):
...     def __init__(self, *data):
...         self.data = list(data)
...     def next(self):
...         if not self.data:
...             raise StopIteration
...         return self.data.pop()
...         
...     
... 
>>> c = MyContainer(1, "two", 3, 4.0)
>>> for i in c:
...     print i
...     
... 
4.0
3
two
1

While you are looking at the collections module, consider inheriting from Sequence, Mapping or another abstract base class if that is more appropriate. Here is an example for a Sequence subclass:

>>> from collections import Sequence
>>> class MyContainer(Sequence):
...     def __init__(self, *data):
...         self.data = list(data)
...     def __getitem__(self, index):
...         return self.data[index]
...     def __len__(self):
...         return len(self.data)
...         
...     
... 
>>> c = MyContainer(1, "two", 3, 4.0)
>>> for i in c:
...     print i
...     
... 
1
two
3
4.0

NB: Thanks to Glenn Maynard for drawing my attention to the need to clarify the difference between iterators on the one hand and containers that are iterables rather than iterators on the other.

Muhammad Alkarouri
Don't mix up iterable objects and iterators--you don't want to inherit from Iterator for an iterable object that isn't an iterator itself (eg. a container).
Glenn Maynard
@Glenn: You are correct that a typical container isn't an iterator. I just followed the question, which mentions the iterator types. I think it is more appropriate to inherit from a more suitable option as I said in the end of the answer. I will clarify this point in the answer.
Muhammad Alkarouri