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

268 python inheritance abstract-class class abstract

如何在Python中创建类或方法摘要?

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

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

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

class G(F):
    pass
Run Code Online (Sandbox Code Playgroud)

然后我也无法实例化G,因为它调用了它的超类__new__方法.

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

ale*_*sel 477

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

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

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

    @abstractmethod
    def foo(self):
        pass
Run Code Online (Sandbox Code Playgroud)

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

>>> 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>
Run Code Online (Sandbox Code Playgroud)

  • @CharlieParker - `@ abstractmethod`使得在实例化类之前必须覆盖修饰函数*.从文档:`具有从ABCMeta派生的元类的类不能被实例化,除非它的所有抽象方法和属性都被覆盖. (21认同)
  • 有没有办法禁止用户在没有任何@abstractmethod方法的情况下创建Abstract()? (12认同)
  • @abstractmethod做了什么?你为什么需要它?如果类已经建立为抽象,那么编译器/解释是否应该知道所有方法都来自有问题的抽象类? (7认同)
  • @CharlieParker - 基本上,它允许你以一种方式定义一个类,其中子类*必须*实现一组指定的方法来实例化. (4认同)
  • @Joe 看来你也可以将 `@abstractmethod` 用于 `__init__` 方法,参见 /sf/ask/3136046161/ (3认同)
  • 为什么我不能只使用“@abstractmethod”而不使用“ABC”?感觉很不Python。当我写abstractmethod时,这意味着这已经是一个抽象基类了。或者我错过了什么? (2认同)
  • 值得注意的是,“@abstractmethod”(例如“foo()”_can_)有一个不平凡的实现。虽然此代码不能直接使用(因为抽象类无法实例化),但实现“foo()”的派生类可以调用“super().foo()”。 (2认同)

Tim*_*ert 91

老派(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): [...]
Run Code Online (Sandbox Code Playgroud)

这与使用abc模块的功能不同.您仍然可以实例化抽象基类本身,在运行时调用抽象方法之前,您不会发现错误.

但是如果你正在处理一小组简单的类,也许只需要一些抽象的方法,这种方法比试图浏览abc文档要容易一些.

  • 欣赏这种方法的简单性和有效性。 (3认同)

小智 11

这是一个非常简单的方法,无需处理ABC模块.

__init__您希望成为抽象类的类的方法中,您可以检查self的"类型".如果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()
Run Code Online (Sandbox Code Playgroud)

运行时,它会产生:

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
Run Code Online (Sandbox Code Playgroud)

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

欧文


gre*_*pit 8

大多数以前的答案是正确的,但这是Python 3.7的答案和示例.是的,您可以创建一个抽象类和方法.正如提醒有时,类应该定义逻辑上属于类的方法,但该类不能指定如何实现该方法.例如,在下面的"父母和婴儿"课程中,他们都吃,但实施情况各不相同,因为婴儿和父母吃的食物种类不同,吃的次数也不同.因此,eat方法子类会覆盖AbstractClass.eat.

from abc import ABC, abstractmethod

class AbstractClass(ABC):

    def __init__(self, value):
        self.value = value
        super().__init__()

    @abstractmethod
    def eat(self):
        pass

class Parents(AbstractClass):
    def eat(self):
        return "eat solid food "+ str(self.value) + " times each day"

class Babies(AbstractClass):
    def eat(self):
        return "Milk only "+ str(self.value) + " times or more each day"

food = 3    
mom = Parents(food)
print("moms ----------")
print(mom.eat())

infant = Babies(food)
print("infants ----------")
print(infant.eat())
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

moms ----------
eat solid food 3 times each day
infants ----------
Milk only 3 times or more each day
Run Code Online (Sandbox Code Playgroud)

  • 为什么在“AbstractClass”构造函数中需要调用“super().__init__()”? (3认同)
  • 这也适用于*Python 3.6*__ +1 (2认同)

Cle*_*leb 8

正如其他答案中所述,是的,您可以使用abc模块在 Python 中使用抽象类。下面我举个实际的例子使用抽象@classmethod@property@abstractmethod(使用Python 3.6+)。对我来说,从我可以轻松复制和粘贴的示例开始通常更容易。我希望这个答案对其他人也有用。

让我们首先创建一个名为 的基类Base

from abc import ABC, abstractmethod

class Base(ABC):

    @classmethod
    @abstractmethod
    def from_dict(cls, d):
        pass
    
    @property
    @abstractmethod
    def prop1(self):
        pass

    @property
    @abstractmethod
    def prop2(self):
        pass

    @prop2.setter
    @abstractmethod
    def prop2(self, val):
        pass

    @abstractmethod
    def do_stuff(self):
        pass
Run Code Online (Sandbox Code Playgroud)

我们的Base类将始终具有 a from_dict classmethod、 a property prop1(只读)和 a property prop2(也可以设置)以及一个名为 的函数do_stuff。现在基于任何类构建Base都必须实现所有这四个方法/属性。请注意,对于抽象方法,需要两个装饰器 -classmethod和 abstract property

现在我们可以创建一个A这样的类:

class A(Base):
    def __init__(self, name, val1, val2):
        self.name = name
        self.__val1 = val1
        self._val2 = val2

    @classmethod
    def from_dict(cls, d):
        name = d['name']
        val1 = d['val1']
        val2 = d['val2']

        return cls(name, val1, val2)

    @property
    def prop1(self):
        return self.__val1

    @property
    def prop2(self):
        return self._val2

    @prop2.setter
    def prop2(self, value):
        self._val2 = value

    def do_stuff(self):
        print('juhu!')

    def i_am_not_abstract(self):
        print('I can be customized')
Run Code Online (Sandbox Code Playgroud)

所有必需的方法/属性都已实现,我们当然可以添加不属于Base(此处:)的其他功能i_am_not_abstract

现在我们可以这样做:

a1 = A('dummy', 10, 'stuff')
a2 = A.from_dict({'name': 'from_d', 'val1': 20, 'val2': 'stuff'})

a1.prop1
# prints 10

a1.prop2
# prints 'stuff'
Run Code Online (Sandbox Code Playgroud)

根据需要,我们不能设置prop1

a.prop1 = 100
Run Code Online (Sandbox Code Playgroud)

将返回

属性错误:无法设置属性

我们的from_dict方法也很好用:

a2.prop1
# prints 20
Run Code Online (Sandbox Code Playgroud)

如果我们现在B像这样定义第二个类:

class B(Base):
    def __init__(self, name):
        self.name = name

    @property
    def prop1(self):
        return self.name
Run Code Online (Sandbox Code Playgroud)

并尝试像这样实例化一个对象:

b = B('iwillfail')
Run Code Online (Sandbox Code Playgroud)

我们会得到一个错误

类型错误:无法使用抽象方法 do_stuff、from_dict、prop2 实例化抽象类 B

列出Base我们没有在B.


小智 6

这个将在python 3中工作

from abc import ABCMeta, abstractmethod

class Abstract(metaclass=ABCMeta):

    @abstractmethod
    def foo(self):
        pass

Abstract()
>>> TypeError: Can not instantiate abstract class Abstract with abstract methods foo
Run Code Online (Sandbox Code Playgroud)