Help us understand the problem. What is going on with this article?

[python]関数をリストに入れて管理する

はじめに

moduleに複数のロジックがあって、それらをある関数から呼び出したいということはあると思います。

やり方

構成

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

コード

modules/logic.py
def func1(word):
    print('こちらfunc1' + word)

def func2(word):
    print('こちらfunc2' + word)   

def func3(word):
    print('こちらfunc3' + word) 

def func4(word):
    print('こちらfunc4' + word) 

def func5(word):
    print('こちら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,'です')
    func(2,'やで')
    func(3,'だよ')
    func(4,'にょろ')
    func(5,'だじぇ')

実行と結果

$ python main.py

こちらfunc1です
こちらfunc2やで
こちらfunc3だよ
こちらfunc4にょろ
こちらfunc5だじぇ

ひとこと

もっと良い書き方ありましたら教えてください。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした