python all 函数_Python all()函数

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 False
    return 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 = n

list_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 True
        else:
            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 函数

  • 10
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值