bool型数组python_Pythonbool()
bool型数组python
Python bool() function returns Boolean value for an object. The bool class has only two instances – True and False. This class can’t be extended.
Python bool()函数返回对象的布尔值。 bool类只有两个实例– True和False。 此类⽆法扩展。
Python bool() (Python bool())
Python bool() function uses standard truth testing rules to convert the specified argument object to Boolean value.
Python bool()函数使⽤标准的真值测试规则将指定的参数对象转换为布尔值。
Some of the rules used to return Boolean value are:
⽤于返回布尔值的⼀些规则是:
Any object Boolean value is considered true if it’s not implementing __bool__() function and __len__() f
unctions.
如果未实现__bool __()函数和__len __()函数,则任何对象布尔值都被视为true。
If the object doesn’t define __bool__() function but defines __len__() function, then __len__() function is used to get the boolean value of object. If __len__() returns 0, then bool() function will return False otherwise True.
如果对象没有定义__bool __()函数⽽是定义了__len __()函数,则使⽤__len __()函数获取对象的布尔值。 如果__len __()返回0,则bool()函数将返回False,否则返回True。
Boolean value will be False for None and False constants.
对于None和False常量,布尔值将为False 。
Boolean value will be False for zero value numbers such as 0, 0.0, 0j, Decimal(0), and Fraction(0, 1).
对于零值数字(例如0、0.0、0j,Decimal(0)和Fraction(0,1)),布尔值将为False。
Boolean value will be False for empty sequences (, ) and , such as ”, (), [], {} etc.
对于空序列( , )和 (例如”,(),[],{}等),布尔值将为False。
Python bool()⽰例 (Python bool() example)
Let’s look at some simple examples of bool() with bool instances and None.
让我们看⼀下带有bool实例和None的bool()的⼀些简单⽰例。
x = True
b = bool(x)
print(type(x))  # <class 'bool'>
print(type(b))  # <class 'bool'>
print(b)  # True
x = False
b = bool(x)
print(b)  # False
x = None
b = bool(x)
print(type(x))  # <class 'NoneType'>
print(type(b))  # <class 'bool'>
print(b)  # False
The output is self-explained and provided in the comments.
输出是不⾔⾃明的,并在注释中提供。
带有字符串的Python bool() (Python bool() with strings)
# string examples
x = 'True'
b = bool(x)
print(type(x))  # <class 'str'>
print(type(b))  # <class 'bool'>
print(b)  # True
x = 'False'
b = bool(x)
print(b)  # True because len() is used
x = ''
print(bool(x))  # False, len() returns 0
带有数字的Python bool() (Python bool() with numbers)
from fractions import Fraction
from decimal import Decimal
print(bool(10))  # True
print(bool(10.55))  # True
print(bool(0xF))  # True
print(bool(10 - 4j))  # True
print(bool(0))  # False
print(bool(0.0))  # False
print(bool(0j))  # False
print(bool(Decimal(0)))  # False
print(bool(Fraction(0, 2)))  # False
具有集合和序列的Python bool()函数 (Python bool() function with collections and sequences)
tuple1 = ()
dict1 = {}
list1 = []
print(bool(tuple1))  # False
print(bool(dict1))  # False
print(bool(list1))  # False
带有⾃定义对象的Python bool()函数 (Python bool() function with custom object) Let’s see what happens with custom object. I will not define __bool__() and __len__() functions for the object.让我们看看⾃定义对象会发⽣什么。 我不会为该对象定义__bool __()和__len __()函数。
class Data:
id = 0
def __init__(self, i):
self.id = i
d = Data(0)
print(bool(d))
d = Data(10)
print(bool(d))
Output:
输出:
True
True
Since none of __bool__() and __len__() functions are defined, object boolean value is returned as True.
由于未定义__bool __()和__len __()函数,因此对象布尔值将返回True。
Let’s add __len__() function to the Data class.
让我们向Data类添加__len __()函数。
# returns 0 for id <= 0, else id
def __len__(self):
print('len function called')
if self.id > 0:
return self.id
else:
return 0
Output:
输出:
len function called
False
len function called
True
It’s clear that __len__() function is called by bool(). When 0 is returned, bool() function is returning False. When positive integer is returned, then bool() function is returning True.
显然__len __()函数是通过bool()调⽤的。 当返回0时,bool()函数将返回False。 当返回正整数时,则bool()函数将返回True。
Now let’s add __bool__() function to Data class:
现在,将__bool __()函数添加到Data类:
# returns True for id > 0 else False
def __bool__(self):
print('bool function called')
return self.id > 0
Now the above snippet output will be:
现在上⾯的代码⽚段输出将是:
bool function called
False
bool function called
True
It’s clear from the output that if both __bool__() and __len__() functions are defined for the object, then __bool__() function is used to get the Boolean value of object.
从输出中很明显,如果同时为对象定义了__bool __()和__len __()函数,则可以使⽤__bool __()函数获取对象的布尔值。
. 检出完整的python脚本和更多Python⽰例。
Reference:
参考:
bool型数组python
false是什么函数

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。