[問題] 變數設定問題

看板Python作者 (呆呆)時間10年前 (2015/12/15 02:42), 編輯推噓2(202)
留言4則, 3人參與, 最新討論串1/1
想請教各位高手 小弟初學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

12/15 08:31, , 1F
你的class繼承自K的type,再自寫三個method!然後input完
12/15 08:31, 1F

12/15 08:31, , 2F
轉型k到自定義class再呼叫method!
12/15 08:31, 2F

12/15 10:38, , 3F
d={"A":A, "B":B, "C":C} d[K].method1()
12/15 10:38, 3F

12/15 15:09, , 4F
謝謝你們的建議,解決問題了 !!!
12/15 15:09, 4F
文章代碼(AID): #1MRmsAg- (Python)
文章代碼(AID): #1MRmsAg- (Python)