在一些Python的工程项目中,我们会看到函数参数中会有冒号,有的函数后面会跟着一个箭头,你可能会疑惑,这些都是什么东西?
其实函数参数中的冒号是参数的类型建议符
,告诉程序员希望传入的实参的类型。函数后面跟着的箭头是函数返回值的类型建议符
,用来说明该函数返回的值是什么类型
- 例1
#!/usr/bin/python3
# -*- coding:utf-8 -*-
def twoSum(num1: int, num2: int=100) -> int:
sum = num1 + num2
return sum
if __name__ == "__main__":
print(twoSum.__annotations__)
print(twoSum(1,2))
print(twoSum(1))
print(twoSum('I love ','Arsenal'))
#print(twoSum('Arsenal'))
- 例2
def bev_instance2points_with_offset_z_2(ids: np.ndarray, max_x=50, meter_per_pixal=(0.2, 0.2), offset_y=None, Z=None):
center = ids.shape[1] / 2
lines = mean_col_by_row_with_offset_z_2(ids, offset_y, Z)
points = []
# for i in range(1, ids.max()):
for y, x, z in lines: # cols, rows
# x, y = np.where(ids == 1)
x = np.array(x)[::-1]
y = np.array(y)[::-1]
z = np.array(z)[::-1]
x = max_x / meter_per_pixal[0] - x
y = y * meter_per_pixal[1]
y -= center * meter_per_pixal[1]
x = x * meter_per_pixal[0]
y *= -1.0 # Vector is from right to left
if len(x) < 2:
continue
spline = CubicSpline(x, y, extrapolate=False)
points.append((x, y, z, spline))
return points