#Example; Null references.
if __name__=="__main__":
ref1=None
ref2=True
ref3=False
print "REF1",ref1==True
print "REF2",ref2==True
print "REF3",ref3==True
Normally this wouldn't make a difference. Certain variables will have different content but will evaluate to True in truth tests. For instance, consider the following.
#Example; Null references.
if __name__=="__main__":
ref1=""
ref2="My String"
ref3=None
if ref1:
print "REF1 TRUE"
else:
print "REF1 FALSE"
if ref2:
print "REF2 TRUE"
else:
print "REF2 FALSE"
if ref3:
print "REF3 TRUE"
else:
print "REF3 FALSE"
- ref1 is False because it is empty. But, it is still a string.
- ref2 is True because it is a non-empty string.
- ref3 is False because it is a null reference and hence has no way of knowing if it is True.
#Example; Null references.
def print_length(str_obj):
print len(str_obj)
if __name__=="__main__":
ref1=""
ref2="My String"
ref3=None
print_length(ref1)
print_length(ref2)
print_length(ref3)
If you want something to evaluate to False until it is non-empty, you can still specify a type that will evaluate to false. If not, build your own Null type.
Perhaps I'm missing something here, but what's wrong with testing variables as so:
ReplyDeleteif ref1 is None:
# do something special
In the examples you give, you'd easily be able to get the desired behaviour, with intuitive & simple code...