Test if a variable is a list or tuple

前端 未结 13 1421
余生分开走
余生分开走 2020-12-22 16:53

In python, what\'s the best way to test if a variable contains a list or a tuple? (ie. a collection)

Is isinstance() as evil as suggested here? http://w

13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-22 17:32

    If you just need to know if you can use the foo[123] notation with the variable, you can check for the existence of a __getitem__ attribute (which is what python calls when you access by index) with hasattr(foo, '__getitem__')

提交回复
热议问题