CSE 1010 Lecture Notes - Lecture 28: Iterator, Iter

39 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+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions