![]() You don't need to read the values twice.Usually we pass a list to it: > a = īut in your code, it gets a generator, which is good because: The extend() method is a list object method that expects an iterable and adds its values to the list. In this case, candidates.extend(node._get_child_candidates(distance, min_dist, max_dist)) exhaust all the values of the generator, but while keeps creating new generator objects which will produce different values from the previous ones since it's not applied on the same node. It's a concise way to go through all these nested data even if it's a bit dangerous since you can end up with an infinite loop. The loop iterates on a list, but the list expands while the loop is being iterated. ![]() of the candidateĬandidates.extend(node._get_child_candidates(distance, min_dist, max_dist)) # at all the children of the children of the children, etc. # so the loop will keep running until it will have looked # Add the children of the candidate in the candidate's list # If distance is ok, then you can fill the result # Get the distance between obj and the candidate ![]() # Get the last candidate and remove it from the list # Loop on candidates (they contain only one element at the beginning) # there is no more than two values: the left and the right childrenĬaller: # Create an empty list and a list with the current object reference # If the function arrives here, the generator will be considered empty If self._leftchild and distance - max_dist = self._median: # AND if the distance is ok, return the next child # If there is still a child of the node object on its left # Here is the code that will be called each time you use the generator object: Generator: # Here you create the method of the node object that will return the generatorĭef _get_child_candidates(self, distance, min_dist, max_dist): That can be because the loop has come to an end, or because you no longer satisfy an "if/else". This will continue until the generator is considered empty, which happens when the function runs without hitting yield. Then, each subsequent call will run another iteration of the loop you have written in the function and return the next value. The first time the for calls the generator object created from your function, it will run the code in your function from the beginning until it hits yield, then it'll return the first value of the loop. Then, your code will continue from where it left off each time for uses the generator. The function only returns the generator object, this is a bit tricky. To master yield, you must understand that when you call the function, the code you have written in the function body does not run. Here it's a useless example, but it's handy when you know your function will return a huge set of values that you will only need to read once. ![]() > print(mygenerator) # mygenerator is an object! > mygenerator = create_generator() # create a generator Yield is a keyword that is used like return, except the function will return a generator. BUT, you cannot perform for i in mygenerator a second time since generators can only be used once: they calculate 0, then forget about it and calculate 1, and end calculating 4, one by one. It is just the same except you used () instead of. Generators do not store all the values in memory, they generate the values on the fly: > mygenerator = (x*x for x in range(3)) Generators are iterators, a kind of iterable you can only iterate over once. These iterables are handy because you can read them as much as you wish, but you store all the values in memory and this is not always what you want when you have a lot of values. in." on is an iterable lists, strings, files. When you use a list comprehension, you create a list, and so an iterable: > mylist = Įverything you can use " for. Reading its items one by one is called iteration: > mylist = When you create a list, you can read its items one by one. And before you can understand generators, you must understand iterables. To understand what yield does, you must understand what generators are.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |