I want to do something like that:
action = :default
if some_condition1:
action = :do_something
if some_condition2:
action = :do_other_thing
...
if action == :default:
one_reaction()
elif action == :do_something:
other_reaction()
...
What should I use to represent actions choice?
Variants that come to mind:
-
Create enumeration
class MyActions: DEFAULT=1 SO_SOMETHING=2 SO_OTHER_THING=3inconvenient, need to scroll to and fro and “register” new action choices.
-
Use magic numbers. Not good.
- Use strings.
“Symbols” in Lisp can be thought of, conceptually, as interned strings.
Whenever you would use a symbol in Lisp, Python would use a string. Lisp has to make a distinction because it is homoiconic, but since Python isn’t homoiconic, the distinction is unnecessary.
Alternatives
If you use numbers instead, then you lose REPL-ability. For example,
There are more sophisticated ways to do this, like creating special classes, but the small benefit is not worth the added complexity.