python all 函数

Python all() function is one of the built-in functions. It takes iterable as an argument and returns True if all elements of the iterable are true or it’s empty.

Python all()函数是内置函数之一。 它以iterable作为参数,如果iterable的所有元素均为true或为空,则返回True

Python all()函数 (Python all() function)

Python all() function is a utility method and shortcut to below function.

Python all()函数是一种实用程序方法,是以下函数的快捷方式。

def all(iterable):for element in iterable:if not element:return Falsereturn True

Let’s look at some of the examples of python all() function.

让我们看一下python all()函数的一些示例。

带有布尔值的Python all()示例 (Python all() example with boolean)

# iterable has all True
list_bools = [True, True, True]print(all(list_bools))# iterable all elements are not True
list_bools = [True, True, False]print(all(list_bools))

Output:

输出:

True
False

带有空可迭代的Python all() (Python all() with empty iterable)

# iterable is empty
list_bools = []print(all(list_bools))

Output:

输出:

True

Python all()与字符串列表 (Python all() with list of strings)

# iterable elements are True string
list_strs = ['True', 'True']print(all(list_strs))# iterable all elements are true string with different case
list_strs = ['True', 'true']print(all(list_strs))# iterable all elements are not true string
list_strs = ['abc', 'true']print(all(list_strs))# iterable all elements are empty string
list_strs = ['', 'true']print(all(list_strs))

Output:

输出:

True
True
True
False

When we want an object boolean value, python looks for __bool__ function in the object.

当我们想要一个对象布尔值时,python在对象中寻找__bool__函数。

If __bool__ function is not defined, then len() function is called if it’s defined. The object boolean value is considered as True if len() output is non-zero.

如果__bool__函数,则如果已定义len()函数,则将调用它。 如果len()输出为非零,则对象布尔值被视为True。

If a class defines neither __len__() nor __bool__() functions, all its instances are considered True.

如果一个类__len__()__bool__()函数,则其所有实例均被视为True。

__bool__ function to check object boolean value, if you are using python 2.x then you have to implement __bool__函数检查对象的布尔值,如果使用的是python 2.x,则必须实现__nonzero__ function.__nonzero__函数。

带有自定义对象的Python all() (Python all() with custom objects)

Let’s test above explanation with a custom class. We will create a custom Person class and use its objects in the list and call all() function on it.

让我们用自定义类测试以上解释。 我们将创建一个自定义Person类,并在列表中使用其对象,然后在其上调用all()函数。

class Person:name = ""def __init__(self, n):self.name = nlist_objs = [Person("Pankaj"), Person("Lisa")]
print(all(list_objs))list_objs = [Person("A"), Person("David")]
print(all(list_objs))

Output:

输出:

True
True

Since our object doesn’t have __len__() and __bool__() function defined, it’s boolean value is True.

由于我们的对象没有定义__len __()和__bool __()函数,因此其布尔值是True。

Let’s go ahead and define __len__() function for the Person class as below.

让我们继续为Person类定义__len __()函数,如下所示。

def __len__(self):print('len function called')return len(self.name)

Now the output of earlier code snippets will be:

现在,早期代码片段的输出将是:

len function called
len function called
True
len function called
len function called
True

Notice that len() function is getting called for each object when all() is used with the list of Person objects.

注意,当all()与Person对象列表一起使用时,将为每个对象调用len()函数。

Now let’s define __bool__ function for the Person class and see what happens with the above code.

现在让我们为Person类定义__bool__函数,看看上面的代码会发生什么。

def __bool__(self):print('bool function called')if len(self.name) > 3:return Trueelse:return False

Output:

输出:

bool function called
bool function called
True
bool function called
False

It’s clear from the output that if __bool__ function is defined, then it’s used for getting the python object boolean value. Notice that second list all() function output is False because ‘A’ length is less than 3.

从输出中很明显,如果定义了__bool__函数,那么它将用于获取python对象的布尔值。 请注意,第二个列表all()函数的输出为False,因为'A'的长度小于3。

That’s all for python all() function examples.

这就是python all()函数示例的全部内容。

GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/22635/python-all-function

python all 函数

python all 函数_Python all()函数相关推荐

  1. 在python中使用关键字define定义函数_python自定义函数def的应用详解

    这里是三岁,来和大家唠唠自定义函数,这一个神奇的东西,带大家白话玩转自定义函数 自定义函数,编程里面的精髓! def 自定义函数的必要函数:def 使用方法:def 函数名(参数1,参数2,参数-): ...

  2. python神秘的魔法函数_Python魔法函数

    1.什么是魔法函数 魔法函数即Python类中以__(双下划线)开头,以__(双下划线)结尾的函数,Python提供的函数,可让咱们随意定义类的特性 示例: class Company(object) ...

  3. python del函数_python del函数是什么以及如何使用?

    这是关于Python里比较难得一个函数,甚至于章节不多,但是讲的内容却很多很多,大家对部分内容不知道有没有过了解--面向对象,而在这里主要用到的函数就是del,大家如果不知道的话,可以跟随小编一起来看 ...

  4. python中模块和函数_Python中函数和模块的体验与使用

    函数基础 目标 函数的快速体验 函数的基本使用 函数的参数 函数的返回值 函数的嵌套调用 在模块中定义函数 01. 函数的快速体验 1.1 快速体验 所谓函数,就是把 具有独立功能的代码块 组织为一个 ...

  5. python反序数函数_python range()函数取反序遍历sequence的方法

    python range()函数取反序遍历sequence的方法 python中的range函数取反序有两种方式 第一种:先构建一个列表,然后对列表中的元素进行反转. 例如: a=range(5) f ...

  6. python用psf函数_Python 嵌套函数(高级用法)

    Python 嵌套函数(高级用法) 一.嵌套函数(高级用法) 1.嵌套函数 函数的嵌套调用是在"函数调用中再调用其他函数".也就是说:函数嵌套允许在一个函数中调用另外一个函数.如下 ...

  7. python asyncio回调函数_python回调函数用法实例分析

    python回调函数用法实例分析 本文实例讲述了python回调函数用法.分享给大家供大家参考.具体分析如下: 软件模块之间总是存在着一定的接口,从调用方式上,可以把他们分为三类:同步调用.回调和异步 ...

  8. python引用函数_python 调用函数

    Python内置了很多有用的函数,我们可以直接调用. 要调用一个函数,需要知道函数的名称和参数,比如求绝对值的函数abs,只有一个参数.可以直接从Python的官方网站查看文档: 也可以在交互式命令行 ...

  9. python中的get函数_python之函数用法get()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法get() #http://www.runoob.com/python/att-dic ...

  10. python reduce函数_Python reduce()函数的用法小结

    reduce()函数也是Python内置的一个高阶函数. reduce() 格式: reduce (func, seq[, init()]) reduce()函数即为化简函数,它的执行过程为:每一次迭 ...

最新文章

  1. DHCP常用配置文件解析
  2. Microsoft SharePoint Server 2016 部署文档(2)
  3. python获取当前文件夹下所有文件名
  4. html5 背景拼贴,AI创建漂亮的无缝拼贴图案背景样式
  5. Java——设计模式(工厂方法模式)
  6. ifconfig命令找不到_02. Linux命令之查看网络连接
  7. 最大子列和问题(JAVA)
  8. java 定义方法_java如何定义方法
  9. 基于element ui的收起展开检索条件效果
  10. ORACLE常用的一些特殊SQL,收藏收藏,下次需要的时候就不用再翻箱倒柜了
  11. 数据结构—二叉树,满二叉树、完全二叉树、二叉树的性质(思维导图)
  12. python基础知识——函数(下)
  13. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库...
  14. R语言 openair 做后向轨迹
  15. java使用itext导出pdf,图片、表格、背景图
  16. 解读:加性高斯白噪声信道
  17. 为什么要读“无用”的古文
  18. 拉了300M的网,下载速度为啥还是这么慢?计算机基础(五)之网络层完结
  19. 视频转换成gif动图如何操作?教你三步完成视频转gif
  20. 74HC245引脚定义 使用方法

热门文章

  1. 自适应共振理论网络 ART
  2. iOS新的旅程之Swift语言的学习
  3. Sqlserver2008 数据库镜像会话的初始连接
  4. Folder and jar
  5. 工作经验总结:百万数据引发的性能瓶颈问题
  6. [转载] OpenCV-Python 图像处理(二):图像的读取、显示与保存
  7. [转载] python--isalnum()函数
  8. verilog读入.txt的有符号十进制数,把有符号十进制数写入到.txt文件中
  9. 要继续看Python写算法的内容请到那里去
  10. JFinal Web开发学习(一)开启HelloWorld