CSE 1010 Lecture Notes - Lecture 28: Iterator, Iter

42 views2 pages
Verified Note

Document Summary

Cse 1010 - introduction to computing for engineers. A generator is a special kind of iterator. It is created as a function that retains its local state (aka the values of local variables) between calls. The yield statement is used in place of the return statement. The next function is used to get the next value from the generator. Example into a generator def gen1(): x = 0 while true: yield x x += 1. The presence of the yield tells python that this is a generator and not a normal function. When this function is called, it creates and returns a generator. >>> g = gen1() #this doesn"t run the function gen1, it gives you a generator of gen1. #this will keep going forever until it"s manually interrupted. A bounded generator example: def gen2(limit): x = 0 while x <= limit:

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents

Related Questions