溫馨提示×

如何使用python獲取類型來做類型檢查

小樊
121
2024-06-26 12:32:50
欄目: 編程語言

在Python中,可以使用type()函數來獲取一個對象的類型,然后可以使用if語句來進行類型檢查。下面是一個簡單的示例:

def check_type(obj):
    if type(obj) == int:
        print("This is an integer")
    elif type(obj) == str:
        print("This is a string")
    else:
        print("Unknown type")

# 測試
check_type(10)  # 輸出:This is an integer
check_type("hello")  # 輸出:This is a string
check_type(3.14)  # 輸出:Unknown type

需要注意的是,在Python中有許多種類型檢查的方法,建議根據具體的情況選擇最適合的方法。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女