python中assert和isinstance的用法
assert语句是一种插入调试断点到程序的一种便捷的方式。
assert 3 == 3 assert 1 == True assert (4 == 4) print(‘-----------‘) assert (3 == 4) ‘‘‘ 抛出AssertionError异常,后面程序不执行 ‘‘‘ print(‘-----------‘)
isinstance函数说明:
当我们定义一个class的时候,我们实际上就定义了一种数据类型。我们定义的数据类型和Python自带的数据类型,比如str、list、dict没什么两样:
判断一个变量是否是某个类型可以用isinstance()判断:
class Student(): def __init__(self, name, score): self.name = name self.score = score a = ‘10‘ b = 3 c = [1, 2, 3] d = (1, 2, 3) f = Student(‘Eden‘, 99.9)
print(isinstance(a, str)) # True
print(isinstance(b, int)) # True
print(isinstance(c, list)) # True
print(isinstance(d, tuple)) # True
print(isinstance(f, Student)) # True
相关推荐
拉斯厄尔高福 2020-10-19
83540690 2020-08-16
lustdevil 2020-08-03
doupoo 2020-07-28
yuzhongdelei0 2020-07-04
lustdevil 2020-06-25
lustdevil 2020-06-21
上班打发 2020-06-14
xcguoyu 2020-05-15
fanhuasijin 2020-02-17
Julywhj 2020-02-02
xiaobater 2020-02-02
柠檬班 2020-01-30
KilluaZoldyck 2020-01-29
JackYin 2020-01-24
86427019 2020-01-20
taoqilin 2020-01-18
八角塘塘主 2020-01-04
jszy 2020-01-07