So, say there is a dictionary var_dict
, and I want to print the value at a key a
for eg.
So, one way would be:
if var_dict is None:
return
if 'a' not in var_dict.keys():
return
print(var_dict['a'])
And other method would be:
try:
print(var_dict['a'])
except (KeyError, TypeError):
return
My question is which is recommended. I read here: Python: try-except vs if-else to check dict keys , that exception handling is slower but what if we specify the expected error? Is it slow in that case as well?