Python 3 detect and prevent TypeError-s

Опубликовано: 30 Сентябрь 2024
на канале: Softhints - Python, Linux, Pandas
1,790
7

Python detect and prevent TypeError: sequence item 0 and join iterable

https://blog.softhints.com/python-det...

Errors:

* TypeError: sequence item 0: expected str instance, int found

and

* TypeError: can only join an iterable

Option 1

import collections
mylist = 2
if isinstance(mylist, collections.Iterable):
print(', '.join(mylist))

Option 2

try:
iterator = iter(mylist)
except TypeError:
print('not iterable')
else:
print(', '.join(mylist))