[Python-ideas] Null coalescing operators

Bruce Leban bruce at leban.us
Tue Sep 22 02:16:47 CEST 2015


On Mon, Sep 21, 2015 at 3:56 PM, Carl Meyer <carl at oddbird.net> wrote:

>
> 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.
>

+1

Some language features are "prescriptive," designed to encourage particular
ways of writing things. Others are "respective," recognizing the variety of
ways people write things and respecting that variety. Python has None and
generally respects use of it. To say that using None is an anti-pattern is
something I would strongly disagree with. Yes, NPE errors are a problem,
but eliminating null/None does not eliminate those errors. It merely
replaces one common error with an assortment of other errors.

I like the feature. I have been asking for features like this for years and
the number of times I have written the longer forms is too many to count.

I like the ?.  ?[]  ?()  ?? syntax. I think:

(1) it's strongly related to the . [] () syntax;
(2) any syntax that uses a keyword is either not syntactically related to .
[] () or mixes a keyword and punctuation, both of which I dislike;

(3) it's the same syntax as used in other languages (yes, Python is not C#
or Dart but there's a good reason Python uses ^ for xor, ** for power, +=
for add to, etc.)


--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150921/cfc30976/attachment-0001.html>


More information about the Python-ideas mailing list