the code in figure replaces the getstudents function in class grades with function that uses kind of statement we have not yet useda yield statement def getstudents(self)"""return the students in the grade book one at time""if not self issortedself students sort(self issorted true for in self studentsyield figure new version of getstudents any function definition containing yield statement is treated in special way the presence of yield tells the python system that the function is generator generators are typically used in conjunction with for statements at the start of the first iteration of for loopthe interpreter starts executing the code in the body of the generator it runs until the first time yield statement is executedat which point it returns the value of the expression in the yield statement on the next iterationthe generator resumes execution immediately following the yieldwith all local variables bound to the objects to which they were bound when the yield statement was executedand again runs until yield statement is executed it continues to do this until it runs out of code to execute or executes return statementat which point the loop is exited the version of getstudents in figure allows programmers to use for loop to iterate over the students in objects of type grades in the same way they can use for loop to iterate over elements of built-in types such as list for examplethe code book grades(book addstudent(grad('julie')book addstudent(grad('charlie')for in book getstudents()print prints julie charlie thus the loop in figure that starts with for in course getstudents()does not have to be altered to take advantage of the version of class grades that contains the new implementation of getstudents the same for loop can iterate over the values provided by getstudents regardless of whether getstudents returns list of values or generates one value at time generating one value at this explanation of generators is bit simplistic to fully understand generatorsyou need to understand the way built-in iterators are implemented in pythonwhich is not covered in this book