Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.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; 'python,': 0.02; 'programmer': 0.03; 'languages,': 0.04; 'subject:would': 0.07; 'string': 0.09; 'caller': 0.09; 'check,': 0.09; 'returns,': 0.09; 'cc:addr:python-list': 0.11; '(when': 0.16; '25%': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'itself,': 0.16; 'noticed?': 0.16; 'sense,': 0.16; 'sure.': 0.16; 'unexpected': 0.16; 'exception': 0.16; 'appropriate': 0.16; 'do,': 0.16; 'language': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'feb': 0.22; 'cc:addr:python.org': 0.22; 'case.': 0.24; 'pointer': 0.24; 'string,': 0.24; 'fine': 0.24; 'cc:2**0': 0.24; 'possibly': 0.26; 'pass': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'quite': 0.32; 'fri,': 0.33; 'guess': 0.33; "i'd": 0.34; 'case,': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'consistent': 0.36; 'raising': 0.36; 'subject:?': 0.36; 'should': 0.36; 'being': 0.38; 'sometimes': 0.38; 'rather': 0.38; 'extremely': 0.39; 'sure': 0.39; 'either': 0.39; 'users': 0.40; 'even': 0.60; 'most': 0.60; 'worth': 0.66; 'determine': 0.67; '20,': 0.68; 'safe': 0.72; 'increase': 0.74; 'special': 0.74; 'day': 0.76; '2015': 0.84; 'dict.': 0.84; 'subject:you': 0.87; 'to:none': 0.92; 'hand,': 0.93 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:cc :content-type; bh=XogVXD9vaC0fBMxYjAQzRqxJmWVdXukuOT6Z1qIrLjc=; b=regbPjrz5NRWZh0Upik3sn5Gcoa1qm3E3ycU0UaItXisIfr2KYX10Fhisa3v9lclG9 t3ysvtPh+U1Ps1lVMicBTdP9o4llj+uefBg8ZyVo0/apFMs5hrtabSRRTUqGSBwRXv5J Cu/Z0kFEhjSLTk/il40RPSxVjazu9w83ufUfVAeD2Kt8D1T6++GkRVb4D2hYmNs8Xm6b INLZ2MIqvhKdylLD+oAdtRApUAvf4K0DQPR7ZFsIXT4uVLapn6wZ79IIgCg1jWU8ONDG PCCAc3byxs0pH0Qq0A+KiRwPzfxD5gJJWUVsUazOYEWR1nwqMzSc4Jv8CRwP97+UnjVj 7BZA== MIME-Version: 1.0 X-Received: by 10.42.52.200 with SMTP id k8mr8238925icg.26.1424376673291; Thu, 19 Feb 2015 12:11:13 -0800 (PST) In-Reply-To: References: Date: Fri, 20 Feb 2015 07:11:13 +1100 Subject: Re: What behavior would you expect? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424376675 news.xs4all.nl 2896 [2001:888:2000:d::a6]:60974 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85929 On Fri, Feb 20, 2015 at 7:03 AM, Denis McMahon wrote: > On the one hand, the return type of a function (when it returns, rather > than raising an exception) should be consistent to itself, even if using > a language where types are not declared. > Yes, so I'd advise against having a function sometimes return a string and sometimes a dict. Sure. But None gets a bit of a special pass here; in a lot of languages, what you'd have is a "nullable" type (eg in C you might declare the return value as "pointer to char", and either return a pointer to a string, or NULL), but in Python, you return None for the special case. So in a sense, "str or None" is not so inconsistent. > I guess at the end of the day the programmer has to consider and > determine which is most appropriate to his application, given the case. Indeed. But knowing that your caller quite possibly won't check, what would you do, to increase the chances of the unexpected being noticed? Again, it comes down to expectations. If 25% of queries are going to return "nothing found", then having that be the empty string is fine - you can be pretty sure your users will test for it. But if that's an extremely rare case, then it may be worth raising an exception instead, so it's safe even if someone forgets to test for the unusual. ChrisA