4 ms·
You don't need to cast the generator, you can do: next(itertools.islice(my_generator, n, n+1)) With that said... > In theory a "sufficiently smart language" s
by Cpoll 7y ago
You don't need to cast the generator, you can do:
next(itertools.islice(my_generator, n, n+1))
With that said...
> In theory a "sufficiently smart language" should have a feature to understand that `thing[3]` can be translated to "call `next()` 3 times
This might be a newbie trap, because next() isn't the same as indexing. What happens if I perform `thing[7]` followed by `thing[5]`? Should performing `thing[7]` put 1-6 in memory and turn the object into a generator-list hybrid?
- nnq 7y ago> Should performing `thing[7]` put 1-6 in memory and turn the object into a generator-list hybrid You're right here. Probably can't work like that since generators are too general, you can't expect them to be rewindable or to not have side effects... prob you'd need a more specialized concept like a "lazy list" that would be a subtype of generator with some extra restrictions that would make it possible to implement the "hybrid" structure as an implementation detail without changing semantics. Anyway... it would be too much work and probably would turn into a footgun.