首页 > 编程语言 >Python - Overloading

Python - Overloading

时间:2024-06-17 21:59:14浏览次数:24  
标签:parameters overloading Python dispatch Overloading int operator method

Object Oriented Programming (OOP) allows the programmers to add some additional functionalities to the operators and methods which have basic properties. Such a kind of redefining of the entities of the programming structure is called as polymorphism. In earlier chapters, the readers could notice that the addition “+” operator is used for the concatenation of strings inside the print function. This is a kind of polymorphism that is introduced in earlier chapters. Overloading is a type of polymorphism that adds additional functionalities for the existing properties of objects (methods and attributes) or the object itself. Overloading exhibits code reusability and code readability. Overloading is of two types: method overloading (or function overloading) and operator overloading.

 

Method Overloading

Method overloading is adding additional functionalities to a method of a class by altering the parameters of the methods. In general, method overloading is implemented by defining multiple methods of the same name with

  1. Varying data types of the parameters.
  2. A varying number of parameters.

For implementing the method overloading with variable datatypes, the dispatch decorator is used to select different method definition implementations of the same method based on the specified datatypes. The "dispatch" decorator (@dispatch) from the multi-dispatch module is one of the famous dispatch decorators used for method overloading. The module "multi dispatch" is not the default available module in the Python environment, and it should be installed by executing "pip install multi dispatch" in the terminal or command prompt.

The syntax for implementing the dispatch operator is shown below.

Before the method definition, @dispatch decorator is used in the above syntax with parameters consisting of datatypes. To specify the data types of the parameters, data of the parameters are given as a parameter to the dispatch decorator. For example,

"@dispatch(int, char, int)

def methodName(i , j, k):

method definition"

The dispatch decorator @dispatch(int, char, int) denotes the parameters of the following method: i, j, and k are int, char, and int data types, respectively. Now, the method would accept only the arguments of datatypes int, char, and int, respectively. So, defining two methods with the same name and changing the parameters of the dispatch operator implements the method overloading by varying datatypes of parameters.

 

 

Operator Overloading

Operator overloading is an essential process in OOP, which adds multiple functions to the available operators. Operator overloading is the process of extending the predefined function of an operator to user defined functions. For example, the operator + is used to add two integers, join two strings, and merge two lists. The operator '+' is used for multiple purposes and is thus called operator overloading.

 

Binary Arithmetic Operators

 

Comparison Operator

 

标签:parameters,overloading,Python,dispatch,Overloading,int,operator,method
From: https://www.cnblogs.com/zhangzhihui/p/18253305

相关文章

  • Python函数式编程
    函数式编程基础函数式编程与面向对象编程一样都是一种编程范式,函数式编程也称为面向函数的编程。Python提供了高阶函数、函数类型和lambda表达式,他们是实现函数式编程的基础。高阶函数与函数类型如果一个函数可以作为其他函数的参数或者其他函数的返回值,那么这个函数就是“......
  • 使用python脚本玩转古早TCAD软件(待更新)
    前言TCAD(TechnologyComputerAidedDesign),虽然原名中没有与半导体器件有关的词汇,但这种软件便是半导体工艺模拟及器件模拟的工具,可以说是EDA软件的一种。TCAD软件同其他EDA软件一样,底层需要复杂的数学模型和数物模型支撑,能大幅减少半导体制造的研发成本,为新型半导体器件提供初......
  • Python - pandas 利用 某一列的值过滤数据
    #FA存在3D不存在建模的代码(1).txtEDLG-S1-M3-L12有一个excel:需求:利用txt中的代码去匹配execl中的调整后的规格型号,将匹配的数据保留,生成新的excelimportpandaswithopen('FA存在3D不存在建模的代码(1).txt','r')asf:txt_codes={item.replace('\n','')......
  • [AIGC] Python内置函数:刷题必备
    在Python编程和刷题过程中,我们经常会使用到一些内置函数来提升我们的效率。这些内置函数功能强大,使用恰当会大大简化我们的代码。接下来,让我们来看看其中的一些特别常用的函数:max(),sum(),min()和sorted()。max()max()函数用于返回给定参数的最大值,参数可以是序列。numb......
  • [AIGC] 详细了解Python中的sorted()函数
    Python语言为我们提供了许多内置函数,以方便和增强我们在编程过程中的效率和便捷性。其中,sorted()是非常重要且常用的一个函数,它用于对序列进行排序,并返回一个排序后的列表。一、函数简介sorted()函数主要用于对序列进行排序,创建一个这个序列的已排序列表。这个函数的基本......
  • 超详细Python教程——作用域
    学习过Java的同学都知道,Java的类里面可以给方法和属性定义公共的(public)或者是私有的(private),这样做主要是为了我们希望有些函数和属性能给别人使用或者只能内部使用。通过学习Python中的模块,其实和Java中的类相似,那么我们怎么实现在一个模块中,有的函数和变量给别......
  • 超详细Python教程——Magic Method
    在Python中,所有以"__"双下划线包起来的方法,都统称为"魔术方法"。比如我们接触最多的 __init__ 。魔术方法有什么作用呢?使用这些魔术方法,我们可以构造出优美的代码,将复杂的逻辑封装成简单的方法。那么一个类中有哪些魔术方法呢?我们可以使用Python内置的方法 dir()......
  • Web框架,Python框架初识,Django框架初识与安装,
    ⅠWeb框架【一】Web框架本质web框架本质上可以看成是一个功能强大的socket服务端,用户的浏览器可以看成是拥有可视化界面的socket客户端。两者通过网络请求实现数据交互,从架构层面上先简单的将Web框架看做是对前端、数据库的全方位整合#TCP服务端与客户端进行交互的过程#......
  • python字符串的一些操作实例
    已知字符串a=“aAsomr3idd4HGHbigs7Dlsf9YeAF”,要求如下1.请将a字符串的大写改为小写,小写改为大写。2.将a字符串的数字取出,并输出成一个新的字符串。3.将a字符串中的内容反向输出4.打印a字符串中所有奇数位上的字符(下标是1,3,5,7…位上的字符)5.将a字符串的所有偶数位上......
  • python爬取数据爬取图书信息
    #encoding=utf-8importjson#json包,用于读取解析,生成json格式的文件内容importtimefromrandomimportrandintimportrequests#请求包用于发起网络请求frombs4importBeautifulSoup#解析页面内容帮助包fromlxmlimportetreeimportre#正则表达式......