Repository navigation
Virtual iterators #392
markshannon
started this conversation in
Ideas
Replies: 4 comments 1 reply
|
I suppose this would only be useful if the sequence is a list or tuple? (Or perhaps some other builtin sequence like str.) Not for a user-defined Sequence subclass, we wouldn't be able to prove that |
0 replies
|
Yes, the main benefit would be for builtin sequences. |
0 replies
|
How would this work for situations like this? it = iter(list(range(20)))
next(it)
next(it)
for i in it:
if i > 5:
next(it)
if i > 10:
break
next(it)
next(it) |
1 reply
|
Now that we support |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Much like we have "virtual bound-methods" for calls to methods, we could have "virtual iterators" for iteration over sequences.
The
LOAD_METHODinstruction pushesNULL funcorfunc selfto the stack, the latter being a representation of the bound-methodtypes.MethodType(func, self).We could do something similar for
GET_ITER.For non-sequence iterators, like range, generators etc, it would push
NULL iter. For sequences, it would pushseq 0.The behavior of
FOR_ITERwould depend on whetherSECONDisNULL.NULL: Pushnext(iter)NULL: Pop index; pushindex+1; pushseq[index]If we allow unboxed integers on the stack, then this could be reasonably efficient with specialization.
For this to be useful, we would need have tagged integers on the stack.
I doubt it would be worth implementing tagged ints just for this, but if we had tagged values for another reason, e.g. deferred reference counts, then it could be a worthwhile enhancement.
All reactions