首页 > 编程语言 >Python - Abstract Base classes

Python - Abstract Base classes

时间:2024-07-31 09:30:17浏览次数:9  
标签:Base methods Python Abstract abstract base pass classes class

We have seen that if we have to define a group of classes that have similar features and show common behavior, we can define a base class and then inherit the classes from it. In the derived classes, we have the choice to either use the base class version of a method or override it. There can be scenarios when it does not make sense to implement some methods in the base class. We need to define a method in the base class just to provide a common interface for the derived classes. We do not need such a base class to be instantiated.

 

Most of the time, abstract methods have an empty body in the abstract class. They are there only for defining the common interface, so their body generally contains a pass statement. However, the abstract methods of an abstract class can contain some basic implementation that the concrete subclasses can call by using super. Even if the abstract method is implemented in the abstract base class, the subclass has to override it. The subclass can call the base implementation by using super and then add its own code for any additional tasks.

You can also declare property methods, class methods, or static methods as abstract:

@property
@abstractmethod
def name(self):
  pass

@classmethod
@abstractmethod
def method1(cls):
   pass

@staticmethod
@abstractmethod
def method2():
   pass

 

标签:Base,methods,Python,Abstract,abstract,base,pass,classes,class
From: https://www.cnblogs.com/zhangzhihui/p/18333843

相关文章

  • Datawhale AI夏令营(AI+物质科学)之跑通baseline-Task2新手教程
    前言DatawhaleAI的夏令营(线上的),这期夏令营是基于天池平台“第二届世界科学智能大赛物质科学赛道:催化反应预测”开展的从零入门AIforScience。上篇文章完成Task1教程后,继续给大家干Task2的教程,话不多说,开整!正文第一步:1.打开魔塔社区:链接如下:魔搭社区汇聚各领域最先进的......
  • python3 unittest+BeautifulReport单个进程输出多个测试报告
    最近一个项目中需要由于输出的案例内容非常多(上万条),导致BeautifulReport输出的报告内容非常大(几百兆)。浏览器无法正常处理这么大的测试报告,就算打开了,也不方便阅读和处理,因此需要将报告分成多个输出。经修改代码,发现单个进程内输出多个测试报告出现问题:第一个测试报告能正常数据......
  • 具有自引用的类装饰器的 Python 类型提示
    我的最终目标是编写一个系统来轻松记录函数调用(特别是类方法)。我首先编写一个带有包装方法的类Loggable,该方法允许我装饰子类方法并记录它们的调用|||现在我可以编写子类并记录它们的调用:Param=ParamSpec("Param")RetType=TypeVar("RetType")CountType=......
  • 如何在for循环中使用curve_fit函数在python中一次性创建多个回归?
    简而言之,我有两个矩阵,一个称为t,另一个称为y。每个都有7列。假设它们被称为a、b、c、d、e、f和g。我想要的是从a对a、b对b、...、g对g这两个矩阵进行回归。我已经设法使我的算法使用curve_fit对一列进行回归一次。但我真正希望的是它能够一次性完成7个回归......
  • 激活虚拟环境会让python消失?
    VisualStudioCode终端的屏幕截图如屏幕截图所示,python在Powershell中运行得很好。然后我在E:\DrewFTCAPI\ftcapivenv激活虚拟环境,然后python就消失了。不仅没有消失,它不运行任何东西,也不产生任何输出。我至少预计会出现某种类型的"python"i......
  • Python 3.6 中的相互递归类型,使用命名元组语法
    我正在尝试实现图的节点和边。这是我的代码:fromtypingimportNamedTuple,ListclassNode(NamedTuple):name:stredges:List[Edge]classEdge(NamedTuple):src:Nodedest:Node这会引发错误,因为创建Edge时未定义Node类型。......
  • 使用 keras 模型对函数进行 Python 类型提示
    如果我创建这样的函数:defmdl(input_shape):model=Sequential()model.add(Conv2D(depth=64,kernel_size=(3,3),input_shape=input_shape,activation='relu'))model.add(Dense(32),activation='relu')model.add(Dropout(0.3))m......
  • Python:自动完成可以用于列表中的元素吗?
    Python在函数参数和函数返回类型中具有类型提示。类的元素是否有类似的东西?我希望能够在如下示例中使用自动完成功能:classMyClass:defhello(self):print("Hello")mylist=[]mylist.append(MyClass())foriinmylist:i.hello()#Noautocomplete......
  • python 中 COM 对象的正确类型提示是什么?
    我在python中使用COM对象来向3rd方软件公开可编程接口。这是通过使用Dispatchfromwin32com.client来实现的。我的项目也一直在使用python.3.7中的类型提示,但是我不确定如何为了类型提示的目的定义这些COM对象的类型。这个问题涉及我拥有的所有COM......
  • 如何遍历Python字典同时避免KeyErrors?
    解析大型JSON时,某些键可能仅在某些情况下存在,例如出现错误时。从服务器的API获取200OK的情况并不少见,但是您得到的响应包含应检查的错误。处理此问题的最佳方法是什么?我知道使用类似||之类的东西。|是处理KeyError的一种方法。get()但是如果......