-2

Have tried to create a python Single Linked list , but i'm not able to create a iterator. Here is my code :

class LinkedList: def __init__(self): self._head=self self._tail=self self._size=0 def __iter__(self): print 'Calling Iterator\n\n' _ListIterator(self._head) class ListObj: def __init__(self,value): self._data=value self._pointingTo=None class _ListIterator: def __init__(self,listHead): LIST=None self._curNode=listHead print dir(self._curNode) def __next__(self): if self._curNode._pointingTo is None: raise StopIteration else: item=self._curNode._data self._curNode=self._curNode._pointingTo return item 

This iterator is failing by throwing an error as

TypeError: __iter__ returned non-iterator of type 'NoneType' 
6
  • 1
    Do you really have to post the entire code? Please read sscce.org Also, please show your entire traceback. Commented May 12, 2014 at 5:46
  • Ok , I got that i am passing just a single LinkedList object in the iterator , but how can i pass the complete list as a whole ! Commented May 12, 2014 at 5:49
  • This is the entire traceback: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: iter returned non-iterator of type 'NoneType' Commented May 12, 2014 at 5:52
  • There is no reason to make the ListIterator object. Read the duplicate question and try that. Commented May 12, 2014 at 5:55
  • Yes , the linked helped,there is no point in making it a seperate object! Thanks Lego Stormtroopr Commented May 12, 2014 at 6:11

1 Answer 1

1
_ListIterator(self._head) 

You forgot to return this.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks , got all possibilities of making it work with/without seprate iterator object :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.