【问题标题】:Is it possible to make abstract classes in Python?是否可以在 Python 中创建抽象类?
【发布时间】:2012-11-18 17:37:37
【问题描述】:

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

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

【问题讨论】:

标签: python class inheritance abstract-class abstract


【解决方案1】:

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

【讨论】:

  • @abstractmethod 有什么作用?你为什么需要它?如果该类已经被建立为抽象类,编译器/解释器不应该知道所有方法都来自相关抽象类吗?
  • @CharlieParker - @abstractmethod 使得装饰函数必须在类可以被实例化之前被覆盖。来自文档:A class that has a metaclass derived from ABCMeta cannot be instantiated unless all of its abstract methods and properties are overridden.
  • @CharlieParker - 基本上,它允许您以子类必须实现一组指定的方法来实例化的方式定义一个类。
  • 有没有办法禁止用户在没有任何@abstractmethod 方法的情况下创建Abstract()?
  • @Joe 看来您也可以将@abstractmethod 用于__init__ 方法,请参阅stackoverflow.com/q/44800659/547270
【解决方案2】:

在您的代码 sn-p 中,您也可以通过在子类中提供 __new__ 方法的实现来解决此问题,同样:

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

但这是一种 hack,我建议您不要这样做,除非您知道自己在做什么。对于几乎所有情况,我建议您使用 abc 模块,这是我之前的其他人所建议的。

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

【讨论】:

    【解决方案3】:

    执行此操作的老派(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 文档要容易一些。

    【讨论】:

    • 欣赏这种方法的简单性和有效性。
    • 哈哈,我在我的 OMCS 课程中随处看到这个,但不知道它是什么 :)
    • 不过,它并没有什么帮助。因为你可能想在Abstract#foo 中实现一些常见的行为。应该禁止直接调用,但应该还是可以用super()调用的。
    【解决方案4】:

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

    【讨论】:

    • 目前我们只能from abc import ABCclass MyABC(ABC)
    【解决方案5】:

    只是对@TimGilbert 的老派答案的快速补充……您可以让抽象基类的 init() 方法抛出异常,这会阻止它被实例化,不是吗?

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

    【讨论】:

    • 这将阻止子类受益于逻辑上应该存在于它们的公共基类中的任何公共 init 代码。
    • 很公平。对老派来说就这么多了。
    【解决方案6】:

    这也很有效并且很简单:

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

      这是一个非常简单的方法,无需处理 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()
      

      运行时会产生:

      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
      

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

      【讨论】:

        【解决方案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())
        

        输出:

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

        【讨论】:

        • 也许import abc 没用。
        • 我们也可以在 init 函数上写@abstractmethod 吗?
        • +1 为什么@abstractmethod def eat(self)?如果这个类是抽象的,因此不打算被实例化,你为什么要通过 (self) 来吃?没有它也能正常工作
        • 为什么你的AbstractClass构造函数需要调用super().__init__()
        【解决方案9】:

        您还可以利用 __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 关键字。这就是你错过的一切。

        【讨论】:

          【解决方案10】:

          正如其他答案中所解释的,是的,您可以使用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 classmethod、一个property 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 中实现的东西。

          【讨论】:

            【解决方案11】:

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

            【讨论】:

            • 甚至 abc 文档在他们的示例中使用 self link
            【解决方案12】:
             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
            

            【讨论】:

            • 很好@Shivam Bharadwaj,我也是这样做的
            【解决方案13】:

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

            # decorators.py
            def abstract(f):
                def _decorator(*_):
                    raise NotImplementedError(f"Method '{f.__name__}' is abstract")
                return _decorator
            
            
            # yourclass.py
            class Vehicle:
                def add_energy():
                   print("Energy added!")
            
                @abstract
                def get_make(): ...
            
                @abstract
                def get_model(): ...
            

            类基类 Vehicle 仍然可以实例化以进行单元测试(与 ABC 不同),并且存在 Pythonic 引发的异常。哦,是的,为方便起见,您还可以使用此方法获取异常中抽象的方法名称。

            【讨论】:

            • 我喜欢这个,但有一个疑问:抽象方法必须在具体子类中实现的约定不是吗?您的解决方案的(令人钦佩的)简单性(测试的可能性是其优点的一个重要方面)意味着我可以问这个问题:我(目前)还不清楚abc 模块解决方案是否强制实施具体子类中指定的抽象方法。 NB 在 2021 年 addGas 应该类似于 add_energy...
            猜你喜欢
            • 1970-01-01
            • 2016-09-25
            • 1970-01-01
            • 2010-10-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多