1

I created an iterator to increment the figure number in various plotting function calls:

figndx=itertools.count()

I then proceed to call these throughout my code, passing next(figndx) as an argument to increment the value I use for the figure number: - for ex:

an.plotimg(ref_frame,next(figndx),'Ref Frame')
an.plotimg(new_frame,next(figndx),'New Frame')

etc...

After some particular function call, I want to read back the figndx value and store it in a variable for later use. However, when I look at figndx , it returns count(7), for example. How do I extract the '7' from this?

I've tried :

figndx

figndx.__iter__()

and I can't find anything else in the 'suggested' methods (when I type the dot (.)) that will get the actual iterator value. Can this be done?

`

12
  • 1
    What you want to do is like peek the value, I mean get the value without moving the iterator? Commented Oct 28, 2021 at 14:44
  • I don't think an itertools.count objects expose anyway to retrieve that value. I suppose, you could parse the string representation with a retro or something, but that would be relying on an implementation detail. Commented Oct 28, 2021 at 14:49
  • right @ Dani Mesejo.... I found this answer as I kept searching for alternatives, and it seems to work: stackoverflow.com/questions/38101507/…. figndx.__reduce__()[1][0] seems to work. Commented Oct 28, 2021 at 14:49
  • What do you mean by "the actual iterator"? figndx is the actual iterator. Commented Oct 28, 2021 at 14:50
  • 1
    @jrive yeah definitely don't do that Commented Oct 28, 2021 at 14:51

2 Answers 2

3

Just wrap a count object

class MyCount:
    def __init__(self, *args, **kwargs):
        self._c = itertools.count(*args, **kwargs)
        self._current = next(self._c)
    def __next__(self):
        current = self._current
        self._current = next(self._c)
        return current
    def __iter__(self):
        return self
    def peek(self):
        return self._current
         
Sign up to request clarification or add additional context in comments.

3 Comments

wow...cool, but beyond my current mastery of Python. Will have to digest it. Thank you. How is that class then used?
@jrive the same way, just use figndx = MyCount() then use figndx the same as you have been. If you want to peek at the next value that will come from the iterator, use figndx.peek()
You can actually subclass itertools.count and save some lines there. But this custom iterator approach is definitely cleaner than (repeated) teeing!
2

You can create yourself a peeker, using itertools.tee, and encapsulate the peek:

from itertools import count, tee

def peek(iterator):
    iterator, peeker = tee(iterator)
    return iterator, next(peeker)

Then you can call it like

figndx = count(1)    
next(figndx)
next(figndx)

figndx, next_value = peek(figndx)
next_value
# 3

4 Comments

But what happens if you do next(peeker) 3 times and next(figndx) two times?
Of course, you have to make yourself a new peeker for every peek.
Thank you!...I don't understand it, though. Within the function, when you do x,y =tee(iterator), the value is returned in peeker. But, why doesn't tee(figndx) work the same way?
tee just splits into independent iterators. That way you can call next on one of them without affecting the other. The value is returned by next(peeker)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.