How do I get a reference to all classes implementing descriptor object in python

后端 未结 1 1004
醉梦人生
醉梦人生 2021-01-19 09:10

I am creating a descriptor, and I want to create a list inside it that holds references to all objects implementing it, it is supposed to be some kind of a shortcut where I

相关标签:
1条回答
  • 2021-01-19 09:56

    If that's all your descriptor do, it's a bit of an abuse of the descriptor protocol. Just overriding your class __new__ or __init__ would be simpler:

    class Foo(object):
        _instances = []
    
        def __new__(cls, *args, **kw):
            instance = object.__new__(cls)
            cls._instances.append(instance)
            return instance
    
        @classmethod
        def get_instances(cls):
            return self._instances
    
    0 讨论(0)
提交回复
热议问题