Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53117 > unrolled thread

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

Started byPiotr Dobrogost <p@google-groups-2013.dobrogost.net>
First post2013-08-28 01:57 -0700
Last post2013-08-30 06:59 +1000
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Why is str(None) == 'None' and not an empty string? Piotr Dobrogost <p@google-groups-2013.dobrogost.net> - 2013-08-28 01:57 -0700
    Re: Why is str(None) == 'None' and not an empty string? Terry Reedy <tjreedy@udel.edu> - 2013-08-28 06:33 -0400
    Re: Why is str(None) == 'None' and not an empty string? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-28 12:21 +0000
      Re: Why is str(None) == 'None' and not an empty string? Ian Kelly <ian.g.kelly@gmail.com> - 2013-08-29 04:43 -0600
        Re: Why is str(None) == 'None' and not an empty string? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-30 01:57 +0000
          Re: Why is str(None) == 'None' and not an empty string? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-30 02:44 +0000
      Re: Why is str(None) == 'None' and not an empty string? Tim Delaney <timothy.c.delaney@gmail.com> - 2013-08-30 06:59 +1000

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

FromPiotr Dobrogost <p@google-groups-2013.dobrogost.net>
Date2013-08-28 01:57 -0700
SubjectWhy is str(None) == 'None' and not an empty string?
Message-ID<155b0796-147b-4132-adf0-e73c0e30969a@googlegroups.com>
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

[toc] | [next] | [standalone]


#53124

FromTerry Reedy <tjreedy@udel.edu>
Date2013-08-28 06:33 -0400
Message-ID<mailman.300.1377686649.19984.python-list@python.org>
In reply to#53117
On 8/28/2013 4:57 AM, Piotr Dobrogost wrote:

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

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#53143

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-08-28 12:21 +0000
Message-ID<521deb50$0$6599$c3e8da3$5496439d@news.astraweb.com>
In reply to#53117
On Wed, 28 Aug 2013 01:57:16 -0700, Piotr Dobrogost wrote:

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



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#53237

FromIan Kelly <ian.g.kelly@gmail.com>
Date2013-08-29 04:43 -0600
Message-ID<mailman.358.1377773045.19984.python-list@python.org>
In reply to#53143
On Wed, Aug 28, 2013 at 6:21 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Wed, 28 Aug 2013 01:57:16 -0700, Piotr Dobrogost wrote:
>
>> 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.

[toc] | [prev] | [next] | [standalone]


#53267

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-08-30 01:57 +0000
Message-ID<521ffc19$0$6599$c3e8da3$5496439d@news.astraweb.com>
In reply to#53237
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

[toc] | [prev] | [next] | [standalone]


#53271

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-08-30 02:44 +0000
Message-ID<5220070b$0$6599$c3e8da3$5496439d@news.astraweb.com>
In reply to#53267
On Fri, 30 Aug 2013 01:57:45 +0000, Steven D'Aprano wrote:

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


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#53257

FromTim Delaney <timothy.c.delaney@gmail.com>
Date2013-08-30 06:59 +1000
Message-ID<mailman.372.1377809995.19984.python-list@python.org>
In reply to#53143

[Multipart message — attachments visible in raw view] — view raw

On 29 August 2013 20:43, Ian Kelly <ian.g.kelly@gmail.com> wrote:

> On Wed, Aug 28, 2013 at 6:21 AM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> wrote:
> > On Wed, 28 Aug 2013 01:57:16 -0700, Piotr Dobrogost wrote:
> >
> >> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web