One might prefer to check for string-ness, as strings can
duck-type somewhat like lists:
my_list = ['my', 'brain', 'hurts']
my_string = 'Are you the brain specialist?'
for test in [my_list, my_string]:
try:
for thing in test:
process_list_item(thing)
except Exception: #whatever flavor you want
The exception should be the one that process_list_item raises when it
receives a string instead of a list. if you want to treat strings and
list in different ways, maybe it means that you are doing different
operations on then, like appendind things to the list or whatever. If
not, than you maybe want to test the types.
process_string(thing) # not called because
#strings are iterable
What if you invert your code?
for test in [my_string, my_list]:
try:
process_string_item(thing)
#suppose process_string_item does some string operation on a
list and gets this
# exception - because if not, I see no meanning in distinguishing then