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

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

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


3
是的,你可以使用 Python 的 abc(抽象基类)模块创建抽象类。这个网站会帮助你了解它:http://docs.python.org/2/library/abc.html。 - ORION
13个回答

752
使用abc模块创建抽象类。使用abstractmethod装饰器声明一个方法为抽象方法,并根据你的Python版本使用三种不同的方式之一来声明一个抽象类。
在Python 3.4及以上版本中,可以继承ABC。在早期的Python版本中,则需要将你的类的元类指定为ABCMeta。在Python 2和Python 3中,指定元类的语法不同。以下是三种可能性:
# 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>

19
@abstractmethod是什么?为什么需要它?如果类已经被定义为抽象类,编译器/解释器不应该知道所有的方法都来自于这个抽象类吗? - Charlie Parker
52
@CharlieParker - 使用@abstractmethod装饰的函数必须在类被实例化之前被重写。根据文档:如果一个类的元类派生自 ABCMeta,则该类不能被实例化,除非它的所有抽象方法和属性都被重写。 - Fake Name
7
基本上,它允许你以一种方式定义一个类,其中子类必须实现一组指定的方法才能被实例化。 - Fake Name
20
有没有方法可以禁止用户创建没有任何@abstractmethod方法的Abstract()类? - Joe
5
为什么我不能只使用@abstractmethod而不使用ABC?这感觉不符合Python的风格。当我写abstractmethod时,它意味着这已经是一个抽象基类了。或者我有什么遗漏吗? - hans
显示剩余8条评论

152

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文档要容易一些。

3
感受到这种方法的简单易行和高效性。 - mohit6up
1
哈哈,我在我的OMSCS课程中到处都看到这个,但不知道它是什么 :) - mLstudent33
1
但这并不是真正有帮助的。因为您可能想要在Abstract#foo中实现一些常见的行为。直接调用它应该被禁止,但仍然可以通过super()调用它。 - Eric Duminil
一个不错的小型轻量级替代方案,适用于那些对它不感到威胁的使用情况。 - NeilG
一个不错的小型轻量级替代方案,适用于那些不感到受到威胁的使用情况。 - undefined

29

这里有一种非常简单的方法,无需使用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
这表明您可以实例化一个继承自基类的子类,但不能直接实例化基类。

这表明您可以创建一个继承自基类的子类的实例,但不能直接创建基类的实例。


另一个有趣的选择。 - NeilG

24

大多数之前的答案都是正确的,但这里是Python 3.7的答案和示例。是的,你可以创建一个抽象类和方法。只要提醒一下,有时候一个类应该定义一个方法,这个方法在逻辑上属于这个类,但是这个类不能指定如何实现这个方法。例如,在下面的Parents和Babies类中,它们都吃东西,但是实现方式对每个类来说都不同,因为婴儿和父母吃的食物不同,并且吃饭的次数也不同。因此,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

我们可以在__init__函数上使用@abstractmethod吗? - variable
1
为什么要在 @abstractmethod def eat(self) 中加上 self 参数?如果这个类是抽象的,因此不应该被实例化,为什么还需要在 eat 方法中传递 self 参数呢?即使没有 self 参数,它也可以正常工作。 - VMMF
3
为什么在您的“AbstractClass”构造函数中需要调用super().__init__() - Shlomi A

17

如其他回答所述,是的,您可以使用Python中的抽象类,使用abc模块。下面我将使用抽象@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类方法,一个只读的prop1属性,一个可读可写的prop2属性以及一个名为do_stuff的函数。现在基于Base构建的任何类都必须实现这四个方法/属性。请注意,为了使方法成为抽象方法,需要使用两个装饰器-classmethodabstractproperty

现在我们可以创建一个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

将返回

属性错误: 无法设置属性

同时我们的 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')

我们将会遇到一个错误

类型错误:无法用抽象方法 do_stuff,from_dict,prop2 实例化抽象类 B,来自于 Base

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


请注意,有一个@abstractmethod注释,您可以使用它来代替两个单独的注释。 - FishingIsLife
1
@FishingIsLife:根据文档,如果我理解正确的话,@abstractclassmethod从3.3版本开始已被弃用。 - Cleb
1
你是对的。谢谢你的提示。 - FishingIsLife

9

这个需要在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

1
目前我们只能使用 from abc import ABCclass MyABC(ABC) - user26742873

3

同时这个方法也很简单:

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

3
你也可以利用 __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 关键字。这就是您所错过的全部内容。

3
我发现被接受的答案和其他答案都很奇怪,因为它们将 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()

2
即使是 abc 文档在其示例中也使用了 self 链接 - igor Smirnov

3
您可以通过扩展名为“Abstract Base Classes”的ABC来创建抽象类,并在抽象类中使用@abstractmethod创建抽象方法,如下所示:
from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

