[問題]python用多個變數或用**kwargs

看板Python作者 (jacobcan118)時間8年前 (2017/05/10 21:28), 編輯推噓1(102)
留言3則, 3人參與, 最新討論串1/1
請問如果我想寫一段從funcA傳值到func函式, 可以接收兩種input, 依照input不同取不同值, 下面哪一種寫法會比較好? 方法一 def func_mind(**kwargs): func(**kwargs) def func(**kwargs): l = {'typea': '//div', 'typeb': '//a'} if 'typea' in kwargs: val = '{}[{}]'.formate(l['typea'], kwargs['typea']) elif 'typeb' in kwargs: val = '{}[{}]'.formate(l['typeb'], kwargs['typeb']) driver.find_element_by_xpath(val) func_mind(typea=4) func_mind(typeb=4) 方法二 def func_mind(type, val): func(type, val) def func(type, val): lo = {'typea': '//div', 'typeb': '//a'} val = '{}[{}]'.formate(lo[type], val) driver.find_element_by_xpath(val) func_mind(typea, 4) func_mind(typeb, 7777777) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 209.90.32.81 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1494422930.A.C5D.html

05/10 21:41, , 1F
Singledispatch
05/10 21:41, 1F

05/11 10:31, , 2F
我會用1 然後for typeTerm in l: 而不是if-elif
05/11 10:31, 2F

05/11 10:37, , 3F
這介面太 magic 了, 直接傳 //div 和 //a 進去更直接
05/11 10:37, 3F
文章代碼(AID): #1P4nMInT (Python)