Loop better: a deeper look at iteration in Python
summary
Shows that a generator like squares gets exhausted (tuple(squares) produces the numbers but sum(squares) after that returns 0) and that testing membership can consume it so `9 in squares` can be True then False, explains that unpacking a dict yields keys because unpacking uses iteration, demonstrates QuerySet.iterator to avoid Django queryset caching for large results, compares a hand-rolled iterator class to an equivalent generator function and generator expression, and recommends generators are iterators, iterators are their own iterables, and itertools.islice for lazy work.
AI-written summary · about
chapters (16)
- 0:00Intro
- 0:32About me
- 1:20Problems
- 3:35Loops
- 8:25Iterators
- 10:14While loop
- 11:25Iterator protocol
- 12:12Why should we care
- 13:32Iterators are intervals
- 14:33Iterators are iterables
- 15:33Generators are iterators
- 16:00Why care about iterators
- 17:28Django query sets
- 19:15Django iterators
- 20:48For loop
- 26:28Conclusion
related talks
25mLazy Looping: The Next Iteration
29mPythonic iterators and generators
27m"Snakes & Ladders: A Gentle Introduction to Generators and Iterators"
02mTutorial Sneak Peek: "Lazy Looping in Practice..."
32mIterating Efficiently in Python with itertools
44mIterables and Iterators: Going Loopy With Python
15mPythonic FuncProg: List comprehensions
21mBecome a Better Pythonista by Abusing List Comprehensions and :=
