问题:是否可以在Python中创建抽象类?
如何在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__方法。
有没有更好的方法来定义抽象类?
回答 0
使用该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
老式的方法(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
这是一种非常简单的方法,而无需处理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
这表明您可以实例化从基类继承的子类,但不能直接实例化基类。
回答 3
先前的大多数答案都是正确的,但这是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
回答 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
回答 5
如其他答案所述,是的,您可以使用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我们的Base类将始终具有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')所有必需的方法/属性均已实现,我们当然可以添加不属于Base(here :)的其他功能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:无法使用抽象方法do_stuff,from_dict,prop2实例化抽象类B
列出Base我们未在其中实现的所有定义的事物B。
回答 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
您还可以利用__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
我发现了可接受的答案,所有其他答案都很奇怪,因为它们传递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()回答 9
 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回答 10
在您的代码段中,您还可以通过为__new__子类中的方法提供一个实现来解决此问题,类似地:
def G(F):
    def __new__(cls):
        # do something here但这是一个hack,除非您知道自己在做什么,否则我建议您不要这样做。对于几乎所有情况,我都建议您使用该abc模块,而我之前的其他人都建议使用该模块。
同样,当您创建一个新的(基)类时,使其成为子类object,如下所示:class MyBaseClass(object):。我不知道它是否还有那么大的意义,但它有助于保持代码的样式一致性
回答 11
只是@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! 
