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

2024-04-20 05:08:39 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

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

class G(F):
    pass

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

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


Tags: ofto方法instanceannew定义def
3条回答

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

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

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()

运行时,它会产生:

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

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

内部收益率

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

在Python 3.4及更高版本中,可以从^{}继承。在早期版本的Python中,需要将类的元类指定为^{}。在Python 3和Python 2中,指定元类有不同的语法。三种可能性如下所示:

# Python 3.4+
from abc import ABC, abstractmethod
class Abstract(ABC):
    @abstractmethod
    def foo(self):
        pass
# Python 3.0+
from abc import ABCMeta, abstractmethod
class Abstract(metaclass=ABCMeta):
    @abstractmethod
    def foo(self):
        pass
# Python 2
from abc import ABCMeta, abstractmethod
class Abstract:
    __metaclass__ = ABCMeta

    @abstractmethod
    def foo(self):
        pass

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

>>> 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>

以前的方法(pre-PEP 3119)就是在调用抽象方法时在抽象类中raise NotImplementedError

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文档要简单一些。

相关问题 更多 >