python的any函数
Python any() function is one of the built-in functions. It takes iterable as an argument and returns True if any of the element in the iterable is True. If iterable is empty, then it returns False.
Python any()函数是内置函数之一。 它以iterable作为参数,如果iterable中的任何元素为True,则返回True。 如果iterable为空,则返回False。
Python any() function is a utility method and shortcut to below function.
Python any()函数是一种实用程序方法,是以下函数的快捷方式。
def any(iterable):
for element in iterable:
if element:
return True
return False
Let’s look at some of the examples of python any() function.
让我们看一下python any()函数的一些示例。
# iterable has at least one True
list_bools = [True, True, True]
print(any(list_bools))
# iterable none of the elements are True
list_bools = [False, False]
print(any(list_bools))
Output:
输出:
True
False
# iterable is empty
list_bools = []
print(any(list_bools)) # False
# iterable elements are True string (at least one)
list_strs = ['True', 'false']
print(any(list_strs))
# iterable any elements is true string with different case
list_strs = ['fff', 'true']
print(any(list_strs))
# iterable any elements are not true string
list_strs = ['abc', 'def']
print(any(list_strs))
# iterable all elements are empty string
list_strs = ['', '']
print(any(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__
函数。
Let’s test above explanation with a custom class. We will create a custom Employee class and use its objects in the list and call any()
function on it.
让我们用自定义类测试以上解释。 我们将创建一个自定义的Employee类,并在列表中使用其对象,然后在其上调用any()
函数。
class Employee:
name = ""
def __init__(self, n):
self.name = n
list_objs = [Employee("Pankaj"), Employee("Lisa")]
print(any(list_objs))
list_objs = [Employee("A"), Employee("D")]
print(any(list_objs))
Output:
输出:
True
True
Since our Employee class doesn’t have __len__() and __bool__() function defined, it’s boolean value is True.
由于我们的Employee类没有定义__len __()和__bool __()函数,因此其布尔值是True。
Let’s go ahead and define __len__() function for the Employee class as below.
让我们继续为Employee类定义__len __()函数,如下所示。
def __len__(self):
print('len function called')
return len(self.name)
Now the output of earlier code snippets will be:
现在,早期代码片段的输出将是:
len function called
True
len function called
True
Notice that len()
function is getting called when any() is used with the list of Employee objects. Since the first object in the iterable returned True, other elements are not required to be evaluated for the boolean value.
注意,将any()与Employee对象的列表一起使用时,会调用len()
函数。 由于iterable中的第一个对象返回True,因此不需要评估其他元素的布尔值。
Now let’s define __bool__ function for the Employee class and see what happens with the above code.
现在让我们为Employee类定义__bool__函数,看看上面的代码会发生什么。
def __bool__(self):
print('bool function called')
if len(self.name) > 3:
return True
else:
return False
Output:
输出:
bool function called
True
bool function called
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.
从输出中很明显,如果定义了__bool__
函数,那么它将用于获取python对象的布尔值。
Notice that second list any() function output is False and boolean value is retrieved for all the objects in the list.
请注意,第二个列表的any()函数输出为False,并且为列表中的所有对象检索了布尔值。
That’s all for python any() function examples.
这就是python any()函数示例的全部内容。
Reference: Official Documentation
参考: 官方文档
翻译自: https://www.journaldev.com/22639/python-any-function
python的any函数