我有这个代码:
def try_(things):
try:
return things
except:
return None
但我希望这些调用产生以下结果:
try_([1,2,3][2]) -> 3
try_([1,2,3][4]) -> Error (But I want it to return None)
您需要将索引作为参数传递:
def try_(things, index):
try:
return things[index]
except:
return None
像这样称呼它:
try_([1,2,3], 2) -> 3
try_([1,2,3], 4) -> None