jlcv
jlcv

Reputation: 1808

Is there a better way to define this boolean variable?

Suppose I have a some config file that specifies whether or not I get none, one of all of something. That is if get_one_of_this is True, then I get one of something. If it is False then I get none of it. However, I also want a boolean to specify that I get all of something, so I denote this as get_all_of_this. The issue is that the two are in a way redundant, ie. if get_one_of_this is False, then so should get_all_of_this. Is there a better way to do this?

@dataclass
class SomeConfig:
  get_one_of_this: bool
  get_all_of_this: bool

Upvotes: 0

Views: 41

Answers (2)

JT Raber
JT Raber

Reputation: 41

I would look into enumerations if I were you. You could enumerate none, one, all and use that because boolean values are really only supposed to be used when there are 2 discrete states, while enumerations can be used for an arbitrary number of discrete states.

class Quantity(Enum):
    NONE = 0
    ONE = 1
    ALL = 2

Upvotes: 1

lllrnr101
lllrnr101

Reputation: 2343

You can calculate get_all_of_this value in conjunction with get_one_of_this.

get_all_of_this = get_one_of_this and config.get_all_of_this

However I would use an enum to get it done by defining an enum for 3 states.

>>> class State(Enum):
...     NONE = 0
...     ONE = 1
...     ALL = 2
... 
>>> 

Upvotes: 2

Related Questions