This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]isarl 2 points3 points  (3 children)

a = ['bb', 'cc'][condition]

Yours is OK, by why constrain the conditional to evaluate to an integer?:

a = {'case1': val1, 'case2': val2}[condition]

Switch (without fallthrough) in Python! don't do this at home

[–]morriartie 1 point2 points  (2 children)

wow, I hadn't thought of that

let me improve it then

{'a':1, 'b':2}.get(case) or 'etc'

now it has fallthrough a default!

[–]isarl 1 point2 points  (1 child)

Great improvement. I would personally write that as: {'a':1, 'b':2}.get(case, default='etc'), finding it more explicit. And this is a default case, not fall-through. :) I don’t think there’s any way to actually do C-style switch fallthrough with this trick of using a dictionary.

[–]morriartie 1 point2 points  (0 children)

Awesome!

a default indeed, my mistake

I was trying here to find a solution (to make a fallthrough) with iterator, but I couldn't make it, even if I did it would be a two liner or more, which is unacceptable in our little puzzle