Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python abc 和six #32

Open
sfPPP opened this issue Aug 27, 2018 · 0 comments
Open

python abc 和six #32

sfPPP opened this issue Aug 27, 2018 · 0 comments

Comments

@sfPPP
Copy link
Owner

sfPPP commented Aug 27, 2018

abc 讲解参考
Python本身不提供抽象类和接口机制,要想实现抽象类,可以借助abc模块
abc是Abstract Base Classes的缩写
一般用 @abc.abstractmethod装饰器修饰抽象方法
一般用@six.add_metaclass(abc.ABCMeta)装饰器修饰抽象类
例:

@six.add_metaclass(abc.ABCMeta)
class PluginBase(object):
    @abc.abstractmethod
    def func_a(self, data):

@six.add_metaclass(abc.ABCMeta)用于指定元类,兼容python2和3
@abc.abstractmethod用于声明抽象方法;
abc.ABCMeta.register方法用于将任意类注册为虚拟子类
通过标准方式继承抽象类的类,必须全部实现抽象类的抽象方法,否则不能实例化
虚拟子类不影响subclasses, 可以不实现抽象方法,只要不调用。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant