What is the rationale for the design decision for None
type being passed into type-conversion functions?
bool(None)
returnsFalse
- which makes perfect sensestr(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
and0.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.