[python] Manage functions in a list

Introduction

I think you may have multiple logics in a module and want to call them from a function.

reference

I think you should read this!

manner

Constitution

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

code

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) 

main.py


import modules.logic as logic


def func(func_num, word):
    a = [_,
         logic.test1,
         logic.test2,
         logic.test3,
         logic.test4,
         logic.test5]
    return a[func_num](word

if __name__ == '__main__'
    func(1,'is')
    func(2,'Yade')
    func(3,'That's right')
    func(4,'Nyoro')
    func(5,'Daje')

Execution and results

$ python main.py

This is func1
This is func2
This is func3
This is func4
This is func5

one thing

Please let me know if there is a better way to write it.

Recommended Posts

[python] Manage functions in a list
[python] Manage functions in a dictionary (command table, function table, function pointer)
Display a list of alphabets in Python 3
Sorted list in Python
Filter List in Python
List find in Python
How to clear tuples in a list (Python)
Make a copy of the list in Python
A note on optimizing blackbox functions in Python
View drug reviews using a list in Python
Sort list elements in a specified order in Python
[Python] Manipulating elements in a list (array) [Sort]
Take a screenshot in Python
Create a function in Python
Create a dictionary in Python
Overriding library functions in Python
Python list is not a list
Make a bookmarklet in Python
Draw a heart in Python
Getting list elements in Python
Python functions learned in chemoinformatics
Things to note when initializing a list in Python
A note on handling variables in Python recursive functions
Group by consecutive elements of a list in Python
Extract multiple list duplicates in Python
Write a binary search in Python
Difference between list () and [] in Python
Output 2017 Premium Friday list in Python
Hit a command in Python (Windows)
Create a list in Python with all followers on twitter
Create a DI Container in Python
Draw a scatterplot matrix in python
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
Get the number of specific elements in a python list
Python functions
Developed a library to get Kindle collection list in Python
Create a binary file in Python
python / Make a dict from a list.
Using global variables in python functions
Solve ABC036 A ~ C in Python
How to get the last (last) value in a list in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
How to get a list of built-in exceptions in python
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
[Python] list
Dynamically define functions (methods) in Python
Introducing a good way to manage DB connections in Python
Solve ABC037 A ~ C in Python
Delete multiple elements in python list
Run a simple algorithm in Python
Draw a CNN diagram in Python
When creating a matrix in a list
Create a random string in Python
Schedule a Zoom meeting in Python
When writing a program in Python
Try to get a list of breaking news threads in Python.
Post a message from IBM Cloud Functions to Slack in Python