2

What is the rationale for the design decision for None type being passed into type-conversion functions?

  • bool(None) returns False - which makes perfect sense

  • str(None) returns 'None' which is not okay - Returning an empty string would be a better choice as below

    >>> bool(None)
    False
    >>> bool(str(None))     #Returning empty string will make it consistent by returning False
    True
    >>> bool('')
    False
    
  • And list(None), dict(None), tuple(None), int(None), float(None) return Type errors - From which if they return [], {}, (), 0 and 0.0 it would have been a obvious and will be useful in many real life scenarios to avoid manual handling.

It's appreciated If someone could explain what might have motivated Guido to go the other way around.

PS : This arose from this question Python: most idiomatic way to convert None to empty string?. Most of the answers are of if-else approaches where as in real life converting/overriding None by empty string, list, dict or 0 would be common.

smci
  • 32,567
  • 20
  • 113
  • 146
nehem
  • 12,775
  • 6
  • 58
  • 84
  • What use would the `bool(str(None))` offer when you can just test truthiness? – SuperBiasedMan Jul 06 '15 at 11:55
  • 2
    `bool()` takes **any** type and determines the boolean value. `None` isn't special there. `str(None)` produces the string `'None'` which isn't empty, so `True`. The rest *don't take `None` as argument*, nor should they. – Martijn Pieters Jul 06 '15 at 11:55
  • I'm not sure why you think passing in `None` gives you 'empty' objects. Don't pass in *any* argument, so `list()`, and `tuple()` and `int()` and `float()`. `None` *is still an object*, not an empty value. – Martijn Pieters Jul 06 '15 at 11:56
  • Also printing `None` as `None` allows you to easier distinguish between a variable being empty or a function returning no `None` instead of a string (due to error, etc.). – SuperBiasedMan Jul 06 '15 at 11:56
  • @Martijn Pieters♦ : Individually everything is justified to work fine yet I could relate `None` to Empty dict or Empty List or 0 based on the data type that gets casted into. – nehem Jul 06 '15 at 11:58
  • @itsneo: no, `None` is an object you pass in instead of another argument. You are trying to convert `None` to that type, but there is no such conversion. For `str()` there *is* a conversion, you get the string `'None'`. – Martijn Pieters Jul 06 '15 at 12:00
  • @itsneo: `None` is *not empty*. It is just a singleton object commonly used to use anywhere you *have* to use an object. – Martijn Pieters Jul 06 '15 at 12:01

1 Answers1

4

None is still an object. It is not an empty value, it is merely a sentinel to use anywhere you have to produce or use an object but you really wanted to leave it empty.

Function calls don't require an argument, calling something with no arguments is perfectly valid. As such there is no point in passing in None, as that would be one argument, not zero.

To create 'empty' objects using the type functions then, just leave the argument list empty:

>>> list()
[]
>>> tuple()
()
>>> str()
''

bool() gives you the boolean type of the argument you passed in. You can call it without an argument:

>>> bool()
False

but if you pass in None, it'll determine the truth value of that object. For None that value is False. str(None) produces the string 'None', which is not empty so it is True when passed to bool(). See the Truth Value Testing section.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343