I am looking through some text file for a certain string with the method.
re.finditer(pattern,text) I would like to know when this returns nothing. meaning that it could find nothing in the passed text.
I know that callable iterators, have next() and __iter__
I would like to know if I could get the size or find out if it returns no string matching my pattern.
EDIT 3: The answer by @hynekcer is much much better than this.
EDIT 2: This will not work if you have an infinite iterator, or one which consumes too many Gigabytes (in 2010 1 Gigabyte is still a large amount of ram/ disk space) of RAM/disk space.
You have already seen a good answer, but here is an expensive hack that you can use if you want to eat a cake and have it too 🙂 The trick is that we have to clone the cake, and when you are done eating, we put it back into the same box. Remember, when you iterate over the iterator, it usually becomes empty, or at least loses previously returned values.
EDIT: Here is a safer version, but using it still requires some discipline. It does not feel quite Pythonic. You would get the best solution if you posted the whole relevant code sample that you are trying to implement.