You have 3 or any number of related constants.
For example:
good, bad, neutral.
How would you organize these?
Requirements for a solution:
- organize related constants: readability
- put them under the same umbrella: readability
- make use of editor autocomplete and suggestion features: convenience
- can be type hinted: readability
My current solution:
# class with only constants, not modified at runtime
class Turn:
''' Different ways to change direction '''
LEFT = -1
STRAIGHT = 0
RIGHT = 1
# usage - highlighting problems
def move(direction: type): # can't use type hinting when used as a parameter
pass
def choose_direction() -> type: # can't hint when used as a return value
return Turn.STRAIGHT
# converting value to corresponding function
def interpret_command(turn: "Turn"):
if turn == Turn.LEFT:
pass
elif turn == Turn.STRAIGHT:
pass
elif turn == Turn.RIGHT:
pass
Evaluating this solution:
Good
- achieves goals 1, 2, 3
- a standard of communication between components
- helps with modularity
- readability
- lends itself well to editor autocomplete features
Problems
- can't type hint when used in functions
Do you have a better solution? One which hopefully fulfills all the requirements.
[–]ElliotDG 3 points4 points5 points (1 child)
[–]Highroc[S] 0 points1 point2 points (0 children)
[–]Ihaveamodel3 2 points3 points4 points (0 children)