1.assert
函數說明:
assert語句是一種插入調試斷點到程序的一種便捷的方式。
使用范例
assert 3 == 3
assert 1 == True
assert (4 == 4)
print('-----------')
assert (3 == 4)
'''
拋出AssertionError異常,后面程序不執行
'''
print('-----------')
輸出結果:
D:\Users\lenovo\Anaconda3\python.exe F:/機器學習/生物信息學/Code/NumPy.py
-----------
Traceback (most recent call last):
File "F:/機器學習/生物信息學/Code/NumPy.py", line 38, in <module>
assert (3 == 4)
AssertionError
可以看到只輸出一個-----------
,后面的由於assert (3 == 4)
拋出異常而不執行。
2.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