Since too many python operations return ValueError
, how can we differentiate between them?
Example: I expect an iterable to have a single element, and I want to get it
a, = [1, 2]
: ValueError: too many values to unpacka, = []
: ValueError: too few values to unpack
How can I differentiate between those two cases?? eg
try:
a, = lst
except ValueError as e:
if e.too_many_values:
do_this()
else:
do_that()
I realise that in this particular case I could find a work-around using length/indexing, but the point is similar cases come up often, and I want to know if there's a general approach. I also realise I could check the error message for if 'too few' in message
but it seems a bit crude.