Consider this line:
some_value = lst.attr[idx]
There are two possible errors here, the attr
might not exist, and the idx
might be out of range.
Is there any elegant way to reduce this statement? Ideally, to something like this:
some_value = lst.attr[idx] or default_value
(Don't try that at home. That only works for properly defined expressions that evaluate to something.)
Sure I can do:
try:
some_value = lst.attr[idx]
except:
some_value = default_value
But what if I'm in the context of an assignment? For example:
print [x.attr[idx] for x in y]
What's the pythonic way to handle errors and assign default values in this case?
You can set the default values for variables by adding ! default flag to the end of the variable value. It will not re-assign the value, if it is already assigned to the variable.
Introduction to Python default parameters When you define a function, you can specify a default value for each parameter. In this syntax, you specify default values ( value2 , value3 , …) for each parameter using the assignment operator ( =) .
Adding a Default Value in R You can specify default values for any disagreements in the argument list by adding the = sign and default value after the respective argument. You can specify a default value for argument mult to avoid specifying mult=100 every time.
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
You need to decide what you are trying achieve here. The use of the word "error" is probably misleading.
If you are actually trying to handle the case where the wrong type of object is passed to your function then you don't want to handle that and should raise an exception.
If you are trying to allow your function to be used on a series of different types then that's not really an error and using a default value may be reasonable.
The simplest option is to test whether the attribute exists first. For example:
if hasattr(lst, "attr"):
attr = lst.attr
else:
attr = {}
I'm assuming the lst.attr
is a dictionary, in which case you can handle the default value like so:
lst.attr.get(idx, default_value)
Never use a try
/except
statement where you don't specify what exception you are catching. You can end up masking much more than you intended to.
With your final piece of code I think you should not try and solve it in a single line. Readability counts. I'm not happy with the code below, but it would be improved if x
, y
and attr
were replaced with more descriptive names.
attrs = [(x.attr if hasattr(x) else {}) for x in y]
print [attr.get(idx, default_value) for attr in attrs]
What's the pythonic way to handle errors and assign default values in this case?
>>> import this
...
Explicit is better than implicit.
...
Sparse is better than dense.
Readability counts.
...
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
I have the feeling that the "Pythonic way to assign default values"* is either to handle exception - as you already mentioned in your question - either to write you own getters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With