Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why python designed as str(None) return 'None' instead of an empty string?

Tags:

python

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?

like image 891
Rob L Avatar asked Jul 17 '13 04:07

Rob L


People also ask

Why does Python use None instead of null?

Python uses the keyword None to define null objects and variables. While None does serve some of the same purposes as null in other languages, it's another beast entirely. As the null in Python, None is not defined to be 0 or any other value. In Python, None is an object and a first-class citizen!

Does an empty string return None Python?

Use the boolean OR operator to convert None to an empty string in Python, e.g. result = None or "" . The boolean OR operator returns the value to the left if it's truthy, otherwise the value to the right is returned. Since None is a falsy value, the operation will return "" .

Is None the same as empty string Python?

None is not the same as 0, False, or an empty string. None is a data type of its own (NoneType) and only None can be None.

How do you return a blank string in Python?

Use len to Check if a String in Empty in Python # Using len() To Check if a String is Empty string = '' if len(string) == 0: print("Empty string!") else: print("Not empty string!") # Returns # Empty string!


Video Answer


1 Answers

Checking if a string has characters in it by checking len(str(result)) is definitely not pythonic (see http://www.python.org/dev/peps/pep-0008/).

result = foo() # foo will return None if failure 
if result:
    # deal with result.
    pass

None and '' coerce to the boolean False.


If you are really asking why str(None) does return 'None', then I believe it is because it is necessary for three-valued logic. True, False and None can be used together to determine if a logical expression is True, False or cannot be decided. The identity function is the easiest for representation.

True  -> 'True'
False -> 'False'
None  -> 'None'

The following would be really weird if str(None) was '':

>>> or_statement = lambda a, b: "%s or %s = %s" % (a, b, a or b)
>>> or_statement(True, False)
'True or False = True'
>>> or_statement(True, None)
'True or None = True'
>>> or_statement(None, None)
'None or None = None'

Now, if you really want for an authoritative answer, ask Guido.


If you really want to have str(None) give you '' please read this other question: Python: most idiomatic way to convert None to empty string?

like image 112
dnozay Avatar answered Nov 15 '22 23:11

dnozay