作者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://webptt.com/cn.aspx?n=bbs/Python/M.1494422930.A.C5D.html
1F:→ zerof: Singledispatch 05/10 21:41
2F:推 Yshuan: 我会用1 然後for typeTerm in l: 而不是if-elif 05/11 10:31
3F:→ uranusjr: 这介面太 magic 了, 直接传 //div 和 //a 进去更直接 05/11 10:37