3 ms·
Since you're using Python for your example, it's worth mentioning that 3.5 extended the unpacking syntax [1] to sort of do what you want. foo = { '
by rraval 9y ago
Since you're using Python for your example, it's worth mentioning that 3.5 extended the unpacking syntax [1] to sort of do what you want.
foo = {
'a': 1,
'b': 2,
**({
'c': 3,
'd': 4,
} if bar else {}),
}
This adds `c: 3` and `d: 4` into `foo` conditional on `bar`.
[1] https://docs.python.org/3/whatsnew/3.5.html#pep-448-additional-unpacking-generalizations https://docs.python.org/3/whatsnew/3.5.html#pep-448-addition...
- rtpg 9y agooh, this is most definitely a thing I want. Not sure if my coworkers would appreciate this however. Would still have liked a bit of a simpler syntax for inclusion but this could be worth the ugliness Nice that this works on lists as well.
- rraval 9y agoYeah, I personally find this sort of syntax convolution ugly and hard to read but I thought it relevant. Enough rope to hang yourself with and whatnot.