4 ms·
That's not fair. `range()` is a function which outputs something and understanding what that something is is imperative to understanding what that Python code d
by CivBase 5y ago
That's not fair. `range()` is a function which outputs something and understanding what that something is is imperative to understanding what that Python code does. I didn't start talking about the internals of `range()` or how the language is interpreted/compiled because none of that is necessary to understanding what the code does.
I'm not just sharing my personal thoughts here. I know half a dozen people who tried and failed to start learning programming with Python. Some of them moved onto other languages and had a better time while others gave up on the skill entirely. This is just what I've learned from discussing the issues they had. I have no reason to disparage Python. It's my favorite language and by far the one I use most.
- boricj 5y agoAnd yet you've described how "range(n)" is implemented in exquisite details, whereas the caller only cares about its interface. 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" and at worst "it makes my for loop run n times". Which is actually a fairly reasonable interpretation as long as the beginner doesn't try to concatenate a range with a tuple. 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.
- 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.