The generator generator
To be honest, the name Generator is a misnomer that is too broad and confusing.
By generator, we mean an object that performs its internal logic step by step with additional methods, and we’ll discuss a special kind of function that generates this object: the generator function;
As you can see from what I understand above, generators are not an easy concept for beginners to understand, but they are important.
Generator function
First we’ll look at generator functions, starting with a generic function:
def PowTwoGen(max = 0):
n = 0
x=[]
while n < max:
x.append(2**n)
n += 1
return x
r = PowTwoGen(6)
print (r)
[1, 2, 4, 8, 16, 32]
Copy the code
This is a function that implements a list of exponents that return a 2;
Now let’s transform it into a generator function:
def PowTwo(max = 0):
n = 0
while n < max:
yield 2**n
n += 1
r = PowTwo(6)
print (r)
<generator object PowTwo at 0x7f2e94d7b360>
Copy the code
Notice that we’ve removed the return, we’ve removed the x List that connects to elements, and we’ve replaced it with yield, so if you’re confused, don’t panic, we’re getting to the point.
First, if you use the yield keyword inside a function, it is a generator function. A generator function does not return the actual data result, it only returns a generator object. When we say generator, we refer to this generator object.
The key now lies in the generator object. If you understand how it is used and what it is used for, you will know what the generator function used to generate it means.
The generator object
def PowTwoGen(max = 0):
n = 0
while n < max:
yield 2**n
n += 1
r = PowTwoGen(6)
print (r)
output = next(r)
print(output)
output = next(r)
print(output)
output = next(r)
print(output)
output = next(r)
print(output)
output = next(r)
print(output)
output = next(r)
print(output)
<generator object PowTwoGen at 0x7fcf837b2360>
1
2
4
8
16
32
Copy the code
< Generator Object PowTwoGen at 0x7FCF837b2360 > indicates that R is a generator object. This object has the address 0x7FCF837b2360 in memory, but it does not contain any data, only the algorithm. If you want to fetch data, use the next() method; Next () takes a result, indicating that the generator object is a recyclable body.
This is interesting, when you run the PowTwo normal function, it will immediately return you a List, which is nice, but it will take up memory, like 100,000 elements, which is awkward, whereas PowTwoGen Generator returns a generator object, It only takes up as much memory as an object, but that object represents an algorithm that takes 100,000 elements, and you can take them when you need them instead of taking them all at once.
Of course, the next() method is silly, as we use for in to fetch elements from generator objects:
def PowTwoGen(max = 0):
n = 0
while n < max:
yield 2**n
n += 1
r = PowTwoGen(6)
print (r)
output = []
for i in r:
if i >4:
output.append(i)
print(output)
<generator object PowTwoGen at 0x7f296ff62360>
[8, 16, 32]
Copy the code
Compared with [1, 2, 4, 8, 16, 32], we can flexibly take out [8, 16, 32], the storage is reduced a lot, does it feel flexible and cost-effective?
To generate a generator object, you do not have to use a generator function.
g = (x for x in range(10))
print(next(g))
0
Copy the code
Change the list generator [] to () to generate a generator object.
This paper address: http://larabase.com/collection/6/post/187
If you want to learn more about Python with me, please visit my website http://larabase.com