I've been toying with a similar idea myself. I've felt the pain
described by Brian, and I share Marco's dislike for the suggested
syntax. Moreover, I dislike the idea that the conditional should
somehow refer to the function's default arguments.
My half-baked idea is along the lines of
f(val1, val2, if cond3: val3, if cond4: arg4=val4)
with the sense that if a condition is not met, the argument is just not
passed; this would be equivalent to
f(
val1, val2,
*[_ for _ in [val3] if cond3],
**{'arg4': _ for _ in [val4] if cond4}
)
As presented, this would work for list and set literals as well, but
the syntax is not good for dict literals; "{if cond: key: value}" feels
completely wrong. Alternative "{key if cond: value}" looks palatable at
first glance, but doesn't feel quite "in line" with the rest of the
proposal, and for my taste, looks too similar to the already valid
"{key1 if cond else key2: value}".
That last similarity is also why I don't like the syntax
f(arg1, arg2, arg3 if cond3, arg4=cond4 if cond4)
I've been thinking about alternatives such as
f(arg1, if (cond2) arg2)
f(arg1, (if cond2) arg2)
or just marking the if as a "special if", e.g.
f(arg1, arg2 if* cond2)
But I'm not really pleased with any of them.
Have fun,
Shai.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/ZRDK3Z55MMTVZOLMYSKC32WE7DTZYNFS/
Code of Conduct: http://python.org/psf/codeofconduct/