求指点
Topic sourceisinstance(object,class)是用来确定对象是否是类的实例。在上述的例子里,x应该是我们对象,然后(int,float)应当是我们的实例。(不确定这样理解是否准确)
为了引发异常,可以使用一个类或者实例参数调用raise语句。他的作用是输入的对象不符合先前定义的类时,会给出一个报错。报错的内容就由我们来定义。比如 def my_abs(x): if not isinstance(x,(int,float)): raise TypeError('what the hells value are you entering here')
因此,当利用函数def my_abs(x),输入的x不是所定义的int和float时,那么会显示错误。 TypeError('what the hells value are you entering here')
不敢十分确定,有错误之处,望更正。
- 1
不杀猪的樵夫
isinstance
raise
这些是什么意思啊?