是否可以在 Python 中创建抽象类?
- 2025-02-28 08:24:00
- admin 原创
- 66
问题描述:
如何在 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__
方法。
有没有更好的方法来定义抽象类?
解决方案 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>
解决方案 2:
实现此目的的传统方法(PEP 3119raise 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:
这是一个非常简单的方法,无需处理 ABC 模块。
在__init__
要成为抽象类的类的方法中,您可以检查自身的“类型”。如果自身的类型是基类,则调用者正在尝试实例化基类,因此会引发异常。这是一个简单的例子:
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
这表明,可以实例化从基类继承的子类,但不能直接实例化基类。
解决方案 4:
之前的大多数答案都是正确的,但这里是Python 3.7的答案和示例。是的,您可以创建一个抽象类和方法。提醒一下,有时一个类应该定义一个逻辑上属于另一个类的方法,但该类不能指定如何实现该方法。例如,在下面的父母和婴儿类中,他们都吃东西,但每个类的实现会有所不同,因为婴儿和父母吃的食物种类不同,而且他们吃的次数也不同。因此,吃方法子类会覆盖 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:
正如其他答案所解释的那样,是的,您可以使用 module 在 Python 中使用抽象类abc
。下面我给出了一个使用 abstract 和(使用 Python 3.6+)的实际示例@classmethod
。@property
对@abstractmethod
我来说,通常从可以轻松复制粘贴的示例开始更容易;我希望这个答案对其他人也有用。
让我们首先创建一个名为的基类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
和 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
(此处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:
这个将在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
解决方案 7:
您还可以利用 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:
这也有效并且很简单:
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()
解决方案 9:
我发现可接受的答案和所有其他答案都很奇怪,因为它们传递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:
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:
您可以通过扩展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:无法使用抽象方法实例化抽象类 Animal 声音
并且,如果您没有在子类中重写抽象类的抽象方法并且您实例化子类,如下所示:
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!!
此外,你可以替换下面的扩展Cat
类:Animal
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))
然后,上述两个代码都会输出相同的结果,表明Cat
class是Animal
class的子类:
True
解决方案 12:
在这里回答有点晚了,但是为了回答这里指出的另一个问题“如何制作抽象方法”,我提出以下内容。
# 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 异常引发。哦,是的,为了方便起见,您还可以使用此方法获取异常中抽象的方法名称。
解决方案 13:
__new__
在您的代码片段中,您还可以通过在子类中提供该方法的实现来解决这个问题,同样:
def G(F):
def __new__(cls):
# do something here
但这是一种黑客行为,我建议你不要这样做,除非你知道自己在做什么。对于几乎所有情况,我建议你使用abc
在我之前其他人建议的模块。
此外,当你创建一个新的(基)类时,请将其设为子类object
,如下所示:class MyBaseClass(object):
。我不知道这是否还有那么重要,但它有助于保持代码的样式一致性
扫码咨询,免费领取项目管理大礼包!