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: 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; '>>': 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; '>': 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: References: <155b0796-147b-4132-adf0-e73c0e30969a@googlegroups.com> <521deb50$0$6599$c3e8da3$5496439d@news.astraweb.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 To: Ian Kelly Content-Type: multipart/alternative; boundary=089e013c708a239c4404e51c63da Cc: Python X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --089e013c708a239c4404e51c63da Content-Type: text/plain; charset=UTF-8 On 29 August 2013 20:43, Ian Kelly wrote: > On Wed, Aug 28, 2013 at 6:21 AM, Steven D'Aprano > 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 --089e013c708a239c4404e51c63da Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On 29 August 2013 20:43, Ian Kelly <<= a href=3D"mailto:ian.g.kelly@gmail.com" target=3D"_blank">ian.g.kelly@gmail= .com> wrote:
On Wed, Aug 28, 2013 at 6:= 21 AM, Steven D'Aprano
<steve+comp.la= ng.python@pearwood.info> wrote:
> On Wed, 28 Aug 2013 01:57:16 -0700, Piotr Dobrogost wrote:
>
>> Hi!
>>
>> Having repr(None) =3D=3D 'None' is sure the right thing bu= t why does
>> str(None) =3D=3D 'None'? Wouldn't it be more correct i= f 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. =C2=A0Generally=
it's safe to just call str() on the data, except that I'd rather em= pty
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. =C2=A0Not a major inconvenience, but eno= ugh to
make me wonder if there could be a better way.

There is.

def format(value):
=C2= =A0 =C2=A0 if value is None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return &= #39;'

=C2=A0 =C2=A0 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 type= s.

Tim Delaney
--089e013c708a239c4404e51c63da--