1

Looping Over the Wrong Things

As in most procedural programming languages, Python has two kinds of loops: for and while. The semantics of these loops is very similar to that in most analogous languages. However, Python puts a special emphasis on looping over iterables—including lazy iterables along with concrete collections—which many languages do not do. Many of the mistakes in this chapter are “best practices” in other programming languages but are either stylistically flawed or unnecessarily fragile when translated too directly into Python.

Technically, Python also allows for recursion, which is another way of “looping,” but recursion depth is limited in Python, and tail-call optimization (see https://en.wikipedia.org/wiki/Tail_call) is ...

Get Better Python Code: A Guide for Aspiring Experts now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.