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