cs 61a discussion 5
play

CS 61A Discussion 5 Iterators/Generators and Midterm Review Albert - PowerPoint PPT Presentation

CS 61A Discussion 5 Iterators/Generators and Midterm Review Albert Xu Kaavya Shah Slides: albertxu.xyz/teaching/cs61a/ Announcements Iterators, Iterables oh my iterable iterator Iterators, Iterables oh my iterable iterator Iterators,


  1. CS 61A Discussion 5 Iterators/Generators and Midterm Review Albert Xu Kaavya Shah Slides: albertxu.xyz/teaching/cs61a/

  2. Announcements

  3. Iterators, Iterables oh my iterable iterator

  4. Iterators, Iterables oh my iterable iterator

  5. Iterators, Iterables oh my iterable iter as in iteration! iterator

  6. Iterators, Iterables oh my iterable both iterators and iterables implement the __ iter__ method, meaning that iter as in iteration! given an instance x of an iterator or iterable, calling iter(x) will give you a iterator new iterator over x !

  7. Iterators, Iterables oh my iterable implying that it is ABLE to be iterated over! iterator

  8. Iterators, Iterables oh my iterable an iterable is any object, sequence or not, that can be iterated over! Examples include lists, tuples, sets, implying that it is ABLE to be iterated over! strings, and dictionaries! iterator

  9. Iterators, Iterables oh my iterable an iterable is any object, sequence or not, that can be iterated over! Examples include lists, tuples, sets, implying that it is ABLE to be iterated over! strings, and dictionaries! iterator this is the thing DOING the iteration!

  10. Iterators, Iterables oh my iterable an iterable is any object, sequence or not, that can be iterated over! Examples include lists, tuples, sets, implying that it is ABLE to be iterated over! strings, and dictionaries! iterator an iterator is an object that lets you get the next element of a sequence this is the thing DOING the iteration! repeatedly until no more elements exist.

  11. A Good Comparison *borrowed from Kevin T. Li

  12. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a)

  13. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter)

  14. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1

  15. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter)

  16. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2

  17. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter)

  18. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a)

  19. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter)

  20. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3

  21. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter)

  22. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration

  23. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter)

  24. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration

  25. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter)

  26. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter) 1

  27. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter) 1 >>> a

  28. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter) 1 >>> a [1, 2, 3]

  29. Generators how do you write your own iterators?

  30. Generators how do you write your own iterators? Using a list

  31. Generators how do you write your own iterators? >>> fibs = [1, 1, 2, 3, 5, 8] Using a list >>> fib_iter = iter(fibs)

  32. Generators how do you write your own iterators? >>> fibs = [1, 1, 2, 3, 5, 8] Using a list >>> fib_iter = iter(fibs) Two issues : 1) I have to calculate out values of fib myself 2) I can only make a finite iterator

  33. Generators how do you write your own iterators? >>> fibs = [1, 1, 2, 3, 5, 8] Using a list >>> fib_iter = iter(fibs) Two issues : 1) I have to calculate out values of fib myself 2) I can only make a finite iterator Using a generator

  34. Generators how do you write your own iterators? >>> fibs = [1, 1, 2, 3, 5, 8] Using a list >>> fib_iter = iter(fibs) Two issues : 1) I have to calculate out values of fib myself 2) I can only make a finite iterator >>> def fibs(): prev = 0 current = 1 Using a while True : generator yield current current = current + prev prev = current

  35. Generators how do you write your own iterators? >>> fibs = [1, 1, 2, 3, 5, 8] Using a list >>> fib_iter = iter(fibs) Two issues : 1) I have to calculate out values of fib myself 2) I can only make a finite iterator >>> def fibs(): prev = 0 current = 1 Using a while True : generator yield current current = current + prev prev = current >>> fib_iter = fibs()

  36. Generators how do you write your own iterators? >>> fibs = [1, 1, 2, 3, 5, 8] Using a list >>> fib_iter = iter(fibs) Two issues : 1) I have to calculate out values of fib myself 2) I can only make a finite iterator generator function >>> def fibs(): prev = 0 current = 1 Using a while True : generator yield current current = current + prev prev = current >>> fib_iter = fibs()

  37. Generators how do you write your own iterators? >>> fibs = [1, 1, 2, 3, 5, 8] Using a list >>> fib_iter = iter(fibs) Two issues : 1) I have to calculate out values of fib myself 2) I can only make a finite iterator generator function >>> def fibs(): prev = 0 current = 1 Using a while True : generator yield current current = current + prev prev = current >>> fib_iter = fibs() generator object (generator)

  38. a final note on equality if the second part doesn’t make sense, it’s okay. • a is b • Object equality • This tells you whether two names are pointing to the same thing (consult environment diagram)

  39. a final note on equality if the second part doesn’t make sense, it’s okay. • a is b • Object equality • This tells you whether two names are pointing to the same thing (consult environment diagram) • a == b • Value equality • This tells you whether two numbers or two lists contain identical values.

  40. a final note on equality if the second part doesn’t make sense, it’s okay. • a is b • Object equality • This tells you whether two names are pointing to the same thing (consult environment diagram) • a == b • Value equality • This tells you whether two numbers or two lists contain identical values. But what happens if you use is for checking number equality ? Weird things happen.

Recommend


More recommend