Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:not': 0.03; 'string.': 0.05; "subject:' ": 0.07; 'string': 0.09; "'')": 0.09; 'subject:None': 0.09; 'subject:Why': 0.09; 'subject:string': 0.09; "wouldn't": 0.14; 'operates': 0.16; 'piotr': 0.16; 'singleton': 0.16; 'spamming': 0.16; 'spreadsheet.': 0.16; 'str()': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'aug': 0.22; 'convenient': 0.24; 'instead.': 0.24; 'string,': 0.24; "i've": 0.25; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'am,': 0.29; 'generally': 0.29; 'respective': 0.29; 'wonder': 0.29; 'message- id:@mail.gmail.com': 0.30; '-0700,': 0.31; 'cells': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'types.': 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; 'consistent': 0.36; 'data,': 0.36; 'instances': 0.36; 'subject:?': 0.36; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'expect': 0.39; 'does': 0.39; 'though,': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'major': 0.40; 'how': 0.40; 'more': 0.64; '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:from:date:message-id:subject:to :content-type; bh=UR/Y+9Yx+U/sB0VY2ck3QzrI0IHo+dun8wlBadjkAb0=; b=u2xesz1RRCjP0exI0Q5JdeiBAevGWfhXBSkMTfjwoexOTrhjEEBCXe2cj918O9KvbL DkSGU1C5gXGVp4EwL352rF5Cx/RTj1aaKIesMwajEIxvBAZave+Txcl5Yi7eS3RYQJMi eVEbL6bbkxp4xj4COTlglTn1Jhm8nwmwi8mBbmg35BdO6wLcxZpy4ufzp6rktGXuK0a7 4xNIM91BPZJfiztpSu8h9Ta4s+gpsnJUfu8tLw6Hep2t6SJ7gMgvfzBjZdfcC75Wm76I mAfswgvLi9EpdwQpUh+Bsssbz2PbTCBy6LzNfn+AW3ULY99gttnZLpWhwhraYJpfBkWg FPiQ== X-Received: by 10.68.36.132 with SMTP id q4mr2849689pbj.118.1377773036683; Thu, 29 Aug 2013 03:43:56 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <521deb50$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <155b0796-147b-4132-adf0-e73c0e30969a@googlegroups.com> <521deb50$0$6599$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Thu, 29 Aug 2013 04:43:16 -0600 Subject: Re: Why is str(None) == 'None' and not an empty string? To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1377773045 news.xs4all.nl 15946 [2001:888:2000:d::a6]:45757 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:53237 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. 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.