Python list.index throws exception when index not found

前端 未结 10 1092
抹茶落季
抹茶落季 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:46

    def GetListIndex(list, searchString):
        try:
            return list.index(searchString)
    
        except ValueError:
            return False
    
        except Exception:
            raise
    

提交回复
热议问题