Iterating over objects of a class

Kottiyath n.kottiyath at gmail.com
Wed Dec 24 12:37:31 EST 2008


Hi,
   How can I iterate over all the objects of a class?
   I wrote the code like following:
class baseClass(object):
    __registry = []

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

    def __iter__(self):
        baseClass.item = 0
        return self.__registry[0]

    def next(self):
        if baseClass.item >= len(self.__registry):
            raise StopIteration
        baseClass.item += 1
        return self.__registry[baseClass.item - 1]

For testing, create the following objects-
a = baseClass("Test1")
b = baseClass("Test2")

class subClass (baseClass):
   pass
c = subClass("Test3")

---->Actual Iteration<----
for i in a:
    print i.name

Test1
Test2
Test3

---------------------------------------------------
I see the following problems in the code:
1. I have to iterate over any of the objects. For correctness, I
wanted to iterate over the class, like
for i in baseClass():
   do x
but that will will create one more object - which I do not want.

2. If the subclass wants to do somethings in its constructor, I am not
sure how to update the registry.
class subClass (baseClass):
   def __init__(self, name):
       **do something**
       super.init(self, name)  ----> This errors out, saying it needs
super, not subClass

Another method I thought of implementing it was using generators -
where-in baseClass.objects() is a generator which will yield the
objects one by one - but even then the second issue remains.
If somebody can help me out, I would be very thankful.

Regards
K



More information about the Python-list mailing list