Consider this simple code, which stores a few properties of a MyClass()
object in a list:
c = MyClass()
props = [c.property1, c.property2, ..., c.propertyN]
Now, under the hood, c.property1..N
are realized as @property
-decorated getter functions, which may raise exceptions during execution. In that case, I want to use a default value ('n/a') in the list assignment, instead of the inaccessible property value.
But I don't want to skip the entire list assignment altogether. So, if only c.property2
causes an exception, the result should still be:
props = ['value_of_property1', 'n/a', ..., 'value_of_propertyN']
So I cannot wrap the try-except around the entire list assignment:
c = MyClass()
try:
props = [c.property1, c.property2, ..., c.propertyN]
except MyException:
props = # yes, what?
Of course I could try-except for each element individually:
c = MyClass()
props = []
try:
props.append(c.property1)
except MyException:
props.append('n/a')
try:
props.append(c.property2)
except MyException:
props.append('n/a')
# repeated N times
... but this bloats the code and doesn't feel right.
Is there some sort of inline exception handling?
Perhaps similar these inline if-else statements:
props = [c.property1 if len(c.property1) else 'n/a',
c.property2 if len(c.property2) else 'n/a',
...
c.propertyN if len(c.propertyN) else 'n/a',
]
Notes:
I have full control over the code for
MyClass()
and the getter functions.Modifying the getter functions to handle exceptions internally and return a default value straight away is not an option, because in other situations I want to know (via exception...) if the property is accessible or not. Otherwise how could I tell if 'n/a' was the true value of a property or the replaced default (fallback) value?
I have the same problem in print statements using
(N*'{};').format(c.property1, c.property2, ..., c.propertyN)