Horje
names of all methods in class introspect pythonm Code Example
names of all methods in class introspect pythonm
import gc
for obj in gc.get_objects():
    if isinstance(obj, some_class):
        dome_something(obj)
names of all methods in class introspect pythonm
import weakref

class A:
    instances = []
    def __init__(self, name=None):
        self.__class__.instances.append(weakref.proxy(self))
        self.name = name

a1 = A('a1')
a2 = A('a2')
a3 = A('a3')
a4 = A('a4')

for instance in A.instances:
    print(instance.name)
names of all methods in class introspect pythonm
from collections import defaultdict
import weakref

class KeepRefs(object):
    __refs__ = defaultdict(list)
    def __init__(self):
        self.__refs__[self.__class__].append(weakref.ref(self))

    @classmethod
    def get_instances(cls):
        for inst_ref in cls.__refs__[cls]:
            inst = inst_ref()
            if inst is not None:
                yield inst

class X(KeepRefs):
    def __init__(self, name):
        super(X, self).__init__()
        self.name = name

x = X("x")
y = X("y")
for r in X.get_instances():
    print r.name
del y
for r in X.get_instances():
    print r.name




Python

Related
udp client server chat program in python Code Example udp client server chat program in python Code Example
numpy argsort Code Example numpy argsort Code Example
python script to execute shell azure cli commands in python Code Example python script to execute shell azure cli commands in python Code Example
single line return python Code Example single line return python Code Example
discord.py find user by name Code Example discord.py find user by name Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7