Asked By – Sridhar Ratnakumar
This is what I normally do in order to ascertain that the input is a
tuple – but not a
str. Because many times I stumbled upon bugs where a function passes a
str object by mistake, and the target function does
for x in lst assuming that
lst is actually a
assert isinstance(lst, (list, tuple))
My question is: is there a better way of achieving this?
Now we will see solution for issue: How to check if an object is a list or tuple (but not string)?
In python 2 only (not python 3):
assert not isinstance(lst, basestring)
Is actually what you want, otherwise you’ll miss out on a lot of things which act like lists, but aren’t subclasses of
This question is answered By – Nick Craig-Wood
This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0