看板 Python 關於我們 聯絡資訊
想請教各位高手 小弟初學Python遇到一個問題 我建立一個類別裡面定義數個方法 class test(object): def methond1(): .... def methond2(): .... A = test(A) B = test(B) C = test(C) 我想讓使用者輸入一個變數K,用來選擇A、B、C輸出結果 K = input("輸入A、B、C") 我天真以為可以直接使用: K.methond1() 來取代 A.methond1() B.methond1() C.mehtond1() 但失敗了,想請教是否有更好的解決辦法? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.184.24.148 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1450118538.A.ABE.html
mraaa: 你的class繼承自K的type,再自寫三個method!然後input完 12/15 08:31
mraaa: 轉型k到自定義class再呼叫method! 12/15 08:31
tiefblau: d={"A":A, "B":B, "C":C} d[K].method1() 12/15 10:38
northsky: 謝謝你們的建議,解決問題了 !!! 12/15 15:09