Entry
If i want to find out all the objects in a particular class like parser, how do i find it?
Aug 29th, 2002 00:04
Peter Funk, bert cazzato,
I believe you want to find all instances of a specific class.
you could add a simple registry to the class to accomplish this:
class Parser:
instance_registry = {}
def __init__(self, ....):
"""This is the constructor method of class Parser"""
self.instance_registry[id(self)] = self
.... other init code ....
def foo(self):
....
Later you can retrieve a list of all parser instances:
for parser_id, parser_object in Parser.instance_registry.items():
do_something_with(parser_object)