I’ve been thinking about this a little bit more today, and I pretty much agree with what I think is the general consensus in the thread:
- Some form of this idea is useful, and
- narrowing the scope (conditional expressions only, and only in certain contexts) would get rid of a lot of the weird cases and make it a lot more palatable.
There is still a question of where exactly this thing should be allowed/disallowed. Looks like there’s broad agreement about:
- Single items in literal list/set/tuple displays:
[..., x, y if condition else pass, z, ...] - Full key/value pairs in literal dict displays:
{..., k: v if condition else pass, ...} - Keyword arguments:
foo(..., x, a=b if condition else pass, y=z, ...)
And some other things that might be more divisive[1]:
- Positional arguments:
foo(..., x if condition else pass, ...) - Unpackings of various kinds:
[..., x, *y if condition else pass, z, ...]foo(x, y, **d if condition else pass)
- Others?
Separately, the more I look at this, the more I think that syntax like x if condition() else pass does suggest that pass is a normal expression of some kind, rather than a keyword.
I’m still oscillating, but I think right now I prefer @15r10nk’s suggestion from the thread I linked in a footnote above, which is to allow omitting the else clause from conditional expressions in some situations, rather than allowing pass to be used inside of conditional expressions in some situations, so instead of:
my_tuple = (
"first_item",
"second_item" if condition else pass,
"third_item",
)
we would have:
my_tuple = (
"first_item",
"second_item" if condition,
"third_item",
)
And instead of:
foo(
a=1,
b=2,
c=3 if condition else pass,
)
we would have:
foo(
a=1,
b=2,
c=3 if condition,
)
This loses the pass idiom as an explicit indicator that we’re excluding something, but to my eyes it’s just as clear, and with quite a bit less typing
. Of course, we could also make the else pass optional to mirror the way you can always add an else: pass to a conditional statement right now.
I quite like these though
↩︎