要使用抽象类,需要通过子类继承它,并且子类需要覆盖抽象类中的抽象方法,如下所示:

from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

class Cat(Animal): # Extends "Animal" abstract class
    def sound(self): # Overrides "sound()" abstract method
        print("Meow!!")

obj = Cat()
obj.sound()

输出:

Meow!!

抽象方法可以包含代码,而不仅仅是pass语句,子类可以像下面展示的那样调用它:

from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        print("Wow!!") # Here

class Cat(Animal):
    def sound(self):
        super().sound() # Here
        
obj = Cat()
obj.sound()

输出:

Wow!!

抽象类可以拥有变量和非抽象方法,这些可以被子类调用,而且非抽象方法不需要被子类重写,如下所示:

from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass
    
    def __init__(self): # Here
        self.name = "John" # Here
    
    x = "Hello" # Here
    
    def test1(self): # Here
        print("Test1")
    
    @classmethod # Here
    def test2(cls):
        print("Test2")
        
    @staticmethod # Here
    def test3():
        print("Test3")

class Cat(Animal):
    def sound(self):
        print(self.name) # Here
        print(super().x) # Here
        super().test1()  # Here
        super().test2()  # Here
        super().test3()  # Here

obj = Cat()
obj.sound()

输出:

John
Hello
Test1
Test2
Test3

同时,你可以定义一个抽象类和静态方法,以及在抽象类中定义抽象的getter、setter和deleter,如下所示。必须将@abstractmethod放在最内层装饰器位置,否则会导致错误,你可以参考我的回答,其中更详细地解释了抽象的getter、setter和deleter:

from abc import ABC, abstractmethod

class Person(ABC):

    @classmethod
    @abstractmethod # The innermost decorator
    def test1(cls):
        pass
    
    @staticmethod
    @abstractmethod # The innermost decorator
    def test2():
        pass

    @property
    @abstractmethod # The innermost decorator
    def name(self):
        pass

    @name.setter
    @abstractmethod # The innermost decorator
    def name(self, name):
        pass

    @name.deleter
    @abstractmethod # The innermost decorator
    def name(self):
        pass

然后,您需要在子类中覆盖它们,如下所示:
class Student(Person):
    
    def __init__(self, name):
        self._name = name
    
    @classmethod
    def test1(cls): # Overrides abstract class method
        print("Test1")
    
    @staticmethod
    def test2(): # Overrides abstract static method
        print("Test2")
    
    @property
    def name(self): # Overrides abstract getter
        return self._name
    
    @name.setter
    def name(self, name): # Overrides abstract setter
        self._name = name
    
    @name.deleter
    def name(self): # Overrides abstract deleter
        del self._name

然后,您可以实例化子类并像下面所示调用它们:
obj = Student("John") # Instantiates "Student" class
obj.test1() # Class method
obj.test2() # Static method
print(obj.name) # Getter
obj.name = "Tom" # Setter
print(obj.name) # Getter
del obj.name # Deleter
print(hasattr(obj, "name"))

输出:

Test1
Test2
John 
Tom  
False

如果您尝试像下面这样实例化一个抽象类:

from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

obj = Animal()

以下错误会发生:
TypeError: Can't instantiate abstract class Animal with abstract methods sound
如果您不在子类中重写抽象类的抽象方法,并像下面所示实例化子类,则会出现上述错误:
from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

class Cat(Animal):
    pass # Doesn't override "sound()" abstract method

obj = Cat() # Here

出现以下错误:

TypeError: 无法实例化具有声音抽象方法的抽象类 Cat

如果在不扩展 ABC 的非抽象类中定义了一个抽象方法,则该抽象方法是普通实例方法,因此即使实例化非抽象类并且子类未覆盖非抽象类的抽象方法,也不会出现错误,如下所示:

from abc import ABC, abstractmethod

class Animal: # Doesn't extend "ABC"
    @abstractmethod # Here
    def sound(self):
        print("Wow!!")

class Cat(Animal):
    pass # Doesn't override "sound()" abstract method

obj1 = Animal() # Here
obj1.sound()

obj2 = Cat() # Here
obj2.sound()

输出:

Wow!!
Wow!!

此外,您可以用下面的方法替换继承AnimalCat
from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

# ↓↓↓ Here ↓↓↓

class Cat(Animal):
    def sound(self):
        print("Meow!!")

# ↑↑↑ Here ↑↑↑

print(issubclass(Cat, Animal))

以下是具有register()的代码:

from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

# ↓↓↓ Here ↓↓↓

class Cat:
    def sound(self):
        print("Meow!!")
        
Animal.register(Cat)

# ↑↑↑ Here ↑↑↑

print(issubclass(Cat, Animal))

那么,上面两段代码的输出结果都相同,显示CatAnimal的子类:

True

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接