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

时间:2012-11-30 13:29:17

标签: python class inheritance abstract-class abstract

如何在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__方法。

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

14 个答案:

答案 0 :(得分:431)

使用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
# 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>

答案 1 :(得分:81)

当调用抽象方法时,旧学校(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文档要容易一些。

答案 2 :(得分:10)

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

在您希望成为抽象类的类的__init__方法中,您可以检查&#34;类型&#34;自我如果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

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

欧文

答案 3 :(得分:7)

这个将在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

答案 4 :(得分:7)

大多数以前的答案都是正确的,但这是 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())

输出:

moms ----------
eat solid food 3 times each day
infants ----------
Milk only 3 times or more each day

答案 5 :(得分:1)

是的,您可以使用abc(抽象基类)模块在python中创建抽象类。

此网站将为您提供帮助:http://docs.python.org/2/library/abc.html

答案 6 :(得分:0)

这也很有效,很简单:

class A_abstract(object):

    def __init__(self):
        # quite simple, old-school way.
        if self.__class__.__name__ == "A_abstract": 
            raise NotImplementedError("You can't instantiate this abstract class. Derive it, please.")

class B(A_abstract):

        pass

b = B()

# here an exception is raised:
a = A_abstract()

答案 7 :(得分:0)

您还可以利用__new__方法来发挥自己的优势。你只是忘记了什么。 __new__方法始终返回新对象,因此您必须返回其超类的new方法。请执行以下操作。

class F:
    def __new__(cls):
        if cls is F:
            raise TypeError("Cannot create an instance of abstract class '{}'".format(cls.__name__))
        return super().__new__(cls)

使用新方法时,必须返回对象,而不是None关键字。这就是您所错过的全部。

答案 8 :(得分:0)

如其他答案所述,可以,您可以使用abc module在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

我们的Base类将始终具有from_dict classmethodproperty prop1(只读)和property prop2(也可以设置)以及称为do_stuff的函数。现在基于Base构建的任何类都必须为方法/属性实现所有这些。请注意,对于抽象classmethod和抽象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')

所有必需的方法/属性均已实现,我们当然可以添加不属于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'

根据需要,我们无法设置prop1

a.prop1 = 100

将返回

  

AttributeError:无法设置属性

我们的from_dict方法也可以正常工作:

a2.prop1
# prints 20

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

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

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

并尝试实例化这样的对象:

b = B('iwillfail')

我们会收到错误消息

  

TypeError:无法使用抽象方法实例化抽象类B   do_stuff,from_dict,prop2

列出Base中定义的所有内容,而我们未在B中实现。

答案 9 :(得分:0)

我发现接受的答案以及其他所有答案都很奇怪,因为它们将self传递给抽象类。没有实例化抽象类,因此不能有self

所以尝试一下,它就可以了。

from abc import ABCMeta, abstractmethod


class Abstract(metaclass=ABCMeta):
    @staticmethod
    @abstractmethod
    def foo():
        """An abstract method. No need to write pass"""


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


FOO = Derived()
FOO.foo()

答案 10 :(得分:0)

 from abc import ABCMeta, abstractmethod

 #Abstract class and abstract method declaration
 class Jungle(metaclass=ABCMeta):
     #constructor with default values
     def __init__(self, name="Unknown"):
     self.visitorName = name

     def welcomeMessage(self):
         print("Hello %s , Welcome to the Jungle" % self.visitorName)

     # abstract method is compulsory to defined in child-class
     @abstractmethod
     def scarySound(self):
         pass

答案 11 :(得分:0)

在这里回答晚了,但要回答另一个问题“如何制作抽象的方法”,这点在这里,我提供以下内容。

# decorators.py
def abstract(f):
    def _decorator(*_):
        raise NotImplementedError(f"Method '{f.__name__}' is abstract")
    return _decorator


# yourclass.py
class Vehicle:
    def addGas():
       print("Gas added!")

    @abstract
    def getMake(): ...

    @abstract
    def getModel(): ...

仍然可以实例化类基 Vehicle 类以进行单元测试(与 ABC 不同),并且存在 Pythonic 引发的异常。哦对了,为了方便起见,这个方法的异常中你也得到了抽象的方法名。

答案 12 :(得分:-1)

在您的代码段中,您还可以通过为子类中的__new__方法提供实现来解决此问题:

def G(F):
    def __new__(cls):
        # do something here

但这是一个黑客,我建议你反对它,除非你知道你在做什么。对于几乎所有情况,我建议您使用abc模块,我之前建议的其他模块。

另外,当您创建一个新的(基础)类时,请将其设为子类object,如下所示:class MyBaseClass(object):。我不知道它是否具有那么重要性,但它有助于保持代码的样式一致性

答案 13 :(得分:-2)

只是快速添加@TimGilbert的老派答案......你可以让你的抽象基类的 init ()方法抛出一个异常,这会阻止它被实例化,不是吗? / p>

>>> class Abstract(object):
...     def __init__(self):
...         raise NotImplementedError("You can't instantiate this class!")
...
>>> a = Abstract()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __init__
NotImplementedError: You can't instantiate this class!