python iterate over instances of class

Solutions on MaxInterview for python iterate over instances of class by the best coders in the world

showing results for - "python iterate over instances of class"
Alix
24 Mar 2020
1class IterRegistry(type):
2    def __iter__(cls):
3        return iter(cls._registry)
4
5class Person(object):
6    __metaclass__ = IterRegistry
7    _registry = []
8
9    def __init__(self, name):
10        self._registry.append(self)
11        self.name = name