[Python-ideas] PEP 505 (None coalescing operators) thoughts
Steven D'Aprano
steve at pearwood.info
Thu Oct 1 02:33:58 CEST 2015
On Wed, Sep 30, 2015 at 09:41:53AM -0700, Jeff Hardy wrote:
> 'def' is currently short for 'define', which would be too confusing.
> Spelling out 'default' isn't so bad, though:
>
> self.x = x default []
Heh, people jest :-)
Oh, you're serious? That just goes to show how different we all are. I
think that using "default" as an infix operator is hideous.
"default" as a function/verb? Sure, that works fine:
if len(missed_payments) > 5:
loan.default()
"default" as a variable/noun? Absolutely fine:
default = "something or other"
But "x default z" just doesn't read well: in English, it sounds like you
are ordering x to cause z to default. Not very many words read naturally
as a binary infix operator.
Besides, I would expect that making "default" a keyword will break a
*huge* number of programs. I'm sure that many, many people use it as a
variable or parameter name.
> And if it's going to be that long anyway, we might as well just put a
> `default` function in the builtins:
>
> self.x = default(x, [])
That's useless, as it breaks the short-circuiting property, which is one
of the critical motives for introducing the null coalescing operator.
Without the short-circuiting property, there's no point in making it a
built-in. If all you want is a default function, then just add it as a
helper function to the top of your code:
def default(obj, alternative):
if obj is None:
return alternative
return obj
> I actually really like 'otherwise', but it's certainly not brief:
>
> self.x = x if x is not None else []
> self.x = x otherwise []
That's almost half the length of the alternative. And if you have
something more realistic, you save proportionally even less:
document if document is not None else Document()
document otherwise Document()
But personally, I have absolutely zero interest in the null coalescing
operator on its own. I don't object to it specifically, but what really
interests me are the two (pseudo)operators, null-aware indexing and
null-aware attribute lookup. How do you extend "otherwise" to work with
the [] and . operators?
--
Steve
More information about the Python-ideas
mailing list