Answers for "how to list all attributes of a python object"

1

get object attributes python

for att in dir(your_object):
    print (att, getattr(your_object,att))
Posted by: Guest on March-05-2021
1

python get attributes of object

getattr(object, 'attribute_name')
Posted by: Guest on July-14-2020
0

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'
Posted by: Guest on June-13-2020

Code answers related to "how to list all attributes of a python object"

Python Answers by Framework

Browse Popular Code Answers by Language