Try instead of check
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: Jan. 26, 2022, 10:14 a.m.
Updated: Jan. 26, 2022, 8:30 p.m.
0