如何在 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__
方法。
有没有更好的方法来定义抽象类?
使用 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>
执行此操作的老式(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
文档要容易一些。
Abstract#foo
中实现一些常见行为。应该禁止直接调用它,但仍然可以使用 super()
调用它。
这是一个非常简单的方法,无需处理 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
这说明可以实例化继承自基类的子类,但不能直接实例化基类。
大多数以前的答案都是正确的,但这里是 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
AbstractClass
构造函数中需要调用 super().__init__()
?
如其他答案中所述,是的,您可以使用 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:无法使用抽象方法 do_stuff、from_dict、prop2 实例化抽象类 B
列出 Base
中定义但我们未在 B
中实现的所有内容。
@abstractclassmethod
。
这个将在 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 ABC
和class MyABC(ABC)
。
这也有效并且很简单:
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()
您还可以利用 __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 关键字。这就是你错过的一切。
我发现接受的答案和所有其他答案都很奇怪,因为它们将 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()
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
在这里回答迟了,但为了回答这里指出的另一个问题“如何制作抽象方法”,我提供以下内容。
# 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
...
在您的代码段中,您还可以通过在子类中为 __new__
方法提供实现来解决此问题,同样:
def G(F):
def __new__(cls):
# do something here
但这是一个 hack,我建议你不要这样做,除非你知道你在做什么。对于几乎所有情况,我都建议您使用 abc
模块,这是我之前的其他人所建议的。
此外,当您创建新(基)类时,将其设为 object
的子类,如下所示:class MyBaseClass(object):
。我不知道它是否有那么重要了,但它有助于保持代码风格的一致性
只是对@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!
不定期副业成功案例分享
@abstractmethod
使得装饰函数 必须 在类可以被实例化之前被覆盖。来自文档:A class that has a metaclass derived from ABCMeta cannot be instantiated unless all of its abstract methods and properties are overridden.
@abstractmethod
用于__init__
方法,请参阅 stackoverflow.com/q/44800659/547270