I repeat a lot:
val = x if x else y
Sometimes xseveral levels go deep into a class or a dictionary, so it becomes very long:
val = obj.elements[0].something if obj.elements[0].something else y
It looks ugly and makes me type a lot more. Any known ways to shorten this? Perhaps such a built-in built-in?
val = first_try(x, y)
I think I could easily write my own, but was hoping for a built-in version.
first_try = lambda x,y: x if x else y
source
share