def myFunc(str):
print "str=", str
if str == None:
print "str is None"
else:
print "str is not None, value is:", str
该函数在我的应用中被多次调用,str为None。但有时,虽然str是None,但测试失败并且它会打印:Python如果检查失败,则为无
str=None
str is not None, value is None
这是怎么发生的?
谢谢,这有帮助。它打印出“str不是无,它是u'None'类型unicode”。这与if检查工作的其他情况不同:“str不是None,它是NoneType的任何类型”。所以问题是什么是'不',我该如何检查。 – Paul 2012-02-16 12:08:09
@Paul不知道你是否已经回答了这个问题,但是对于所有后来进入这个页面的人来说:'u'None''只是一个值为'None'的unicode字符串('u'None'= ='None'' - >'True';'u'None'是'None' - >'False')。所以如果你想检查u'None',你可以检查:'如果mystr是None或str(mystr)=='None'' – 2012-04-30 17:39:00