I wrote a simple command-line tool in Python. To simplify the logic, I use a dict
for storing the commands and handlers, something like this:
#!/usr/bin/env python
# some code here
DISPACHERS = {
"run": func_run
"list": func_list
}
# other code
The problem is, I have to put this piece of code after the function declarations, which is not the standard practice I have seen in other languages (i.e. constants in one block, variables after, and then the functions).
I could use strings for storing the function, and retrieve it using something like getattr(__main__, "func_run")
, so I can stick with my preference, but I wonder if that's a "proper" way.
Any idea?
=== Update ===
Since it's a simple python script that handles some automation tasks of another project, so it would be better to keep it in a single file (no other module) if possible.