美文网首页
python ABCMeta class的作用abstract

python ABCMeta class的作用abstract

作者: 数据小新手 | 来源:发表于2020-04-02 20:01 被阅读0次

ABCMeta class的作用

该类不能直接使用,需要继承之后并重写@abstractmethod之后才能使用

from abc import ABCMeta, abstractmethod

class A(metaclass=ABCMeta):
    @abstractmethod
    def foo(self): pass

A()  # raises TypeError

class B(A):
    pass

B()  # raises TypeError

class C(A):
    def foo(self): print(42)

C()  # works

Note: The @abstractmethod decorator should only be used inside a class body, and only for classes whose metaclass is (derived from) ABCMeta.

@abstractmethod只能在继承ABCMeta的类中使用

reference :

https://legacy.python.org/dev/peps/pep-3119/

相关文章

网友评论

      本文标题:python ABCMeta class的作用abstract

      本文链接:https://www.haomeiwen.com/subject/rbefphtx.html