通过检查len(str(result))检查字符串中是否有字符绝对不是pythonic(见
http://www.python.org/dev/peps/pep-0008/).
result = foo() # foo will return None if failure
if result:
# deal with result.
pass
无和’强制布尔False.
如果你真的问为什么str(None)不返回“None”,那么我认为是因为它是必要的three-valued logic. True,False和None可以一起使用来确定逻辑表达式是True还是False决定.身份功能是最简单的表示方式.
True -> 'True'
False -> 'False'
None -> 'None'
如果str(None)为”,以下内容真的很奇怪
>>> or_statement = lambda a,b: "%s or %s = %s" % (a,b,a or b)
>>> or_statement(True,False)
'True or False = True'
>>> or_statement(True,None)
'True or None = True'
>>> or_statement(None,None)
'None or None = None'
现在,如果你真的想要一个权威的答案,请问Guido.