Titusz
Titusz

Reputation: 1477

Iterating over object instances of a given class in Python

Given a class that keeps a registry of its Objects:

class Person(object):
   __registry = []

   def __init__(self, name):
       self.__registry.append(self)
       self.name = name

How would I make the following code work (without using Person.__registry):

for personobject in Person:
    print personobject

While researching I found a hint that one could go for a __metaclass__ with a __getitem__-method. Any ideas how this would look like?

Upvotes: 20

Views: 46365

Answers (3)

S.Lott
S.Lott

Reputation: 391818

First, do not use double __ names. They're reserved for use by Python. If you want "private" use single _.

Second, keep this kind of thing as simple as possible. Don't waste a lot of time and energy on something complex. This is a simple problem, keep the code as simple as possible to get the job done.

class Person(object):
    _registry = []

    def __init__(self, name):
        self._registry.append(self)
        self.name = name

for p in Person._registry:
    print p

Upvotes: 17

dF.
dF.

Reputation: 75775

You can make your class object iterable with a simple metaclass.

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

(I have also changed __registry to _registry to make it easier to access from the metaclass). Then,

>>> p = Person('John')
>>> p2 = Person('Mary')
>>> for personobject in Person:
...     print personobject
...
<person.Person object at 0x70410>
<person.Person object at 0x70250>

Upvotes: 33

SilentGhost
SilentGhost

Reputation: 319531

you can do it with:

for item in Person.__registry:
    print(item)

Upvotes: 4

Related Questions