栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python Bool和int比较和带有布尔值的列表索引

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python Bool和int比较和带有布尔值的列表索引

发生的是布尔值实际上 整数。True为1,False为0。Bool是int的子类型。

>>> isinstance(True, int)True>>> issubclass(bool, int)True

因此,它不是将它们转换为整数,而只是将它们用作整数。

(由于历史原因,布尔值是整数。在Python中存在布尔类型之前,人们使用整数0表示false和1表示true。因此,在添加布尔类型时,他们将布尔值设为整数以保持向后兼容性包含使用这些整数值的旧代码。例如,请参见http://www.peterbe.com/plog/bool-
is-int
。)

>>> help(True)Help on bool object: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.


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/660573.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号