作者jacobcan118 (jacobcan118)
看板Python
標題[問題]python用多個變數或用**kwargs
時間Wed May 10 21:28:47 2017
請問如果我想寫一段從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
→ zerof: Singledispatch 05/10 21:41
推 Yshuan: 我會用1 然後for typeTerm in l: 而不是if-elif 05/11 10:31
→ uranusjr: 這介面太 magic 了, 直接傳 //div 和 //a 進去更直接 05/11 10:37