[Python-ideas] Null coalescing operators

Carl Meyer carl at oddbird.net
Tue Sep 22 00:56:03 CEST 2015

On 09/21/2015 04:45 PM, Terry Reedy wrote:
> On 9/21/2015 5:48 PM, Guido van Rossum wrote:
>> On Mon, Sep 21, 2015 at 2:23 PM, Terry Reedy
>> <tjreedy at udel.edu
>> <mailto:tjreedy at udel.edu>> wrote:
>>     I agree with Paul Moore that propagating None is generally a bad
>>     idea. It merely avoids the inevitable exception.
> To me, this is the key idea in opposition to proposals that make
> propagating None easier.
> I guess some other pythonistas like keeping None around
> more than I do ;-).

I think it's one of those things that depends on what you're doing. From
a web-development perspective, you rarely keep _anything_ around for
very long, so there's rarely an issue of `None` sneaking in somewhere
unexpectedly and then causing a surprise exception way down the line.
Typical use cases are things like: "If this database query returns a
User, I want to get their name and return that in the JSON dict from my
API, otherwise I want None, which will be serialized to a JSON null,
clearly indicating that there is no user here."

My jaw dropped a bit when I saw it asserted in this thread that
functions returning "useful value or None" is an anti-pattern. I write
functions like that all the time, and I consider it a useful and
necessary Python idiom. I would hate to rewrite all that code to either
deal with exceptions or add default-value-argument boilerplate to all of
them; when "no result" is an expected and normal possibility from a
function, letting the calling code deal with None however it chooses is
much nicer than either of those options.

I don't love the ? syntax, but I would certainly use the feature
discussed here happily and frequently.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150921/35a0012c/attachment-0001.sig>

More information about the Python-ideas mailing list