LoginSignup
2
5

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

Last updated at Posted at 2020-02-22

#はじめに
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だじぇ

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

2
5
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5