Python 简明教程
Python - Method Overloading
Method overloading 是面向对象编程的一个特性,其中一个类可以拥有多个方法,这些方法具有相同名称但不同的参数。要重载方法,我们必须更改参数数量或参数类型,或同时更改这两项。
Method overloading is a feature of object-oriented programming where a class can have multiple methods with the same name but different parameters. To overload method, we must change the number of parameters or the type of parameters, or both.
Method Overloading in Python
不同于其他编程语言如 Java、C++ 和 C#,Python 并不默认支持方法重载功能。但有替代方法可以实现它。
Unlike other programming languages like Java, C++, and C#, Python does not support the feature of method overloading by default. However, there are alternative ways to achieve it.
Example
如果你像下面的代码中所示那样定义多个方法,则后一个定义会覆盖前一个。因此,这种在 Python 中实现方法重载的方法会产生错误。
If you define a method multiple times as shown in the below code, the last definition will override the previous ones. Therefore, this way of achieving method overloading in Python generates error.
class example:
def add(self, a, b):
x = a+b
return x
def add(self, a, b, c):
x = a+b+c
return x
obj = example()
print (obj.add(10,20,30))
print (obj.add(10,20))
在使用三个参数首次调用 add() 方法时成功。但是,在使用两个参数调用 add() 方法(如在类中定义的那样)时失败。
The first call to add() method with three arguments is successful. However, calling add() method with two arguments as defined in the class fails.
60
Traceback (most recent call last):
File "C:\Users\user\example.py", line 12, in <module>
print (obj.add(10,20))
^^^^^^^^^^^^^^
TypeError: example.add() missing 1 required positional argument: 'c'
输出会告诉你 Python 仅考虑对 add() 方法的最新定义,而丢弃较早的定义。
The output tells you that Python considers only the latest definition of add() method, discarding the earlier definitions.
要模拟方法重载,我们可以使用变通方法,即将方法参数的默认值定义为 None,这样便可以使用一个、两个或三个参数。
To simulate method overloading, we can use a workaround by defining default value to method arguments as None, so that it can be used with one, two or three arguments.
Example
下面的示例展示了如何实现 Python 中的方法重载 −
The below example shows how to achieve method overloading in Python −
class example:
def add(self, a = None, b = None, c = None):
x=0
if a !=None and b != None and c != None:
x = a+b+c
elif a !=None and b != None and c == None:
x = a+b
return x
obj = example()
print (obj.add(10,20,30))
print (obj.add(10,20))
它将生成以下 output −
It will produce the following output −
60
30
通过这种方法,我们能够在 Python 类中加入方法重载。
With this workaround, we are able to incorporate method overloading in Python class.
Implement Method Overloading Using MultipleDispatch
Python 的标准函数库没有其他用于实现方法重载的规定。但是,我们可以从一个名为 MultipleDispatch 的第三方模块中使用一个分派函数。
Python’s standard library doesn’t have any other provision for implementing method overloading. However, we can use a dispatch function from a third-party module named MultipleDispatch for this purpose.
首先,你需要使用以下命令安装 Multipledispatch 模块 −
First, you need to install the Multipledispatch module using the following command −
pip install multipledispatch
此模块有一个 @dispatch 装饰器。它接受要重载至的方法需要传递的参数数量。使用 @dispatch 装饰器定义多个 add() 方法副本,如下所示 −
This module has a @dispatch decorator. It takes the number of arguments to be passed to the method to be overloaded. Define multiple copies of add() method with @dispatch decorator as below −
Example
在本示例中,我们使用 multipledispatch 在 Python 中重载一个方法。
In this example, we are using multipledispatch to overload a method in Python.
from multipledispatch import dispatch
class example:
@dispatch(int, int)
def add(self, a, b):
x = a+b
return x
@dispatch(int, int, int)
def add(self, a, b, c):
x = a+b+c
return x
obj = example()
print (obj.add(10,20,30))
print (obj.add(10,20))