# coding=utf-8
# 由于这个函数的逻辑是直接打印参数,并没有做任何别的逻辑
# 所以这个函数可以接受整数、浮点数、list、tuple、dict等等的数据类型
def print_param(param):
print(param)
# 为了保证函数的正常运行,有时需要对函数入参进行类型的校验
# Python提供isinstance()函数,可以判断参数类型
def my_abs(x):
if not isinstance(x, int) and not isinstance(x, float):
print('param type error')
return None
if x >= 0:
return x
else:
return -x
# Python函数参数
if __name__ == '__main__':
print_param(1)
print_param('3.1415')
print_param([1,2,3,4,5])
# 函数参数校验
print(my_abs('123'))
print(my_abs(-1))
print(my_abs(-1.2))
结果:
1
3.1415
[1, 2, 3, 4, 5]
param type error
None
1
1.2