Python|方法重载
与其他语言(例如 C++ 中的方法重载)一样, Python默认不支持方法重载。但是在Python中实现方法重载有不同的方法。
Python中方法重载的问题是我们可以重载方法但只能使用最新定义的方法。
Python3
# First product method.
# Takes two argument and print their
# product
def product(a, b):
p = a * b
print(p)
# Second product method
# Takes three argument and print their
# product
def product(a, b, c):
p = a * b*c
print(p)
# Uncommenting the below line shows an error
# product(4, 5)
# This line will call the second product method
product(4, 5, 5)
Python3
# Function to take multiple arguments
def add(datatype, *args):
# if datatype is int
# initialize answer as 0
if datatype =='int':
answer = 0
# if datatype is str
# initialize answer as ''
if datatype =='str':
answer =''
# Traverse through the arguments
for x in args:
# This will do addition if the
# arguments are int. Or concatenation
# if the arguments are str
answer = answer + x
print(answer)
# Integer
add('int', 5, 6)
# String
add('str', 'Hi ', 'Geeks')
Python3
from multipledispatch import dispatch
#passing one parameter
@dispatch(int,int)
def product(first,second):
result = first*second
print(result);
#passing two parameters
@dispatch(int,int,int)
def product(first,second,third):
result = first * second * third
print(result);
#you can also pass data type of any value as per requirement
@dispatch(float,float,float)
def product(first,second,third):
result = first * second * third
print(result);
#calling product method with 2 arguments
product(2,3,2) #this will give output of 12
product(2.2,3.4,2.3) # this will give output of 17.985999999999997
输出:
100
在上面的代码中,我们定义了两个product方法,但是我们只能使用第二个product方法,因为Python不支持方法重载。我们可以定义许多同名不同参数的方法,但我们只能使用最新定义的方法。调用其他方法会产生错误。就像这里打电话将产生错误,因为最新定义的产品方法需要三个参数。
因此,为了克服上述问题,我们可以使用不同的方式来实现方法重载。
方法1(不是最有效的方法):
我们可以使用参数使相同的函数以不同的方式工作,即根据参数。
Python3
# Function to take multiple arguments
def add(datatype, *args):
# if datatype is int
# initialize answer as 0
if datatype =='int':
answer = 0
# if datatype is str
# initialize answer as ''
if datatype =='str':
answer =''
# Traverse through the arguments
for x in args:
# This will do addition if the
# arguments are int. Or concatenation
# if the arguments are str
answer = answer + x
print(answer)
# Integer
add('int', 5, 6)
# String
add('str', 'Hi ', 'Geeks')
输出:
11
Hi Geeks
上述代码的问题在于,使用多个 if/else 语句使代码更加复杂,并且不是实现方法重载的理想方式。
方法二(高效一):
通过使用多个调度装饰器
Multiple Dispatch Decorator 可以通过以下方式安装:
pip3 install multipledispatch
Python3
from multipledispatch import dispatch
#passing one parameter
@dispatch(int,int)
def product(first,second):
result = first*second
print(result);
#passing two parameters
@dispatch(int,int,int)
def product(first,second,third):
result = first * second * third
print(result);
#you can also pass data type of any value as per requirement
@dispatch(float,float,float)
def product(first,second,third):
result = first * second * third
print(result);
#calling product method with 2 arguments
product(2,3,2) #this will give output of 12
product(2.2,3.4,2.3) # this will give output of 17.985999999999997
输出:
12
17.985999999999997
在后端,Dispatcher 创建一个存储不同实现的对象,并在运行时选择适当的方法作为传递的参数的类型和数量。