有没有办法将一对元组定义为函数的参数?我坚持这个 "tuple parameter unpacking is not supported in Python 3"
Is there a way to define a pair of tuple as argument of a function ? I'm stucked with this "tuple parameter unpacking is not supported in Python 3"
我的代码应该return 基于这 2 个元组的欧氏距离:
def distance_points((x1, y1), (x2, y2)):
dist = ((x1 - x2)**2 + (y1 - y2)**2)** 0.5
return dist
print(distance_points((1.0, 1.0), (2.0, 1.0)))
您可以重写您的函数并在函数体中进行元组解包 (as stated in official PEP 3113 -- Removal of Tuple Parameter Unpacking)。例如:
def distance_points(p1, p2):
(x1, y1), (x2, y2) = p1, p2
dist = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
return dist
print(distance_points((1.0, 1.0), (2.0, 1.0)))
我的代码应该return 基于这 2 个元组的欧氏距离:
def distance_points((x1, y1), (x2, y2)):
dist = ((x1 - x2)**2 + (y1 - y2)**2)** 0.5
return dist
print(distance_points((1.0, 1.0), (2.0, 1.0)))
您可以重写您的函数并在函数体中进行元组解包 (as stated in official PEP 3113 -- Removal of Tuple Parameter Unpacking)。例如:
def distance_points(p1, p2):
(x1, y1), (x2, y2) = p1, p2
dist = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
return dist
print(distance_points((1.0, 1.0), (2.0, 1.0)))