On Thu, 29 Aug 2013 04:43:16 -0600, Ian Kelly wrote: > I've had many occasions where it would have been convenient for > str(None) to return the empty string, e.g. when exporting tabular data > that includes null values from a database to a spreadsheet. Generally > it's safe to just call str() on the data, except that I'd rather empty > cells just be empty rather than spamming the word "None" all over the > place, so I end up having to do something like (str(value) if value is > not None else '') instead. Not a major inconvenience, but enough to > make me wonder if there could be a better way.
Similarly, the interactive interpreter also special-cases None, and prints nothing at all if the result is None: py> None py> '' '' So it isn't that there are *no cases at all* where one might want None to display as empty. But then, there are also good use-cases for wanting 0 to display as empty too, which is why Excel and other spreadsheets allow you to create your own format strings controlling the display of +ve numbers, 0 and -ve numbers individually. The question is, what should str(None) give? I cannot see any reason to have str(None) return '' as the standard behaviour any more than str(0) ought to return '' as standard. > I would not expect str([]) or str(0.0) or str({}) to return an empty > string. I would expect these to return '[]', '0.0', and '{}' > respectively, which are all consistent with how str operates on other > values of their respective types. None is a singleton though, so it's > not constrained by how other instances of NoneType behave. "Other instances of NoneType" is irrelevant. NotImplemented is also a singleton. Would you expect str(NotImplemented ) to return the empty string, or perhaps some other arbitrary string like "?", or would you expect it to return 'NotImplemented'? Likewise for Ellipsis, which is another singleton. Custom classes aside, which of course could do *anything* no matter how silly, I can only think of two objects where str() returns the empty string: the empty byte string '' and the empty Unicode string u''. (In Python 3, there is only one, namely the empty Unicode string.) So while I don't doubt that it wouldn't be occasionally convenient to map None to '' rather than 'None', I think it would be surprising and, indeed, dangerous if it happened by default, since it would encourage people to build up SQL query strings by concatenation, as in the Original Poster's example. (And thank you for picking up on that.) -- Steven -- http://mail.python.org/mailman/listinfo/python-list