I have read some posts on stackoverflow about how to check an object is an iterator in Python but it seems that they did not solve my question. I have this example from the book Effective Python
def normalize_defensive(numbers): if iter(numbers) is iter(numbers): # An iterator — bad! raise TypeError(‘Must supply a container’) total = sum(numbers) result = [] for value in numbers: percent = 100 * value / total result.append(percent) return result To use:
visits = [15, 35, 80] normalize_defensive(visits) # No error visits = ReadVisits(path) # ReadVisits is a class with an __iter__ method. normalize_defensive(visits) # No error it = iter(visits) normalize_defensive(it) >>> TypeError: Must supply a container So my question is in this line:
if iter(numbers) is iter(numbers): # An iterator — bad! Why this line check if the variable numbers is an iterator? When visits = [15, 35, 80], should it be true that iter(numbers) is iter(numbers)?