3 ms·
The use-case is: I create a generator somewhere, then return, pass it around a lot, and finally use the generator twice somewhere else entirely. This is just s
by Confusion 17y ago
The use-case is: I create a generator somewhere, then return, pass it around a lot, and finally use the generator twice somewhere else entirely.
This is just silly. You can't use a generator twice, just like you can't keep reading from a file. When you've reached EOF, you're done. What does this guy expect, code that magically determines that when you ask for 'next()', you mean 'restart_at_the_beginning()'?
I also fail to see what this has to do with laziness: the generator lazily generates it's elements. However, at some points it's still done generating elements. That's not a failure of laziness or whatever, it's a failure of using the generator.
- lutorm 17y agoI thought he was saying that if you think it's a list you might write the code to iterate across it twice. Then you happen to have a generator and you might think that "it'll be just like a list" if you pass it instead. Doesn't seem quite so silly to me.
- Goladus 17y agoIt's also a fundamental issue with dynamic languages. Lots of code fails silently if you make faulty assumptions about the types of parameters.
- paulgb 17y agoThe way Python does it is intuitive to someone coming from an imperative background. But someone coming from a functional background could easily fall into the trap of reasoning with generators as lazy lists. To me, it's not obvious that map(fn, g) should have a side-effect on g. I'm not saying I agree with the guy, but to call it magic is misleading.
- jules 17y ago> This is just silly. You can't use a generator twice, just like you can't keep reading from a file. When you've reached EOF, you're done. What does this guy expect, code that magically determines that when you ask for 'next()', you mean 'restart_at_the_beginning()'? Yes you can't use a generator twice, that's the problem. If you use an imperative next() you have this problem. It can be solved by using lazy lists instead of generators.
- Goladus 17y agoPython is an imperative language. It supports functional style really well but it's definitely not a functional language. So whether it's "wrong" to be unable to use a generator twice is not as obvious as in a functional language.