how to get list of all instance in class python

import weakref

class MyClass:

    _instances = set()

    def __init__(self, name):
        self.name = name
        self._instances.add(weakref.ref(self))

    @classmethod
    def getinstances(cls):
        dead = set()
        for ref in cls._instances:
            obj = ref()
            if obj is not None:
                yield obj
            else:
                dead.add(ref)
        cls._instances -= dead

a = MyClass("a")
b = MyClass("b")
c = MyClass("c")

del b

for obj in MyClass.getinstances():
    print obj.name # prints 'a' and 'c'

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
list all items in a python class object python list all methodes in class get list of all variables in class python return list of instances of a class in python how to list all attributes of a python object python list all objects in a class python return list of object from classmethod python get list of all functions in class how to get list of all instance in class python list all instances of a class python how to get all the methods of a class using instance variables python get all instances of class python find all objects of a class python find all objects of class get instances of class python how to see every instance of a class python python get all class instances how to list all objects of one class python how to add all instances of a class in a list in python how to get instances of class list python extend class with list shows all instances how to see all the objects created in a class python get all objects of a class python how to get a list of all instances of a class in python python get all instances of a class python get all instances how to get list of all objects of a class python find all existing instances of a class python find all instances of a class python list all instances python python list class instances get all instance from class python get all instances of a class python how to see all objects of a class in python instance of a class in a list python python print all instances of a class how to find list of instance in calss python how to print all instances of a class in python get all instances of a class python in class method django get all instances of a class python in class method find all instances of class python how to get all instances of a class in python how can I see all the objects of a class python printing names of class instances from a list in python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source