说我有一个简单的对象:

class Providers:
    Apple = "Apple"
    Banana = "Banana"
    Cherries = "Seedless Cherries"


我希望能够执行类似if "Apple" in Providers..的操作,我相信这需要设置两个魔术方法__len____getitem__

我尝试了一些简单的事情

@classmethod
def __len__(cls):
    return 3


但是当我运行len(Providers)时我得到TypeError: object of type 'type' has no len()

但是Providers.__len__()返回3。

如何在不实例化的情况下获取类的len?还是需要始终使用__init__self.Apple = 'Apple'?实例化它们?

最佳答案

您需要使用metclass,因为执行len(Providers)就像执行type(Providers).__len__(Providers)
和你的情况:

       type(Providers) ==  <class 'type'>   # no __len__ method here


像这样的东西:

    class ProviderType(type):
        def __len__(self):
            # this will call __len__ defined on the class it self
            return self.__len__(self)


    class Providers(metaclass=ProviderType):
        Apple = "Apple"
        Banana = "Banana"
        Cherries = "Seedless Cherries"

        def __len__(self):
            return 1


    p = Providers()
    print(len(Providers))  # type(Providers).__len__(Providers)  equavalent to : ProviderType.__len__(Providers)
    print(len(p))    # type(p).__len__(p)  equal to:  Providers.__len__(p)


python解释器如何处理dunder(魔术方法)方法:type(SomeObject).__dunder__(SomeOBject)

关于python - 在没有实例的类对象上设置魔术方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57928272/

10-12 17:54