Python list.index throws exception when index not found

前端 未结 10 1031
抹茶落季
抹茶落季 2020-12-09 17:00

Why does list.index throw an exception, instead of using an arbitrary value (for example, -1)? What\'s the idea behind this?

To me it looks cleaner to d

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 17:38

    It's a semantic argument. If you want to know the index of an element, you are claiming that it already exists in the list. If you want to know whether or not it exists, you should use in.

提交回复
热议问题