前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python实现抽象基类的3三种方法

Python实现抽象基类的3三种方法

作者头像
py3study
发布2020-01-06 17:16:37
8080
发布2020-01-06 17:16:37
举报
文章被收录于专栏:python3python3

Python的抽象基类类似于Java、C++等面向对象语言中的接口的概念。抽象基类提供了一种要求子类实现指定协议的方式,如果一个抽象基类要求实现指定的方法,而子类没有实现的话,当试图创建子类或者执行子类代码时会抛出异常。这里简单介绍一下Python实现抽象基类的三种方法。

方法一:使用NotImplementedError

见下面的测试代码,只有子类实现了run方法才能运行run。

代码语言:javascript
复制
>>> class Task():
    def __init__(self, x, y):
        self.x = x
        self.y = y

>>> class Task():
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def run(self):
        raise NotImplementedError('Please define "a run method"')

>>> t = Task(1, 2)
>>> t.run()
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    t.run()
  File "<pyshell#10>", line 6, in run
    raise NotImplementedError('Please define "a run method"')
NotImplementedError: Please define "a run method"
>>>
>>> class SubTask(Task):
    def __init__(self, x, y):
        super().__init__(x, y)

    def run(self):
        print('Task(x=%s, y=%s)' % (self.x, self.y))

>>> st = SubTask(1, 3)
>>> st.run()
Task(x=1, y=3)
>>> 

方法二:使用元类

代码语言:javascript
复制
代码语言:javascript
复制
class TaskMeta(type):
    def __new__(cls, name, bases, attrs):

        new_class = super(TaskMeta, cls).__new__(cls, name, bases, attrs)

        if attrs.pop('abstract', False):
            return new_class

        if not hasattr(new_class, 'run') or not callable(new_class.run):
            raise TypeError('Please define "a run method"')

        return new_class


class Task(metaclass=TaskMeta):
    abstract = True

    def __init__(self, x, y):
        self.x = x
        self.y = y


class SubTask(Task):
    def __init__(self, x, y):
        super().__init__(x, y)

    def run(self):
        print('Task(x=%s, y=%s)' % (self.x, self.y))

测试代码一:

代码语言:javascript
复制
>>> t = Task(1, 3)
>>> t.run()
Traceback (most recent call last):
  File "E:/Code/python3/loggingTest/task.py", line 32, in <module>
    t.run()
AttributeError: 'Task' object has no attribute 'run'
>>> st = SubTask(1, 3)
>>> st.run()
Task(x=1, y=3)



这个示例类似于方法一,但有一些细微的区别。第一个区别就是Task类本身仍然能被实例化,但是不能运行run方法,否则会抛出AttributeError错误。更为重要的区别在于子类。当子类被创建时元类会运行__new__方法,解释器讲不再允许创建没有run方法的子类。

>>> class SubTask(Task):
...    pass
...
Traceback (most recent call last):
  File "E:/Code/python3/loggingTest/task.py", line 31, in <module>
    class SubTask(Task):
  File "E:/Code/python3/loggingTest/task.py", line 10, in __new__
    raise TypeError('Please define "a run method"')
TypeError: Please define "a run method"



方法三:使用@abstractmethod
  abc模块提供了一个使用某个抽象基类声明协议的机制,并且子类一定要提供了一个符合该协议的实现。

import abc


class Task(metaclass = abc.ABCMeta):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    @abc.abstractmethod
    def run(self):
        pass


class SubTask(Task):
    def __init(self, x, y):
        super().__init__(x, y)

    def run(self):
        print('Task(x=%s, y=%s)' % (self.x, self.y))

class OtherSubTask(Task):
    def __init(self, x, y):
        super().__init__(x, y)

和方法一、方法二的示例类似,但略有不同。第一,Task类本身不能被实例化。

>>> t = Task(1, 3)
Traceback (most recent call last):
  File "E:/Code/python3/loggingTest/test.py", line 23, in <module>
    t = Task(1, 3)
TypeError: Can't instantiate abstract class Task with abstract methods run


这与方法一不同,方法一允许基类Task被实例化。
对于不能正确重写run方法的子类,在错误的情况下它与之前的两个方法的差别也是不同的。方法一中,使用NotImplementedError,最终在run方法被调用时引发NotImplementedError错误。在方法二中,使用了自定义的TaskMeta元类, 当这个抽象类被创建时引发TypeError错误。
当没有实现run方法的子类实例化时会报错,给出的错误信息与实例化Task类时给出的一样,逻辑上完全符合预期。

>>> ot = OtherSubTask(1, 3)
Traceback (most recent call last):
  File "E:/Code/python3/loggingTest/test.py", line 27, in <module>
    ot = OtherSubTask(1, 3)
TypeError: Can't instantiate abstract class OtherSubTask with abstract methods run
但是,当你定义了一个重新了run方法的子类时,那么子类就能够被实例化,就能正常工作。


>>> st = SubTask(1, 3)
>>> st.run()
Task(x=1, y=3)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档