Skip to main content
 首页 » 编程设计

python之为什么 str(None) 不是 str(None)

2025年05月04日10wuhuacong

<分区>

我使用 str(object) 将对象转换为字符串。我想使用字符串检查我是否有一个 None 对象(在我的例子中它永远不会是“None”,除非对象是 None)。

string = str(object) // object is None 
if string is 'None' or string is str(None): 
  // do something 

问题是条件永远不会通过。我该如何检查这个案例?

str(None) 与 str(None) 返回 False 的方式相同。为什么?