0

I recently I have been digging around the python docs and trying to gain a better understanding of python internals. One thing that I am having trouble understating is python iterators.

I have read a lot of the previous stack posts about these which seem to be similar to other information on the net, which explains how methods like iter() and next() work and how to make a custom object iterable but what I can seem to understand is how python work "under the hood" with these.

As an example if I have a list of random numbers

L=[1000,2202,3456]

I then create a list_iterator object via the iter() method, as shown below.

it = iter(L)

This bring me to my main question:

  • dose the iterator ref the list obj in memory then when next is called it move on to the next ref where the number in this case is stored in memory?

At present my memory management knowledge of python is a little superficial which maybe why I not understanding properly how iterators in python work. But if anyone could clear this up I thank you in advance.

  • 3
    I think the answer is "yes". – mkrieger1 Dec 01 '21 at 22:51
  • 2
    You might be interested in [The Magic Behind Python Generator Functions](https://hackernoon.com/the-magic-behind-python-generator-functions-bc8eeea54220). – jarmod Dec 01 '21 at 22:52
  • 3
    The list_iterator object has a reference to a list object, and that list object has 3 references to int objects. The ints can be anywhere in memory and are not usually in an array-like. The current index for the list iteration is maintained on the list_iterator object. – wim Dec 01 '21 at 22:53
  • 2
    What would the alternative be? That the iterator object stores the references to all the contents of the list? That can be easily checked by modifying the original list after calling `next(it)`. It's more likely the iterator keeps the current index and a reference to the _list_, and the list handles returning elements at that index. – Pranav Hosangadi Dec 01 '21 at 22:57
  • @PranavHosangadi I mean, it *could* be implemented any way you like, I suppose. – juanpa.arrivillaga Dec 01 '21 at 22:57
  • I closed with a target dupe, let me know if that doesn't answer your question – juanpa.arrivillaga Dec 01 '21 at 22:58
  • Note, an iterator is a completely separate object to the iterable that returned it (unless the itereable is itself an iterator, in which case `iter(iterator) is iterator` **should** be true). an iterator is an object that is in charge of iterating over a collection. *How* it does it is *up to whoever implements the iterator*. If you are curious about `list_iterator`, specifically, yes, it basically keeps a reference to the list, a current index, and its `__next__` method access the value at the index, increments the stored index, and returns the value – juanpa.arrivillaga Dec 01 '21 at 23:01

0 Answers0