I'm working through a mechanical systems (for example, a simple helical gear) and want to prevent users from assigning values not found in an associated enum. I am new to enums and 'pythonic' approaches to answering this question...so my request is two-fold:
- Is it more appropriate in python to let input errors happen and use subsequent code to manage the fallout?
- Should the enum be defined within the class that uses it? I don't intend for this enum to be available elsewhere...I feel weird just letting it hang out at the beginning of my file.
Here's the code:
HAND = Enum('HAND', ['LH', 'RH'])
class HelicalGear(Gear):
def __init__(self, hand):
self.type_ = 'Helical'
self.hand = hand
@property
def hand(self):
return self._hand
@hand.setter
def hand(self, hand):
if not hand:
raise ValueError("Gear hand definition required")
elif hand not in [e.name for e in HAND]:
raise ValueError("Gear hand must be LH or RH")
self._hand = hand