Reputation: 113
I have one question when we call an iter(obj)on an iterable(list_1). It returns an iterator object. When we invoke the next method on this iterator object it returns the next value one at a time. My question is does the iterator object(iter_obj) contains the data(after we run the iter method) and it prints out the value one at a time or does it reference/use the iterable(list_1) when running the next(method)
list_1=[1,2,3,4]
iter_obj=iter(list_1)
next(iter_obj)
Upvotes: 1
Views: 287
Reputation: 1138
Your second assumption (iterator contains reference to list) is true.
If you have a look into the C source code of the iter object you'll see that it contains exactly two attributes:
it_index
: the index within the sequence ìt_seq
of the next object that shall be returned by iter()
it_seq
: the sequence (in your case list_1
) that shall be iteratedUpvotes: 4