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


Groups > comp.lang.python > #53257

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <timothy.c.delaney@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:not': 0.03; 'output': 0.05; 'string.': 0.05; 'differently': 0.07; 'none:': 0.07; "subject:' ": 0.07; 'string': 0.09; "'')": 0.09; 'subject:None': 0.09; 'subject:Why': 0.09; 'subject:string': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; "wouldn't": 0.14; '&gt;&gt;': 0.16; 'piotr': 0.16; 'spamming': 0.16; 'spreadsheet.': 0.16; 'str()': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'convenient': 0.24; 'instead.': 0.24; 'string,': 0.24; 'cc:2**0': 0.24; "i've": 0.25; '&gt;': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'am,': 0.29; 'generally': 0.29; 'tim': 0.29; 'wonder': 0.29; 'message-id:@mail.gmail.com': 0.30; '-0700,': 0.31; 'cells': 0.31; "d'aprano": 0.31; 'decimal': 0.31; 'steven': 0.31; 'types.': 0.31; 'allows': 0.31; "i'd": 0.34; 'could': 0.34; "can't": 0.35; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'data,': 0.36; 'subject:?': 0.36; 'skip:& 10': 0.38; 'e.g.': 0.38; 'rather': 0.38; 'expect': 0.39; 'does': 0.39; 'sure': 0.39; 'enough': 0.39; 'skip:p 20': 0.39; 'major': 0.40; 'ian': 0.60; 'is.': 0.60; 'august': 0.61; 'places': 0.64; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'safe': 0.72; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=vsDPMs+hHXtoYwApvAM1xOhSUTyUinZDC5WrqZ8mkgY=; b=mkovNHVm7NM5/f8dknMZ80Mc74SJS1zcyPQKIoSBlPjqfU8/I5Nt6aIeOhPKVREgVV wPWNl59V1pVsNeITnnPXJ/AP/uWAeZTklhnDfU/8FgiPqY2vqn3cIKuW2wUJrH9zgaAc JXwu0OnccRDbWGo6aKdTklb68tz8GVCOezhJVMAjTWMMSpEN34A5uPDcEe4k8H23grNM bFP7NaN3viCKuPjlUEgZjXiE5VU/M3Mu+h4Y14bCeLus4ffYljMavkKiLK6NszfzwMCZ 4rGmBsdqB5TCKt5oM9lC5LqkRAzJ4v3JyAzOtJF7GLckC1nNvp8MgxCorDu7E7cfWH8V 6Qhw==
MIME-Version 1.0
X-Received by 10.60.80.234 with SMTP id u10mr1727747oex.83.1377809991963; Thu, 29 Aug 2013 13:59:51 -0700 (PDT)
In-Reply-To <CALwzidkLXTWzbsW_mjuHE4eO=e5iw7gEWfK+4+tkijCF2nQ3jQ@mail.gmail.com>
References <155b0796-147b-4132-adf0-e73c0e30969a@googlegroups.com> <521deb50$0$6599$c3e8da3$5496439d@news.astraweb.com> <CALwzidkLXTWzbsW_mjuHE4eO=e5iw7gEWfK+4+tkijCF2nQ3jQ@mail.gmail.com>
Date Fri, 30 Aug 2013 06:59:51 +1000
Subject Re: Why is str(None) == 'None' and not an empty string?
From Tim Delaney <timothy.c.delaney@gmail.com>
To Ian Kelly <ian.g.kelly@gmail.com>
Content-Type multipart/alternative; boundary=089e013c708a239c4404e51c63da
Cc Python <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.372.1377809995.19984.python-list@python.org> (permalink)
Lines 101
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1377809995 news.xs4all.nl 16005 [2001:888:2000:d::a6]:34012
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:53257

Show key headers only | View raw


[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

Back to comp.lang.python | Previous | NextPrevious 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