Sorry if I missed the boat, but only just now saw this PEP.
Glancing through the PEP, I don't see mentioned anywhere the SQL alternative of having a coalesce() function: https://www.postgresql.org/docs/9.6/static/functions-conditional.html#FUNCTI...
In Python, something like this:
def coalesce(*args): for arg in args: if arg is not None: return arg return None
Just drop it into builtins, and voila. No need for lengthy discussions about which operator to use because IMHO it needs no operator.
Sure, it's not as sexy as a fancy new operator, nor as headline grabbing, but it is pretty useful.
Just my 2p.
On Fri, Oct 14, 2016 at 4:14 PM Gustavo Carneiro gjcarneiro@gmail.com wrote:
Sorry if I missed the boat, but only just now saw this PEP.
Glancing through the PEP, I don't see mentioned anywhere the SQL alternative of having a coalesce() function: https://www.postgresql.org/docs/9.6/static/functions-conditional.html#FUNCTI...
In Python, something like this:
def coalesce(*args): for arg in args: if arg is not None: return arg return None
Just drop it into builtins, and voila. No need for lengthy discussions about which operator to use because IMHO it needs no operator.
Sure, it's not as sexy as a fancy new operator, nor as headline grabbing, but it is pretty useful.
This has the downside of not being short-circuit - arguments to the function are evaluated eagerly.
Elazar
On 14 October 2016 at 14:19, אלעזר elazarg@gmail.com wrote:
On Fri, Oct 14, 2016 at 4:14 PM Gustavo Carneiro gjcarneiro@gmail.com wrote:
Sorry if I missed the boat, but only just now saw this PEP.
Glancing through the PEP, I don't see mentioned anywhere the SQL alternative of having a coalesce() function: https://www. postgresql.org/docs/9.6/static/functions-conditional. html#FUNCTIONS-COALESCE-NVL-IFNULL
In Python, something like this:
def coalesce(*args): for arg in args: if arg is not None: return arg return None
Just drop it into builtins, and voila. No need for lengthy discussions about which operator to use because IMHO it needs no operator.
Sure, it's not as sexy as a fancy new operator, nor as headline grabbing, but it is pretty useful.
This has the downside of not being short-circuit - arguments to the function are evaluated eagerly.
I see. short-circuiting is nice to have, sure.
But even without it, it's still useful IMHO. If you are worried about not evaluating an argument, then you can just do a normal if statement instead, for the rare cases where this is important:
result = arg1 if result is None: result = compute_something()
At the very least I would suggest mentioning a simple coalesce() function in the alternatives section of the PEP.
coalesce function: Pros: 1. Familiarity, similar to existing function in SQL; 2. No new operator required; Cons: 1. Doesn't short-circuit the expressions; 2. Slightly more verbose than an operator;
Thanks.
On Fri, Oct 14, 2016 at 9:37 AM, Gustavo Carneiro gjcarneiro@gmail.com wrote:
I see. short-circuiting is nice to have, sure.
But even without it, it's still useful IMHO. <snip>
It's worth mentioning that SQL's COALESCE is usually (always?) short circuiting:
https://www.postgresql.org/docs/9.5/static/functions-conditional.html https://msdn.microsoft.com/en-us/library/ms190349.aspx
Given the debate about the utility of coalescing and the simplicity of writing the function yourself, I doubt the standard library will accept it. Most people here will tell you that such a utility belongs on PyPI.
On Fri, Oct 14, 2016 at 6:37 AM, Gustavo Carneiro gjcarneiro@gmail.com wrote:
I see. short-circuiting is nice to have, sure.
No. Short-circuiting is the entire point of the proposed operators.