"""
python中的所有数据类型都能转bool
对应关系:0 空序列 字典 None -》False
else ->True
但是对于自定义对象来说:
这种转换受到内置函数
__bool__
__len__
返回值的影响
其中__bool__影响优先级高于__len__
"""
class Test():
pass
print(bool(Test()))
class Test1():
def __len__(self):
return 0
print(bool(Test1()))
class Test2():
def __bool__(self):
return False
print(bool(Test2()))
class Test3():
def __bool__(self):
return True
def __len__(self):
return 0
print(bool(Test3()))
--------------------------
C:\Python\Python36\python.exe D:/IdeaProjects/python_basic/extend/learn_boo.py
True
False
False
True
Process finished with exit code 0