使用元类来跟踪python中的实例

时间:2019-07-07 01:25:55

标签: python python-3.x metaclass

我需要跟踪某些类的实例(并对这些类进行其他处理)。我不想在相关类中声明任何额外的代码,因此理想情况下,所有内容都应在元类中处理。

我不知道如何为这些类的每个新实例添加一个弱引用。例如:

class Parallelizable(type):
    def __new__(cls, name, bases, attr):
        meta = super().__new__(cls, name, bases, attr)
        # storing the instances in this WeakSet
        meta._instances = weakref.WeakSet()
        return meta

    @property
    def instances(cls):
        return [x for x in cls._instances]

class Foo(metaclass=Parallelizable)
    def __init__(self, name):
        super().__init__()
        self.name = name

        # I would like to avoid having to do that - instead have the metaclass manage it somehow
        self._instances.add(self)

有什么想法吗?我似乎找不到元类方面的钩子来进入Foo的__init__。...

2 个答案:

答案 0 :(得分:2)

元类的“关联”类的每个新实例为__call__时调用的方法。如果您将代码记录在其中,那么这就是您需要做的所有工作:


from weakref import WeakSet

# A convenient class-level descriptor to retrieve the instances:

class Instances:
    def __get__(self, instance, cls):
        return [x for x in cls._instances]

class Parallelizable(type):
    def __init__(cls, name, bases, attrs, **kw):
        super().__init__(name, bases, attrs, **kw)
        cls._instances = WeakSet()
        cls.instances = Instances()

    def __call__(cls, *args, **kw):
        instance = super().__call__(*args, **kw)
        cls._instances.add(instance)
        return instance

相同的代码将完全不需要描述符,这是拥有报告实例的类属性的好方法。但是,如果WeakSet足够,则此代码就足够了:


from weakref import WeakSet
class Parallelizable(type):
    def __init__(cls, name, bases, attrs, **kw):
        super().__init__(name, bases, attrs, **kw)
        cls.instances = WeakSet()

    def __call__(cls, *args, **kw):
        instance = super().__call__(*args, **kw)
        cls.instances.add(instance)
        return instance

答案 1 :(得分:1)

您可以在attrs['__init__']中修饰Parallizable.__new__方法:

import weakref
import functools
class Parallelizable(type):
    def __new__(meta, name, bases, attrs):
        attrs['__init__'] = Parallelizable.register(attrs['__init__'])
        cls = super().__new__(meta, name, bases, attrs)
        cls._instances = weakref.WeakSet()
        return cls

    @classmethod
    def register(cls, method):
        @functools.wraps(method)
        def newmethod(self, *args, **kwargs):
            method(self, *args, **kwargs)
            self._instances.add(self)
        return newmethod

    @property
    def instances(cls):
        return [x for x in cls._instances]

class Foo(metaclass=Parallelizable):
    def __init__(self, name):
        "Foo.__init__ doc string"
        super().__init__()
        self.name = name

# Notice that Foo.__init__'s docstring is preserved even though the method has been decorated
help(Foo.__init__)
# Help on function __init__ in module __main__:
#
# __init__(self, name)
#     Foo.__init__ doc string

stilton = Foo('Stilton')
gruyere = Foo('Gruyere')
print([inst.name for inst in Foo.instances])
# ['Gruyere', 'Stilton']

del stilton
print([inst.name for inst in Foo.instances])
# ['Gruyere']