3 ms·
> And yet you've described how "range(n)" is implemented in exquisite details, whereas the caller only cares about its interface. I didn't not say anything abo
by CivBase 5y ago
> And yet you've described how "range(n)" is implemented in exquisite details, whereas the caller only cares about its interface.
I didn't not say anything about how `range()` is implemented. I only described exactly what you said the caller should care about: the interface. I briefly described the inputs and outputs, then I went into detail about how the output is used by the `for` loop.
> A beginner would further only care about its observable effects, which here would be at best something like "that returns something that's like a tuple containing the integers from 0 to n-1"
In which case you need to describe what a tuple is and how a for loop uses it. In which case we're back to talking about iterators. All `for` loops in Python use iterators. There is no getting away from that.
> When I write a for loop in Python, I don't think about StopIteration and stuff, I just want to iterate something. Only if I were to implement a custom generator or iterable would I worry about that.
Same for me. That's because we already know how this all works and we know what parts we actually need to consider to implement a solution. That hardest part of getting into programming is building a fundamental understanding of how features work and how you can use those features to solve problems. And Python is filled to the brim with relatively complex features designed to help experienced programmers solve complex problems quickly and easily.
Like I said in my original post, you can gloss over details if you want. But understanding iterators is essential to understanding how `for` loops work in Python and if you don't understand how a `for` loop works, you'll struggle with understanding how and when to use it in future applications.