In some other languages I knows, the intuitive result of a null to string conversion should be an empty string. Why Python is designed to make 'None' be sort of special string? And this can lead to extra work when checking a return value from a function
result = foo() # foo will return None if failure
if result is not None and len(str(result)) > 0:
# ... deal with result
pass
if str(None) returns empty string, the code could be shorter:
if len(str(result)) > 0:
# ... deal with result
pass
Looks like Python is trying to be verbose, to make log files be more understandable?
bool(None) == False, but alsobool('') == False. So in reality you just have to checkif result: #pass, as''andNoneboth evaluate False. I'll try and find some reasoning for the choice ofstr(None) == 'None', but I don't think it should impact your code if you're just checking if a proper string was returned.fooreturnsNonein case of failure, you need to check forNone. In your example, anything whose string representation is empty is apparently handled the same was asNone, but there's no particular reason why that should be the default behavior. The question is, why are you using "length of str(x)" as a diagnostic for anything?repr(None)should be'None', but why shouldn'tstr(None)be''?nullto a string results in the literal string"null". Reference: JLS 5.1.11.