[python] Manage functions in a dictionary (command table, function table, function pointer)

Introduction

★ I referred to this! ★

Note that @shiracamus told me that when the number of functions increases, it will be easier to manage if you put the functions in a dictionary and make them commands.

https://qiita.com/mochihisa/items/2bb433636c4c615b0961#comment-f5565d1700921d4edc9f

How to do

Give the function pointer a name like a function table.

You can see the convenience compared to this article (self-deprecation) Manage functions in a list

├── modules
│   └──logic.py
│ 
└─ main.py

modules/logic.py


def func1(word):
    print('Here func1' + word)

def func2(word):
    print('Here func2' + word)

def func3(word):
    print('Here func3' + word)

def func4(word):
    print('Here func4' + word)

def func5(word):
    print('Here func5' + word)

def help_command():
    print("""\
func1 ----Call func1
func2 ----Call func2
func3 ----Call func3
func4 ----Call func4
func5 ----Call func5
    """)

COMMANDS = {
    "func1": func1,
    "func2": func2,
    "func3": func3,
    "func4": func4,
    "func5": func5,
}

main.py


import modules.logic as logic


logic.help_command()
logic.COMMANDS['func1']('is')
logic.COMMANDS['func2']('Yade')
logic.COMMANDS['func3']('That'sright')
logic.COMMANDS['func4']('Nyoro')
logic.COMMANDS['func5']('Daje')

Execution result

$ python main.py

func1 ----Call func1
func2 ----Call func2
func3 ----Call func3
func4 ----Call func4
func5 ----Call func5
    
This is func1
This is func2
This is func3
This is func4
This is func5

Recommended Posts

[python] Manage functions in a dictionary (command table, function table, function pointer)
[python] Manage functions in a list
Create a function in Python
Create a dictionary in Python
Hit a command in Python (Windows)
[Python] Execution time when a function is entered in a dictionary value
Precautions when pickling a function in python
Hash in Perl is a dictionary in Python
Draw a graph of a quadratic function in Python
To execute a Python enumerate function in JavaScript
Get the caller of a function in Python
[Memo] I tried a pivot table in Python
Specify a subcommand as a command line argument in Python
Receive dictionary data from a Python program in AppleScript
A note on handling variables in Python recursive functions
How to execute a command using subprocess in Python
I implemented a Vim-like replacement command in Slackbot #Python
What does the last () in a function mean in Python?
Take a screenshot in Python
A general-purpose program that formats Linux command strings in python
A function that divides iterable into N pieces in Python
Use callback function in Python
ntile (decile) function in python
Make a table of multiplication of each element in a spreadsheet (Python)
Execute external command in python
I made a command to generate a table comment in Django
Overriding library functions in Python
Make a bookmarklet in Python
To return char * in a callback function using ctypes in Python
Nonlinear function modeling in Python
Draw implicit function in python
Immediate function in python (lie)
A memo of writing a basic function in Python using recursion
Avoid KeyError in python dictionary
Introducing a good way to manage DB connections in Python
External command execution in Python
Draw a heart in Python
Try running a function written in Python using Fn Project
Python functions learned in chemoinformatics
Post a message from IBM Cloud Functions to Slack in Python
How to get a string from a command line argument in python
I wrote a function to load a Git extension script in Python
A function that measures the processing time of a method in python
[Python] What is a zip function?
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
Call a Python function from p5.js.
Implement R's power.prop.test function in python
Function argument type definition in python
Included notation in Python function arguments
Create a DI Container in Python
Draw a scatterplot matrix in python
Write AWS Lambda function in Python
Write A * (A-star) algorithm in Python
Measure function execution time in Python
In the python command python points to python3.8
Create a binary file in Python
[Python] Make the function a lambda function
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python