Home > other >  Bosses, excuse me the next iterator () method is where to get the data?
Bosses, excuse me the next iterator () method is where to get the data?

Time:10-24

Iterators and generator will be able to use their next iteration, and seems to be not how to use memory,
Generator is better understood, it is iterative and generate data, then why don't the iterator object how memory?
Like I was in for a length of 100 million list call iter method to get a corresponding iterator object, after I check call before and after the iter memory found the iterator doesn't take up any memory, then the iterator object call next method is where to get the data?
Internet along while didn't find want to answer,,,,,,,,

CodePudding user response:

Data can be stored on the hard disk, memory, CPU cache does not consider
Less memory footprint, it is read from the hard disk of bai,

CodePudding user response:

You can write a class to do the test with __iter__ method is iterative objects, plus __next__ method is an iterator,
The __next__ method is used for the current iteration object, usually record the current iteration objects, and analysis the next iteration object, is actually a generator

Can refer to the following case, the following case is a transfer of

 

# the Fibonacci sequence
The class Fabs () :
Def __init__ (self, Max) :
The self. The Max=Max
Self. N, the self. A, the self. The b=0, 1

# define __iter__ method
Def __iter__ (self) :
Return the self

# define __next__ method
Def __next__ (self) :
If self. NTMP=self. B
Self. A, the self. B=self. B, the self. A + self. B
The self. The n +=1
Return TMP
Raise StopIteration