python - How to link variables so that they cannot both be true or both be false etc.? -


not sure if question title correct, please tell me if isn't so.

say have list, dictionary or tuple of transactions, transactions instances of class. want record whether transaction pending, completed or cancelled, create booleans (within __init__() of class) self.completed , self.cancelled (whether pending or not can inferred both self.completed , self.cancelled being false)

of course, transaction cannot more 1 of 3 states @ time, there library check 1 true @ time?

i check every time change 1 of variables , raise exception, rather save lines of code.

i have variable integer value of 0, 1, or 2; 0 pending, 1 completed etc. however, gets rid of intuitive:

    if transaction_a.completed == true: 

thank in advance!

what want have single status attribute instead of 3 booleans. can use enum represent different states. can make 3 properties query status attribute:

from enum import enum, auto  class status(enum):     pending = auto()     completed = auto()     cancelled = auto()  class transaction:     def __init__(self):         self.status = status.pending      @property     def is_pending(self):         return self.status == status.pending      @property     def is_completed(self):         return self.status == status.completed 
t = transaction()  print(t.is_pending) # true t.status = status.completed print(t.is_pending) # false print(t.is_completed) # true 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -