2024-03-29 05:10:51 发布
网友
我想确定变量是否为整数,因此使用以下代码:
if isinstance(var, int): do_something()
但是当var = False时,执行do_something函数
var = False
do_something
当var = None时,isinstance()函数正常工作
var = None
isinstance()
Python将True视为1,将False视为0。现在,您可以在这里执行以下操作:
True
1
False
0
try: var = int(string(False)) except ValueError: print("Invalid Integer")
在Python3中,布尔定义为整数的子类
Python3
这意味着True等同于1,其中asFalse等同于0
您可以在here中找到更多详细信息。从该链接中得出的完全相同的解释是:
There are three distinct numeric types: integers, floating point numbers, and complex numbers. In addition, Booleans are a subtype of integers
因为bool是int的一个子类。 你可以在builtins.py中找到它
bool
int
builtins.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. """
所以{}也{}。 ^当x的类型是y类型的派生类时,{}是True
x
y
Python将
True
视为1
,将False
视为0
。现在,您可以在这里执行以下操作:在
Python3
中,布尔定义为整数的子类这意味着
True
等同于1
,其中asFalse
等同于0
您可以在here中找到更多详细信息。从该链接中得出的完全相同的解释是:
There are three distinct numeric types: integers, floating point numbers, and complex numbers. In addition, Booleans are a subtype of integers
因为
bool
是int
的一个子类。你可以在
builtins.py
中找到它所以{}也{}。}是
^当
x
的类型是y
类型的派生类时,{True
相关问题 更多 >
编程相关推荐