def myFunc( str ):
print "str=", str
if str == None:
print "str is None"
else:
print "str is not None, value is:", str
This function is called multiple times in my app with str being None. However sometimes, although str is None, the test fails and it prints:
str=None
str is not None, value is None
How can this happen ?
The string
'None'and the bytestringb'None'will both print out None, but not actually be none. Also, you can have custom classes which override their__str__methods to return'None', although they’re actually not None.Some aesthetic notes: Python guarantees that there’ll only ever be one instance of
None, so you should useisinstead of==. Also, you should not name your variablestr, as that’s the name of a built-in.Try this definition: