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


Groups > comp.lang.python > #53237

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

References <155b0796-147b-4132-adf0-e73c0e30969a@googlegroups.com> <521deb50$0$6599$c3e8da3$5496439d@news.astraweb.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2013-08-29 04:43 -0600
Subject Re: Why is str(None) == 'None' and not an empty string?
Newsgroups comp.lang.python
Message-ID <mailman.358.1377773045.19984.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web