I would like to be able to create a list of all instances of a class. Another post here described how to do it by appending to a list during the __init__ routine but since I am a real Python newbie (and even more of a newbie to classes) I was hoping someone could post a simple code example to show a real world example of this. Thanks in advance.
andrewtrench
0
Newbie Poster
Recommended Answers
Jump to PostWell, here is an example
class Thingy(object): instances = [] def __init__(self): self.instances.append(self) def waste_time_and_memory(): t = Thingy() for i in range(5): waste_time_and_memory() print Thingy.instances """ My output --> [<__main__.Thingy object at 0x7f0581777c50>, <__main__.Thingy object at 0x7f0581777c90>, <__main__.Thingy object at 0x7f0581777cd0>, <__main__.Thingy object at 0x7f0581777d10>, <__main__.Thingy object …
All 2 Replies
Gribouillis
1,391
Programming Explorer
Team Colleague
danielochicaiza
commented:
Just joined to say thanks for this workaround. I think I will continue finding nice solutions in this place.
+0
andrewtrench
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.