#判断方法
if not isinstance(number,参数类型):
raise TypeErroe('Bad operand type')
例子:
#-*- coding:utf-8 -*-
def my_abs(x):
if not isinstance(x,(int,float)): #进行参数类型检查,此处只允许参数类型为int和float
raise TypeError("Bad operand type.")
if x >= 0:
return x
else:
return -x
num = my_abs(-12)
print num
本文介绍了Python中使用 isinstance() 函数进行参数类型检查的方法,并通过实例演示了如何在函数中实现异常处理,确保传入的参数符合预期类型。
950

被折叠的 条评论
为什么被折叠?



