在 Python 中檢查型別的規範方法是什麼?
如果你想檢查物件 x 是否是特定型別的一個例項(而不是子型別),可以使用 type 獲取其型別,並使用 is 語句進行檢查。
示例
x = "Hello"
if type(x) is str:
print("x is an instance of str")輸出
將輸出
x is an instance of str
如果你想檢查 x 是否是 MyClass 的一個例項或 MyClass 的任何子類的例項,你可以使用 isinstance 方法呼叫。
示例
x = "Hello"
if isinstance(x, str):
print("x is an instance of str")輸出
將輸出
x is an instance of str
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP