CS61A: Iterable, Iterator and Generator

  • Iterables have __iter__ method, which returns a iterator.
  • Iterators have __iter__method (returns themselves, since they’re iterators), and __next__  method.
  • Generators functions (functions with yield statement) return a iterator after being called. You can write __iter__ in iterables as a generator function.

 

  • Practice: Spring 2017 Final Q9 Solution

Screen Shot 2017-11-13 at 7.23.31 AM.png

Screen Shot 2017-11-13 at 7.23.43 AM

*list takes an iterable and return a list whose items are the same and in the same order as iterable’s items.

Screen Shot 2017-11-29 at 12.45.01 PM.png

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s