Read other people's python source code encountered this field this keyword, a variety of searches finally understand, here to do a summary:
- The usual for... .in... loop, in is followed by an array, which is an iterable object, similarly for chained lists, strings, files. It can be either mylist = [1, 2, 3] or mylist = [x*x for x in range(3)]. The drawback is that all the data is in memory and it will be very memory intensive if there is a huge amount of data.
- The generator is iterable, but you can only read it once. This is because it is only generated when it is used. For example mygenerator = (x*x for x in range(3)), note that () is used here and it is not an array, whereas the above example is [].
- The key to my understanding of a generator (generator) being able to iterate is that it has a next() method, which works by calling the next() method repeatedly until an exception is caught. You can test this with mygenerator above.
- A function with yield is no longer a normal function, but a generator generator, which can be used for iteration and works as above.
- yield is a return-like keyword that returns the value after (to the right of) the yield when it is encountered once. The point is: the next iteration starts with the code after the yield encountered in the previous iteration (the next line).
- Briefly understand: yield is return return a value, and remember the position of this return, the next iteration from this position (the next line) after the start.
- Functions with yield are not only used only in for loops, but can be used as arguments to a function, as long as the function's arguments allow iterating over the arguments. For example, the function, which has the prototype (iterable).
- The difference between send(msg) and next() is that send can pass arguments to a yield expression, which is then passed as the value of the yield expression, which is the value returned to the caller. --In other words, send can force a change in the previous yield expression value. For example, if there is a yield assignment in the function, a = yield 5, the first iteration to this point will return 5, and a has not yet been assigned a value. The second iteration, using .send(10), then, is to forcibly modify the yield 5 expression value to 10, would have been 5, then a = 10
- send(msg) and next() have a return value, their return value is the current iteration encounters yield, the value of the expression after the field, in fact, is the current iteration of the argument after the field.
- The first time you call it, you must either next() or send(None) first, or you will get an error, and the reason why it is None after send is because there is no previous yield at this point (according to section 8). It can be assumed that next() is equivalent to send(None).
Code example 1:
#encoding:UTF-8 def yield_test(n): for i in range(n): yield call(i) print("i=",i) # Do something else print("do something.") print("end.") def call(i): return i*2 # Use a for loop for i in yield_test(5): print(i,",")
The result:
>>>
0 ,
i= 0
2 ,
i= 1
4 ,
i= 2
6 ,
i= 3
8 ,
i= 4
do something.
end.
>>>
The key to understanding this is that on the next iteration, the code starts executing from the next jump statement in yield.
Code example 2:
def node._get_child_candidates(self, distance, min_dist, max_dist): if self._leftchild and distance - max_dist < self._median: yield self._leftchild if self._rightchild and distance + max_dist >= self._median: yield self._rightchild
Unlike the previous, there is no for loop in this function, but it can still be used for iteration.
node._get_child_candidates
The function has yield in it, so it becomes an iterator that can be used for iteration.
When performing the first iteration (actually calling the next() method), if there is a left node and the distance meets the requirements, the first yield will be performed, which will return self._leftchild and complete the first iteration.
The second iteration starts after the first field, and if there is a right node with the required distance, the second field is executed, which returns self._rightchild and completes the first iteration.
When the third iteration is executed, there is no more code after the second yield, an exception is caught and the iteration is exited.
Calling the process:
result, candidates = list(), [self] while candidates: node = () distance = node._get_dist(obj) if distance <= max_dist and distance >= min_dist: (node._values) (node._get_child_candidates(distance, min_dist, max_dist)) return result
uppernode._get_child_candidates(self, distance, min_dist, max_dist)
is placed as an argument in the extend() function, and why it can be used that way is because the extend function's arguments don't just support arrays, as long as it's an iterator. Its prototype is (iterable).
Code example 3:
summarize
Above is the entire content of this article, I hope the content of this article for your study or work has a certain reference learning value, thank you for your support. If you want to know more about the content please check the following related links