我想確定變量是否為整數,因此我使用以下代碼:if isinstance(var, int):
do_something()但是當 執行時,函數被執行。var = Falsedo_something當 時,函數正常工作。var = Noneisinstance()
3 回答

慕村225694
TA貢獻1880條經驗 獲得超4個贊
因為 是 的子類。
您可以在以下位置找到它boolintbuiltins.py
class bool(int):
"""
bool(x) -> bool
Returns True when the argument x is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed.
"""
所以也.
是 當 的類型是 的派生類時。issubclass(bool, int)Trueisinstance(x, y)Truexy

HUH函數
TA貢獻1836條經驗 獲得超4個贊
Python 將 和 視為 ?,F在你可以在這里做的是:True1False0
try:
var = int(string(False))
except ValueError:
print("Invalid Integer")
添加回答
舉報
0/150
提交
取消