How is the yield keyword used in Python? What does it do?
For example, I try to understand this code (**):
def _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
It is called like this:
result, candidates = list(), [self] while candidates: node = candidates.pop() distance = node._get_dist(obj) if distance <= max_dist and distance >= min_dist: result.extend(node._values) candidates.extend(node._get_child_candidates(distance, min_dist, max_dist)) return result
')
What happens when calling the _get_child_candidates method? Is the list returned, some item? Is he called again? When do subsequent calls stop?
** The code belongs to Jochen Schulz (jrschulz), who wrote an excellent Python library for metric spaces. Here is the link to the source: http://well-adjusted.de/~jrschulz/mspace/
>>> mylist = [1, 2, 3] >>> for i in mylist : ... print(i) 1 2 3
>>> mylist = [x*x for x in range(3)] >>> for i in mylist : ... print(i) 0 1 4
>>> mygenerator = (x*x for x in range(3)) >>> for i in mygenerator : ... print(i) 0 1 4
>>> def createGenerator() : ... mylist = range(3) ... for i in mylist : ... yield i*i ... >>> mygenerator = createGenerator() # >>> print(mygenerator) # mygenerator ! <generator object createGenerator at 0xb7555c34> >>> for i in mygenerator: ... print(i) 0 1 4
# , def _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 # ,
# result, candidates = list(), [self] # ( ) while candidates: # node = candidates.pop() # distance = node._get_dist(obj) # , if distance <= max_dist and distance >= min_dist: result.extend(node._values) # , # , # <...> candidates.extend(node._get_child_candidates(distance, min_dist, max_dist)) return result
>>> a = [1, 2] >>> b = [3, 4] >>> a.extend(b) >>> print(a) [1, 2, 3, 4]
>>> class Bank(): # , (ATM — Automatic Teller Machine) ... crisis = False ... def create_atm(self) : ... while not self.crisis : ... yield "$100" >>> hsbc = Bank() # , >>> corner_street_atm = hsbc.create_atm() >>> print(corner_street_atm.next()) $100 >>> print(corner_street_atm.next()) $100 >>> print([corner_street_atm.next() for cash in range(5)]) ['$100', '$100', '$100', '$100', '$100'] >>> hsbc.crisis = True # , ! >>> print(corner_street_atm.next()) <type 'exceptions.StopIteration'> >>> wall_street_atm = hsbc.create_atm() # >>> print(wall_street_atm.next()) <type 'exceptions.StopIteration'> >>> hsbc.crisis = False # , , - ... >>> print(corner_street_atm.next()) <type 'exceptions.StopIteration'> >>> brand_new_atm = hsbc.create_atm() # , ! >>> for cash in brand_new_atm : ... print cash $100 $100 $100 $100 $100 $100 $100 $100 $100 ...
>>> horses = [1, 2, 3, 4] >>> races = itertools.permutations(horses) >>> print(races) <itertools.permutations object at 0xb754f1dc> >>> print(list(itertools.permutations(horses))) [(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (2, 3, 1, 4), (2, 3, 4, 1), (2, 4, 1, 3), (2, 4, 3, 1), (3, 1, 2, 4), (3, 1, 4, 2), (3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 1, 2, 3), (4, 1, 3, 2), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1)]
Source: https://habr.com/ru/post/132554/
All Articles