stay Python in , You can use a generator to return a single element at a time , Thus, a large amount of memory can be avoided . Calculate the house price function in the following example house_price
It is the chestnut of a generator , This function takes two arguments , among unit_price
Unit price per square meter ,areas
Is the area of the house for sale .
def house_price(unit_price ,areas):
for area in areas:
yield unit_price * area
unit_price = 1
areas = [100, 120, 110, 180]
for price in house_price(unit_price, areas):
print(f"Price: {
price}")
""" Price: 100 Price: 120 Price: 110 Price: 180 """
But the above function cannot solve a problem : During the generator call , We would like to change the unit price per square meter for calculating the house price . Therefore, we need a way to change the value of each iteration of the generator unit_price
Parameter values , Can this requirement be achieved ? But of course . Generator's send()
Methods can solve this problem .
Generator's send()
The method can go to The generator sends a value and returns the next value generated by the generator , Here is a simple chestnut to briefly introduce the method .
def my_generator():
receive = yield 3
yield f"{
receive}"
myg = my_generator()
print(myg.send(None)) # print(next(myg)) It is equivalent to its function
# 3
print(myg.send(5))
# 5
First , Explain receive = yield 3
This line of code , This line of code can be split into two parts , namely yield 3
and receive = yield
, The former means to generate a new value , The latter refers to receiving send()
Method . In the order of execution , call send()
The method will First yield 3
, When called again next()
Method receive
Assignment .
send() Method raises a
next()
Method call .
then , Then explain why you need myg.send(None)
, This is a send()
The lower level implementation of the method is rigidly specified , Parameters are not allowed for the first execution , The following low-level source code of the corresponding part :
if (f->f_lasti == -1) {
// f_lasti==-1 For the first time
if (arg && arg != Py_None) {
// First execution is not allowed with parameters
PyErr_SetString(PyExc_TypeError,
"can't send non-None value to a "
"just-started generator");
return NULL;
}
}
Among the above chestnuts , If you remove myg.send(None)
, May be an error :TypeError: can't send non-None value to a just-started generator
.
With send()
Method , We now give the solution to the problem in Section 1 :
def house_price(areas):
unit_price = yield # Receive initial unit_price
for area in areas:
unit_price = yield unit_price * area
unit_prices = [1, 2, 3, 1]
areas = [100, 120, 110, 180]
hp = house_price(areas)
hp.send(None) # Start the generator
for unit_price in unit_prices:
cur_price = hp.send(unit_price)
print(f"Price: {
cur_price}")
""" Price: 100 Price: 240 Price: 330 Price: 180 """
You can see through send()
Method , We can specify a room rate for each transaction .
As can be seen from the previous Introduction :send()
Method provides a communication mechanism between the caller and the generator , Through this mechanism, we can perform some special operations , Of course, it also depends on the demand .
Reference material :
The above is the whole content of this article , If you think it's good , You can like it or follow the blogger , Your support is an inexhaustible driving force for bloggers to create , Of course, if you have any questions, please criticize and correct !!!