Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
5
Help us understand the problem. What is going on with this article?
@osorezugoing

[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だじぇ

ひとこと

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

5
Help us understand the problem. What is going on with this article?
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
osorezugoing
れっつごー

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
5
Help us understand the problem. What is going on with this article?