“Namen aller Methoden in der Klasse Introspect Pythonm” Code-Antworten

Namen aller Methoden in der Klasse 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
Xenophobic Xenomorph

Namen aller Methoden in der Klasse 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)
Xenophobic Xenomorph

Namen aller Methoden in der Klasse Introspect Pythonm

import gc
for obj in gc.get_objects():
    if isinstance(obj, some_class):
        dome_something(obj)
Xenophobic Xenomorph

Ähnliche Antworten wie “Namen aller Methoden in der Klasse Introspect Pythonm”

Fragen ähnlich wie “Namen aller Methoden in der Klasse Introspect Pythonm”

Weitere verwandte Antworten zu “Namen aller Methoden in der Klasse Introspect Pythonm” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen