[Python-ideas] Null coalescing operators

Chris Angelico rosuav at gmail.com
Mon Sep 21 15:27:24 CEST 2015


On Mon, Sep 21, 2015 at 8:55 PM, Paul Moore <p.f.moore at gmail.com> wrote:
> There seem to be a few main use cases:
>
> 1. Dealing with functions that return a useful value or None to signal
> "no value". I suspect the right answer here is actually to rewrite the
> function to not do that in the first place. "Useful value or None"
> seems like a reasonable example of an anti-pattern in Python.

The alternative being to raise an exception? It's generally easier,
when you can know in advance what kind of object you're expecting, to
have a None return when there isn't one. For example, SQLAlchemy has
.get(id) to return the object for a given primary key value, and it
returns None if there's no such row in the database table - having to
wrap that with try/except would be a pain. This isn't an error
condition, and it's not like the special case of iteration (since an
iterator could yield any value, it's critical to have a non-value way
of signalling "end of iteration"). I don't want to see everything
forced to "return or raise" just because someone calls this an
anti-pattern.

ChrisA


More information about the Python-ideas mailing list