python函数之bool([x])用法详解


本文摘自php中文网,作者巴扎黑,侵删。

bool([x])

英文说明:Convert a value to a Boolean, using the standard truth testing procedure. If x is false or omitted, this returns False; otherwise it returns True. bool is also a class, which is a subclass of int. Class bool cannot be subclassed further. Its only instances are False and True.

New in version 2.2.1.

Changed in version 2.3: If no argument is given, this function returns False.

中文说明:将x转换为Boolean类型,如果x缺省,返回False,bool也为int的子类;

参数x:任意对象或缺省;大家注意到:这里使用了[x],说明x参数是可有可无的,如果不给任何参数则会返回False。

版本:在python 2.2.1中引入的新函数,在python2.3以后如果不传参数则返回False。

注意:该函数在python3中也可正常使用

实例:

1

2

3

4

5

6

7

8

9

10

11

12

>>> bool(0)

False

>>> bool("abc")

True

>>> bool("")

False

>>> bool([])

False

>>> bool()

False

>>> issubclass(bool, int) #bool是一个subclass int

True

以上就是python函数之bool([x])用法详解的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python注释用什么符号

Python为什么要配置环境变量

如何输出到stderr

Python匿名函数定义及实例解析

Python if else用法是什么?

Python中add函数怎么用

Python里d是什么意思

Python强大之处在哪里

Python实现读取字符串按列分配后按行输出

Python实现购物系统实例介绍

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...