suppose we have many objects in memory. Each one has a distinct id. How can I iterate the memory to find a specific object that is compared to some id ? In order to grab it and use it through getattr ?
You should maintain a collection of these objects as they are created in a class attribute, then provide a class method for retrieving them.
Something along the lines of:
class Thing(object):
all = {}
def __init__(self, id, also, etc):
self.id = id
self.all[id] = self
@classmethod
def by_id(cls, id):
return cls.all[id]
You could ask the garbage collector for all existing objects and iterate through that collection to find what you are looking for, but thats probably the wrong thing to do, unless you have very special reasons to do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With