首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >可以在Python中创建抽象类吗?

可以在Python中创建抽象类吗?
EN

Stack Overflow用户
提问于 2012-11-30 21:29:17
回答 9查看 252.9K关注 0票数 404

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

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

代码语言:javascript
复制
class F:
    def __new__(cls):
        raise Exception("Unable to create an instance of abstract class %s" %cls)

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

代码语言:javascript
复制
class G(F):
    pass

然后我也不能实例化G,因为它调用它的超类的__new__方法。

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

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2012-11-30 21:30:45

使用abc模块创建抽象类。使用abstractmethod装饰器声明方法抽象,并使用三种方法之一声明类抽象,具体取决于您的Python版本。

在Python3.4及更高版本中,您可以继承ABC。在早期版本的Python中,需要将类的元类指定为ABCMeta。在Python 3和Python 2中,指定元类的语法不同。下面显示了三种可能性:

代码语言:javascript
复制
# Python 3.4+
from abc import ABC, abstractmethod
class Abstract(ABC):
    @abstractmethod
    def foo(self):
        pass
代码语言:javascript
复制
# Python 3.0+
from abc import ABCMeta, abstractmethod
class Abstract(metaclass=ABCMeta):
    @abstractmethod
    def foo(self):
        pass
代码语言:javascript
复制
# Python 2
from abc import ABCMeta, abstractmethod
class Abstract:
    __metaclass__ = ABCMeta

    @abstractmethod
    def foo(self):
        pass

无论使用哪种方法,都无法实例化具有抽象方法的抽象类,但可以实例化提供这些方法的具体定义的子类:

代码语言:javascript
复制
>>> Abstract()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Abstract with abstract methods foo
>>> class StillAbstract(Abstract):
...     pass
... 
>>> StillAbstract()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class StillAbstract with abstract methods foo
>>> class Concrete(Abstract):
...     def foo(self):
...         print('Hello, World')
... 
>>> Concrete()
<__main__.Concrete object at 0x7fc935d28898>
票数 686
EN

Stack Overflow用户

发布于 2014-08-14 12:33:20

老式的方法(在PEP 3119出现之前)就是在调用抽象方法时在抽象类中使用raise NotImplementedError

代码语言:javascript
复制
class Abstract(object):
    def foo(self):
        raise NotImplementedError('subclasses must override foo()!')

class Derived(Abstract):
    def foo(self):
        print 'Hooray!'

>>> d = Derived()
>>> d.foo()
Hooray!
>>> a = Abstract()
>>> a.foo()
Traceback (most recent call last): [...]

这不像使用abc模块那样具有很好的属性。您仍然可以实例化抽象基类本身,直到您在运行时调用抽象方法时才会发现错误。

但是,如果您处理的是一小部分简单的类,可能只有几个抽象方法,那么这种方法比费力阅读abc文档要容易一些。

票数 133
EN

Stack Overflow用户

发布于 2018-01-16 13:30:33

这里有一种非常简单的方法,不必处理ABC模块。

在您希望成为抽象类的类的__init__方法中,您可以检查self的“类型”。如果self的类型是基类,则调用方尝试实例化基类,因此引发异常。下面是一个简单的例子:

代码语言:javascript
复制
class Base():
    def __init__(self):
        if type(self) is Base:
            raise Exception('Base is an abstract class and cannot be instantiated directly')
        # Any initialization code
        print('In the __init__  method of the Base class')

class Sub(Base):
    def __init__(self):
        print('In the __init__ method of the Sub class before calling __init__ of the Base class')
        super().__init__()
        print('In the __init__ method of the Sub class after calling __init__ of the Base class')

subObj = Sub()
baseObj = Base()

运行时,它会生成:

代码语言:javascript
复制
In the __init__ method of the Sub class before calling __init__ of the Base class
In the __init__  method of the Base class
In the __init__ method of the Sub class after calling __init__ of the Base class
Traceback (most recent call last):
  File "/Users/irvkalb/Desktop/Demo files/Abstract.py", line 16, in <module>
    baseObj = Base()
  File "/Users/irvkalb/Desktop/Demo files/Abstract.py", line 4, in __init__
    raise Exception('Base is an abstract class and cannot be instantiated directly')
Exception: Base is an abstract class and cannot be instantiated directly

这表明您可以实例化从基类继承的子类,但不能直接实例化基类。

票数 24
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13646245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档