Why is str(None) == 'None' and not an empty string?

P

Piotr Dobrogost

Hi!

Having repr(None) == 'None' is sure the right thing but why does str(None) == 'None'? Wouldn't it be more correct if it was an empty string?

Regards
Piotr Dobrogost
 
T

Terry Reedy

Having repr(None) == 'None' is sure the right thing but why does str(None) == 'None'? Wouldn't it be more correct if it was an empty string?

No.
There is no reason to be different.
 
S

Steven D'Aprano

Hi!

Having repr(None) == 'None' is sure the right thing but why does
str(None) == 'None'? Wouldn't it be more correct if it was an empty
string?


Why do you think an empty string is more correct? Would you expect
str([]) or str(0.0) or str({}) to also give an empty string?


I can't see any reason for str(None) to return the empty string.
 
I

Ian Kelly

Hi!

Having repr(None) == 'None' is sure the right thing but why does
str(None) == 'None'? Wouldn't it be more correct if it was an empty
string?


Why do you think an empty string is more correct? Would you expect
str([]) or str(0.0) or str({}) to also give an empty string?


I can't see any reason for str(None) to return the empty string.

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.

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.
 
T

Tim Delaney

Hi!

Having repr(None) == 'None' is sure the right thing but why does
str(None) == 'None'? Wouldn't it be more correct if it was an empty
string?


Why do you think an empty string is more correct? Would you expect
str([]) or str(0.0) or str({}) to also give an empty string?


I can't see any reason for str(None) to return the empty string.

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.

There is.

def format(value):
if value is None:
return ''

return str(value)

print(format(value))

This also allows you to format other types differently e.g. only output 2
decimal places for non-integer numeric types.

Tim Delaney
 
S

Steven D'Aprano

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.)
 
S

Steven D'Aprano

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.

Ah, apparently I'm smoking crack, since it wasn't the OP at all. My
apologies. And worse, it was given as an example of what *not* to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top