I'm not sure how to phrase this question, so any pointers is appreciated.
Say you are given an integer which represents the state of multiple conditions. As easy analogy is something like setting chmod on a file. 7 is rwx, 5 is wx, 1 is x, etc. How would you do determine the state in code without doing a bunch of if/else tests for all possible values? In my specific case there are only 3 states with values of 1, 2 or 4, so I know I could do:
But it seems like there should be a better method for doing something like this since this convention appears to be commonly used, I just dont know what to look up. I'm writing this in python (using objc objects), but any general pointers will probably get me in the right place.
Say you are given an integer which represents the state of multiple conditions. As easy analogy is something like setting chmod on a file. 7 is rwx, 5 is wx, 1 is x, etc. How would you do determine the state in code without doing a bunch of if/else tests for all possible values? In my specific case there are only 3 states with values of 1, 2 or 4, so I know I could do:
Code:
def isA(i):
if i in [1, 3, 5, 7]:
return True
else:
return False
def isB(i):
if i in [2, 3, 6, 7]:
return True
else:
return False
def isC(i):
if i in [4, 5, 6, 7]:
return True
else:
return False
But it seems like there should be a better method for doing something like this since this convention appears to be commonly used, I just dont know what to look up. I'm writing this in python (using objc objects), but any general pointers will probably get me in the right place.