为什么 'if not None' return 是真的?
Why does 'if not None' return True?
我无法理解这个
我试过了:
if not None:
print('True')
为什么打印的是True?
None
类型不应该是 None
吗?
所有 Python 个对象都有真值,见Truth Value Testing。这包括 None
,在布尔上下文中被认为是 false。
此外,not
运算符必须始终 产生布尔结果,True
或False
。如果 not None
生成了 False
,那么当 bool(None)
已经生成 False
时,这会令人惊讶。
None
值是一个 哨兵对象 ,一个信号值。您仍然需要能够测试该对象,它具有布尔值非常有帮助。举个例子:
if function_that_returns_value_or_None():
如果 None
没有布尔值,该测试就会失败。
4.1. Truth Value Testing
Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:
None
False
zero of any numeric type, for example, 0, 0.0, 0j.
any empty sequence, for example, '', (), [].
any empty mapping, for example, {}.
instances of user-defined classes, if the class defines a bool() or len() method, when that method returns the integer zero or bool value False.
在Python中None
是单例。它在其他语言中被称为 null
。
在您的 if not None:
中,编译器假定 not None 表示非空或非零,我们知道 if 语句将非零值计算为 True
并执行他们。
函数示例:
1) if not None:
在 test()
中打印参数 x
def test(x):
if not None:
print(x)
>>> test(2)
2
2) if 1:
在 test()
中打印参数 x
def test(x):
if 1:
print(x)
>>> test(2)
2
3) if -1:
在 test()
中打印参数 x
def test(x):
if -1:
print(x)
>>> test(2)
2
4) if 0:
不在 test()
中打印参数 x
def test(x):
if 0:
print(x)
>>> test(2)
5) if True:
在 test()
中打印参数 x
def test(x):
if True:
print(x)
>>> test(2)
2
每个值都有一个 属性,称为 "truthiness"。 None
的"truthiness"是False
。之所以如此,有几个原因,例如当您将 None
的 return 值视为失败或 False
时的干净代码。
"Empty" 对象,如 ''
、[]
、0
或 {}
都计算为 false。请注意,这不包括 'None'
(字符串)或 '0'
等对象。
因此 if not None
将 None
转换为 False
。
"Truthiness" 也称为 "booleaness",在某些情况下更正式。
[讽刺模式开启]
如果你不喜欢打印 True
你可以让它打印 False
:
if not None:
print('False')
现在它打印 False :)
编辑: 如果您担心为什么它不打印 None
而不是 True
或 False
(或 Apples
) 你可以让它打印 None
:
if not None:
print('None')
我无法理解这个
我试过了:
if not None:
print('True')
为什么打印的是True?
None
类型不应该是 None
吗?
所有 Python 个对象都有真值,见Truth Value Testing。这包括 None
,在布尔上下文中被认为是 false。
此外,not
运算符必须始终 产生布尔结果,True
或False
。如果 not None
生成了 False
,那么当 bool(None)
已经生成 False
时,这会令人惊讶。
None
值是一个 哨兵对象 ,一个信号值。您仍然需要能够测试该对象,它具有布尔值非常有帮助。举个例子:
if function_that_returns_value_or_None():
如果 None
没有布尔值,该测试就会失败。
4.1. Truth Value Testing
Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:
None
False
zero of any numeric type, for example, 0, 0.0, 0j.
any empty sequence, for example, '', (), [].
any empty mapping, for example, {}.
instances of user-defined classes, if the class defines a bool() or len() method, when that method returns the integer zero or bool value False.
在Python中None
是单例。它在其他语言中被称为 null
。
在您的 if not None:
中,编译器假定 not None 表示非空或非零,我们知道 if 语句将非零值计算为 True
并执行他们。
函数示例:
1) if not None:
在 test()
def test(x):
if not None:
print(x)
>>> test(2)
2
2) if 1:
在 test()
def test(x):
if 1:
print(x)
>>> test(2)
2
3) if -1:
在 test()
def test(x):
if -1:
print(x)
>>> test(2)
2
4) if 0:
不在 test()
def test(x):
if 0:
print(x)
>>> test(2)
5) if True:
在 test()
def test(x):
if True:
print(x)
>>> test(2)
2
每个值都有一个 属性,称为 "truthiness"。 None
的"truthiness"是False
。之所以如此,有几个原因,例如当您将 None
的 return 值视为失败或 False
时的干净代码。
"Empty" 对象,如 ''
、[]
、0
或 {}
都计算为 false。请注意,这不包括 'None'
(字符串)或 '0'
等对象。
因此 if not None
将 None
转换为 False
。
"Truthiness" 也称为 "booleaness",在某些情况下更正式。
[讽刺模式开启]
如果你不喜欢打印 True
你可以让它打印 False
:
if not None:
print('False')
现在它打印 False :)
编辑: 如果您担心为什么它不打印 None
而不是 True
或 False
(或 Apples
) 你可以让它打印 None
:
if not None:
print('None')