[問題]python用多個變數或用**kwargs
請問如果我想寫一段從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
05/10 21:41, 1F
推
05/11 10:31, , 2F
05/11 10:31, 2F
→
05/11 10:37, , 3F
05/11 10:37, 3F