概述
result = foo() # foo will return None if failure if result is not None and len(str(result)) > 0: # ... deal with result pass
如果str(None)返回空字符串,代码可能会更短:
if len(str(result)) > 0: # ... deal with result pass
看起来Python正在尝试冗长,使日志文件更容易理解?
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.
如果你真的想要str(无)给你”请阅读另一个问题:Python: most idiomatic way to convert None to empty string?
总结
以上是编程之家为你收集整理的为什么python设计为str(无)返回’None’而不是空字符串?全部内容,希望文章能够帮你解决为什么python设计为str(无)返回’None’而不是空字符串?所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