Is it better to catch blocks using try-catch or to check blocks using if-else?
Or more concretely, is my try-catch code Pythonic and appropriate?
Mission: https://py.checkio.org/en/mission/sum-by-type/
try-catch
def sum_by_types(items: list) -> str | int:
strings, digits = "", 0
for item in items:
try:
digits += item
except TypeError:
strings += item
return strings, digits
if-else
def sum_by_types(items: list) -> str | int:
strings, digits = "", 0
for item in items:
if isinstance(item, str):
strings += item
else:
digits += item
return strings, digits
Created at: 2022/01/26 10:14; Updated at: 2022/01/26 20:30