iterate through class python code example
Example 1: iterate through attributes of class python
L = [[getattr(self, attr), attr] for attr in dir(self) if not attr.startswith("__")]
Example 2: python iterate over instances of class
class IterRegistry(type):
def __iter__(cls):
return iter(cls._registry)
class Person(object):
__metaclass__ = IterRegistry
_registry = []
def __init__(self, name):
self._registry.append(self)
self.name = name