(So I’m trying to learn python. I figured it would be good to read code by people better than me. I decided to read through the email module…)
The init function for the Feedparser class in the email.feedparser module is defined as:
def __init__(self, _factory=message.Message):
"""_factory is called with no arguments to create a new message obj"""
self._factory = _factory
self._input = BufferedSubFile()
self._msgstack = []
self._parse = self._parsegen().next
self._cur = None
self._last = None
self._headersonly = False
The line I’m having trouble with is:
self._parse = self._parsegen().next
Which I think should mean ‘set the attribute self._parse to the value of the next attribute of the return value of the method self._parsegen()
As far as I can tell, self._parsgen() when called during __init__() will first call self._new_message() which will set/add values to self._cur, self._last, and self._msgstack. It will then assign an empty list object to the local variable headers then start iterating over the self._input object. I think the first value for line will be a NeedMoreData object. Since the NeedMoreData class just extends object it should have no attribute or method named next. So does next just refer back to the iterator (self._input)?
Is there any way to have a look at this in the interpreter so that I can step through each line of the script?
nextdoes refer to the generator. Since the_parsegen()method usesyield, it returns a generator object. Consider the following simple example (from IPython):So, yes, you are mostly right. It will fall down to the iterator, and reference the method returning the next value from it.
You can use pdb for that.