是否可以在Python中创建抽象类?

如何在Python中使类或方法抽象?


我尝试__new__()像这样重新定义:


class F:

    def __new__(cls):

        raise Exception("Unable to create an instance of abstract class %s" %cls)

但是现在,如果我创建一个G继承自F这样的类:


class G(F):

    pass

那么我也无法实例化G,因为它调用了其超类的__new__方法。


有没有更好的方法来定义抽象类?


萧十郎
浏览 507回答 3
3回答

慕慕森

使用该abc模块创建抽象类。使用abstractmethod装饰器来声明方法抽象,并根据您的Python版本使用以下三种方式之一声明类抽象。在Python 3.4及更高版本中,您可以继承ABC。在Python的早期版本中,您需要将类的元类指定为ABCMeta。指定元类在Python 3和Python 2中具有不同的语法。三种可能性如下所示:# Python 3.4+from abc import ABC, abstractmethodclass Abstract(ABC):&nbsp; &nbsp; @abstractmethod&nbsp; &nbsp; def foo(self):&nbsp; &nbsp; &nbsp; &nbsp; pass# Python 3.0+from abc import ABCMeta, abstractmethodclass Abstract(metaclass=ABCMeta):&nbsp; &nbsp; @abstractmethod&nbsp; &nbsp; def foo(self):&nbsp; &nbsp; &nbsp; &nbsp; pass# Python 2from abc import ABCMeta, abstractmethodclass Abstract:&nbsp; &nbsp; __metaclass__ = ABCMeta&nbsp; &nbsp; @abstractmethod&nbsp; &nbsp; def foo(self):&nbsp; &nbsp; &nbsp; &nbsp; pass无论使用哪种方式,都将无法实例化具有抽象方法的抽象类,但是将能够实例化提供这些方法的具体定义的子类:>>> Abstract()Traceback (most recent call last):&nbsp; File "<stdin>", line 1, in <module>TypeError: Can't instantiate abstract class Abstract with abstract methods foo>>> class StillAbstract(Abstract):...&nbsp; &nbsp; &nbsp;pass...&nbsp;>>> StillAbstract()Traceback (most recent call last):&nbsp; File "<stdin>", line 1, in <module>TypeError: Can't instantiate abstract class StillAbstract with abstract methods foo>>> class Concrete(Abstract):...&nbsp; &nbsp; &nbsp;def foo(self):...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print('Hello, World')...&nbsp;>>> Concrete()<__main__.Concrete object at 0x7fc935d28898>

白板的微信

老式的方法(PEP 3119之前的方法)只是raise NotImplementedError在调用抽象方法的抽象类中进行。class Abstract(object):&nbsp; &nbsp; def foo(self):&nbsp; &nbsp; &nbsp; &nbsp; raise NotImplementedError('subclasses must override foo()!')class Derived(Abstract):&nbsp; &nbsp; def foo(self):&nbsp; &nbsp; &nbsp; &nbsp; print 'Hooray!'>>> d = Derived()>>> d.foo()Hooray!>>> a = Abstract()>>> a.foo()Traceback (most recent call last): [...]它没有与使用abc模块相同的好属性。您仍然可以实例化抽象基类本身,直到在运行时调用抽象方法,您才会发现错误。但是,如果您要处理的是一组简单的类,也许只有一些抽象方法,则此方法比尝试阅读abc文档要容易一些。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python