i don't know "self._iterator = iter(self._container)"in next code.
in django.http :
class HttpResponse(object):
def __iter__(self):
self._iterator = iter(self._container)
return self
def next(self):
chunk = self._iterator.next()
if isinstance(chunk, unicode):
chunk = chunk.encode(self._charset)
return str(chunk)
i read the api :
Return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, o must be a collection object which supports the iteration protocol (the
__iter__()
method), or it must support the sequence protocol (the__getitem__()
method with integer arguments starting at 0). If it does not support either of those protocols,TypeError
is raised. If the second argument, sentinel, is given, then o must be a callable object. The iterator created in this case will call o with no arguments for each call to itsnext()
method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned. One useful application of the second form ofiter()
is to read lines of a file until a certain line is reached. The following example reads a file until "STOP" is reached:
but i also don't know what the iter function made .
i know the __iter__:
class a(object):
def __init__(self,x=10):
self.x = x
def __iter__(self):
return self
def next(self):
if self.x > 0:
self.x-=1
return self.x
else:
raise StopIteration
Please try to use the code, rather than text, because my English is not very good, thank you