0

Given an if statement like:

if response.status == SUCCESS or \
   response.status == FAILURE or \
   response.status == CLEAR or \
   response.status == READY:

Is it better to refactor like (1):

if any(response.status == status for status in (SUCCESS, FAILURE, CLEAR, READY):

Or (2):

if response.status in {SUCCESS, FAILURE, CLEAR, READY}:

My hunch is that 1 is better as it is more transparent (if not also more readable), but 2 is more concise and avoids having to iterate through each item in the tuple.

1 Answers1

-1

You could use tuple or list like this, no need to iterate. (Python3.7)

>>>'SUCCESS' in ('SUCCESS', 'FAILURE', 'CLEAR', 'READY')
True

your case

if response.status in (SUCCESS, FAILURE, CLEAR, READY):
drt
  • 735
  • 6
  • 16
  • Not sure why got -1? At least leave the reason when you do it. – drt Jan 08 '20 at 21:51
  • How is this supposed to be an answer? You ignore what they ask and instead offer yet another alternative and it's almost identical to their second alternative and you don't say anything about that (like, why yours might be any better). – Kelly Bundy Jan 10 '20 at 00:13
  • This is alternative to option 1, and it is pythonic way of doing it. When you have example provided then explanation is not required always. – drt Jan 10 '20 at 14:14