News for hackers. Stuff that matters.
Re:Python is not a great programming language by Cpoll on Saturday October 12, 2019 @05:02PM (#21236267)
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?

Re:Python is not a great programming language by nnq on Saturday October 12, 2019 @05:25PM (#21236444)
> 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.