from collections.abc import Iterator from itertools import tee from typing import Tuple def hasNext(it)->Tuple[bool, Iterator]: ''' A support function to test hasNext for standard python iterators. usage, assuming it is already created (eg it=iter([1,2,3]): nx, it = hasNext(it) After the call, nx will be True iff it has a next value. The new it will be in original state (we have to damage the original iterator) ''' it,it1=tee(it) try: next(it1) return True, it except: return False, it def test(self)->None: a:object = "a" print(a)