给定一个 if 语句,如:
if response.status == SUCCESS or \
response.status == FAILURE or \
response.status == CLEAR or \
response.status == READY:
像(1)这样重构是否更好:
if any(response.status == status for status in (SUCCESS, FAILURE, CLEAR, READY):
或(2):
if response.status in {SUCCESS, FAILURE, CLEAR, READY}:
我的直觉是 1 更好,因为它更透明(如果不是更易读的话),但 2 更简洁,并且避免了遍历元组中的每个项目。