【Python】大神教你五分鐘搞清楚Python函數(shù)的參數(shù)!
靈活的參數(shù)
Python函數(shù)的形參種類多,且具有很高的靈活性。我們經(jīng)常遇到:位置參數(shù)、關(guān)鍵詞參數(shù)、僅限關(guān)鍵字參數(shù)、僅限位置參數(shù)等等。如果概念沒徹底搞清楚,使用起來就不那么順手。
“提要:參數(shù)有 位置參數(shù) 和 關(guān)鍵字參數(shù) 兩大類,分別都可以設(shè)置 默認(rèn)值 ,也都可以設(shè)置 可變參數(shù)收集器 來分別收集兩者。Python3新增了 僅限關(guān)鍵字參數(shù) ,Python3.8新增了 僅限位置參數(shù) 。
”
上圖概括了Python中的參數(shù)及其位置,這樣在寫程序的時(shí)候,我們能做到心中有數(shù)了。
本文以實(shí)際函數(shù)進(jìn)化為例,逐個(gè)分析以上參數(shù)類型,最后囊括所有參數(shù)類型,以檢驗(yàn)和加深理解。
現(xiàn)在,我們要編寫一個(gè)HTML標(biāo)簽的生成函數(shù)。其調(diào)用形式:html = tag('p','Hello')
可得html內(nèi)容為:<p>Hello</p>。
僅使用位置參數(shù)就能實(shí)現(xiàn)這個(gè)功能。
位置參數(shù)
位置參數(shù)有普通位置參數(shù)、帶默認(rèn)值位置參數(shù)和可變參數(shù)收集器。如下用到前兩者:
def tag(name,content='Nothing...'):
template = '<%s>%s</%s>'%(name,content,name)
return template
以上函數(shù)使用簡(jiǎn)單格式化字符串實(shí)現(xiàn)功能。其中name和content都是位置參數(shù),實(shí)際調(diào)用的時(shí)候,可以靈活傳入實(shí)參:
忽略帶默認(rèn)值的參數(shù) tag('p')順序傳入 tag('p','Hello')以命名方式亂序傳入 tag(content='Hello',name='p')元組拆包傳入?yún)?shù) tag(*('p','Hello'))字典展開傳入?yún)?shù) tag(**{'name':'p','content':'Hello'})
可變參數(shù)收集
繼續(xù),我們需要根據(jù)多個(gè)內(nèi)容,生成相同標(biāo)簽名的多個(gè)標(biāo)簽。例如html = tag('li','Line1','Line2','Line3')要得到html內(nèi)容為:<li >Line1</li><li >Line2</li><li >Line3</li>。
位置參數(shù)固定數(shù)目,而內(nèi)容數(shù)目多變。為了實(shí)現(xiàn)收集多出來的'Line2'和'Line3',我們需要在位置參數(shù)列表的末尾,使用*args這種形式,定義可變參數(shù)收集,來收集剩余位置參數(shù)。所謂剩余元素,即實(shí)參傳遞給普通位置參數(shù),覆蓋默認(rèn)值參數(shù)后的剩余參數(shù)。
def tag(name,content='Nothing...',*args):
template = '<%s>%s</%s>'%(name,content,name)
if args:
for arg in args:
template += '<%s>%s</%s>'%(name,arg,name)
return template
可變參數(shù)*args,收集name和content之外的剩余位置參數(shù)。
關(guān)鍵字參數(shù)
HTML標(biāo)簽可能需要許多屬性,比如<a alt='Test URL' href='www.X.com'>X website</a>中的alt和href屬性。這些屬性需要明確指明變量名和具體值,關(guān)鍵字參數(shù)非常合用。
在位置參數(shù)列表的最后,使用**kwargs來收集關(guān)鍵字參數(shù)。
def tag(name,content='Nothing...',*args,**kwargs):
attrs = {}
if kwargs:
attrs.update(kwargs)
attrs = ' '.join(['%s=\'%s\''%(k,v) for k,v in attrs.items()])
template = '<%s %s>%s</%s>'%(name,attrs,content,name)
if args:
for arg in args:
template += '<%s %s>%s</%s>'%(name,attrs,arg,name)
return template
其調(diào)用形式為:html = tag('a','Hello',alt='test',,得html內(nèi)容為:Hello`
僅限關(guān)鍵字參數(shù)
新的需求:HTML標(biāo)簽需要定義style、class屬性。而且是強(qiáng)制要求定義,在生成函數(shù)調(diào)用時(shí),必須指出這兩個(gè)屬性,否則報(bào)錯(cuò)。這樣的強(qiáng)制關(guān)鍵字參數(shù)的要求,用僅限關(guān)鍵字參數(shù)(keyword-only),非常符合要求。
僅限關(guān)鍵字參數(shù)定義在位置參數(shù)列表的末尾,關(guān)鍵字參數(shù)收集器之前。可以有默認(rèn)值。主要特性是強(qiáng)制要求調(diào)用者以關(guān)鍵字形式傳入?yún)?shù)。
其調(diào)用形式header = tag('p','Welcome to my website.',class_='p1',style='font-size:30'),調(diào)用中,明確指出了僅限關(guān)鍵字參數(shù)class_和style。函數(shù)定義如下,其使用attrs字典整合了兩個(gè)僅限關(guān)鍵字參數(shù)和關(guān)鍵字參數(shù)收集器的內(nèi)容:
def tag(name,content='Nothing...',*args,class_,style='color:000000',**kwargs):
attrs = {}
attrs['class'] = class_
attrs['style'] = style
if kwargs:
attrs.update(kwargs)
attrs = ' '.join(['%s=\'%s\''%(k,v) for k,v in attrs.items()])
template = '<%s %s>%s</%s>'%(name,attrs,content,name)
if args:
for arg in args:
template += '<%s %s>%s</%s>'%(name,attrs,arg,name)
return template
僅限位置參數(shù)
tag標(biāo)簽生成HTML中的前兩個(gè)參數(shù):name表示標(biāo)簽名,content表示標(biāo)簽內(nèi)容。我們不希望這個(gè)參數(shù)亂用,或者各種拆包、展開字典賦值。只希望它變成pow(2,3),divmod(14,3),這樣意義非常明確的用法。以使調(diào)用tag時(shí),明確第一個(gè)就是標(biāo)簽名,緊跟著標(biāo)簽內(nèi)容。
實(shí)現(xiàn)這個(gè)功能,Python3.8新增的僅限位置參數(shù)(positional_only)非常適合。在僅限位置參數(shù)后加上一個(gè)/就實(shí)現(xiàn)了之前的參數(shù)是僅限位置參數(shù)的功能。
如下:
def
tag(name,content='Nothing...',/,*args,class_,style='color:
000000',**kwargs):
調(diào)用的時(shí)候,必須指出前兩項(xiàng),而且只能是按照位置傳入,不能命名傳入、元組拆包,字典展開等。這樣的強(qiáng)制要求簡(jiǎn)化和規(guī)范了調(diào)用。
如下調(diào)用中,只有第一種符合僅限位置參數(shù)調(diào)用規(guī)范。
tag('p','content','content2',class_='flo') #可行
tag(name='p',content = 'content','content2',class_='flo')
#失敗
測(cè)試
使用如上述函數(shù),我們實(shí)現(xiàn)了生成HTML標(biāo)簽內(nèi)容的功能,整體測(cè)試如下:新建p,ul,li和a標(biāo)簽,封裝進(jìn)div,最后嵌入html中,生成index.html文件。
header = tag('p','Welcome to my website.',class_='p1',style='font-size:30')
intro = tag('p','gongqingkui M',class_='p1')
li = tag('li','Python Intro.','DIY CPU',class_='li1')
ul1 = tag('ul','Projects',li,class_='ul1')
homeURL = tag('a','X website',alt='Test URL',class_='a1',href='www.X.com',style='color:red')
main = tag('div',header,intro,ul1,homeURL,class_='main')
html = tag('html',tag('head','',class_=''),tag('body',main,class_=''),class_='')
with open('index.html','w',encoding='utf-8')as f:
f.write(html)
源代碼戳:https://gist.github.com/gongqingkui/5e4d1d8967a5eb47a1caee4eee3fc617
總結(jié)
參數(shù)有 位置參數(shù) 和 關(guān)鍵字參數(shù) 兩大類
分別都可以設(shè)置 默認(rèn)值
也都可以設(shè)置 可變參數(shù)收集器 來分別收集兩者
Python3新增了 僅限關(guān)鍵字參數(shù)
Python3.8新增了 僅限位置參數(shù)
參數(shù)形式總結(jié)如下,函數(shù)def fun(a,b=2,/,c,d=4,*args,e,f=6,**kwargs)中:
a 僅限位置參數(shù)
b 帶默認(rèn)值的僅限位置參數(shù)
c 位置參數(shù)
d 帶默認(rèn)值的位置參數(shù)
*args 位置參數(shù)收集器
e 僅限關(guān)鍵字參數(shù)
f 帶默認(rèn)值的僅限關(guān)鍵字參數(shù)
**kwargs 關(guān)鍵字參數(shù)收集器
往期精彩回顧
本站qq群851320808,加入微信群請(qǐng)掃碼:

