Python?函數(shù)參數(shù)11個案例分享
大家好,今天給大家分享一下明哥整理的一篇 Python 參數(shù)的內(nèi)容,內(nèi)容非常的干,全文通過案例的形式來理解知識點,自認(rèn)為比網(wǎng)上 80% 的文章講的都要明白,如果你是入門不久的 python 新手,相信本篇文章應(yīng)該對你會有不小的幫助。
接下來是正文。
1. 參數(shù)分類
函數(shù),在定義的時候,可以有參數(shù)的,也可以沒有參數(shù)。
從函數(shù)定義的角度來看,參數(shù)可以分為兩種:
必選參數(shù):調(diào)用函數(shù)時必須要指定的參數(shù),在定義時沒有等號可選參數(shù):也叫默認(rèn)參數(shù),調(diào)用函數(shù)時可以指定也可以不指定,不指定就默認(rèn)的參數(shù)值來。
例如下面的代碼中,a 和 b 屬于必選參數(shù), c 和 d 屬于可選參數(shù)
def func(a,b,c=0, d=1):
pass 從函數(shù)調(diào)用的角度來看,參數(shù)可以分為兩種:
關(guān)鍵字參數(shù):調(diào)用時,使用 key=value 形式傳參的,這樣傳遞參數(shù)就可以不按定義順序來。位置參數(shù):調(diào)用時,不使用關(guān)鍵字參數(shù)的 key-value 形式傳參,這樣傳參要注意按照函數(shù)定義時參數(shù)的順序來。
def func(a,b,c=0, d=1):
pass
# 關(guān)鍵字參數(shù)傳參方法
func(a=10, c=30, b=20, d=40)
# 位置參數(shù)傳參方法
func(10, 20, 30, 40) 最后還有一種非常特殊的參數(shù),叫做可變參數(shù)。
意思是參數(shù)個數(shù)可變,可以是 0 個或者任意個,但是傳參時不能指定參數(shù)名,通常使用 *args 和 **kw 來表示:
*args:接收到的所有按照位置參數(shù)方式傳遞進(jìn)來的參數(shù),是一個元組類型**kw:接收到的所有按照關(guān)鍵字參數(shù)方式傳遞進(jìn)來的參數(shù),是一個字典類型
def func(*args, **kw):
print(args)
print(kw)
func(10, 20, c=20, d=40) 輸出如下
(10, 20)
{'c': 20, 'd': 40}
2. 十一個案例
案例一:在下面這個函數(shù)中, a 是必選參數(shù),是必須要指定的
>>> def demo_func(a): ... print(a) ... >>> demo_func(10) 10 >>> demo_func() # 不指定會報錯 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: demo_func() missing 1 required positional argument: 'a'
案例二:在下面這個函數(shù)中,b 是可選參數(shù)(默認(rèn)參數(shù)),可以指定也可以不指定,不指定的話,默認(rèn)為10
>>> def demo_func(b=10): ... print(b) ... >>> demo_func(20) 20 >>> demo_func() 10
案例三:在下面這個函數(shù)中, name 和 age 都是必選參數(shù),在調(diào)用指定參數(shù)時,如果不使用關(guān)鍵字參數(shù)方式傳參,需要注意順序
>>> def print_profile(name, age):
... return f"我的名字叫{name},今年{age}歲了"
...
>>> print_profile("iswbm", 27)
'我的名字叫iswbm,今年27歲了' 如果參數(shù)太多,你不想太花精力去注意順序,可以使用關(guān)鍵字參數(shù)方式傳參,在指定參數(shù)時附上參數(shù)名,比如這樣:
>>> print_profile(age=27, name="iswbm") '我的名字叫iswbm,今年27歲了'
案例四:在下面這個函數(shù)中,args 參數(shù)和上面的參數(shù)名不太一樣,在它前面有一個 *,這就表明了它是一個可變參數(shù),可以接收任意個數(shù)的不指定參數(shù)名的參數(shù)。
>>> def demo_func(*args): ... print(args) ... >>> >>> demo_func(10, 20, 30) (10, 20, 30)
案例五:在下面這個函數(shù)中,kw 參數(shù)和上面的 *args 還多了一個 * ,總共兩個 ** ,這個意思是 kw 是一個可變關(guān)鍵字參數(shù),可以接收任意個數(shù)的帶參數(shù)名的參數(shù)。
>>> def demo_func(**kw):
... print(kw)
...
>>> demo_func(a=10, b=20, c=30)
{'a': 10, 'b': 20, 'c': 30} 案例六:在定義時,必選參數(shù)一定要在可選參數(shù)的前面,不然運行時會報錯
>>> def demo_func(a=1, b): ... print(a, b) ... File "<stdin>", line 1 SyntaxError: non-default argument follows default argument >>> >>> def demo_func(a, b=1): ... print(a, b) ... >>>
案例七:在定義時,可變位置參數(shù)一定要在可變關(guān)鍵字參數(shù)前面,不然運行時也會報錯
>>> def demo_func(**kw, *args):
File "<stdin>", line 1
def demo_func(**kw, *args):
^
SyntaxError: invalid syntax
>>>
>>> def demo_func(*args, **kw):
... print(args, kw)
...
>>> 案例八:可變位置參數(shù)可以放在必選參數(shù)前面,但是在調(diào)用時,必選參數(shù)必須要指定參數(shù)名來傳入,否則會報錯
>>> def demo_func(*args, b): ... print(args) ... print(b) ... >>> demo_func(1, 2, 100) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: demo_func() missing 1 required keyword-only argument: 'b' >>> >>> demo_func(1, 2, b=100) (1, 2) 100
案例九:可變關(guān)鍵字參數(shù)則不一樣,可變關(guān)鍵字參數(shù)一定得放在最后,下面三個示例中,不管關(guān)鍵字參數(shù)后面接位置參數(shù),還是默認(rèn)參數(shù),還是可變參數(shù),都會報錯。
>>> def demo_func(**kw, a):
File "<stdin>", line 1
def demo_func(**kw, a):
^
SyntaxError: invalid syntax
>>>
>>> def demo_func(**kw, a=1):
File "<stdin>", line 1
def demo_func(**kw, a=1):
^
SyntaxError: invalid syntax
>>>
>>> def demo_func(**kw, *args):
File "<stdin>", line 1
def demo_func(**kw, *args):
^
SyntaxError: invalid syntax 案例十:將上面的知識點串起來,四種參數(shù)類型可以在一個函數(shù)中出現(xiàn),但一定要注意順序
def demo_func(arg1, arg2=10, *args, **kw):
print("arg1: ", arg1)
print("arg2: ", arg2)
print("args: ", args)
print("kw: ", kw) 試著調(diào)用這個函數(shù),輸出如下:
>>> demo_func(1,12, 100, 200, d=1000, e=2000)
arg1: 1
arg2: 12
args: (100, 200)
kw: {'d': 1000, 'e': 2000}
案例十一:使用單獨的 *,當(dāng)你在給后面的位置參數(shù)傳遞時,對你傳參的方式有嚴(yán)格要求,你在傳參時必須要以關(guān)鍵字參數(shù)的方式傳參數(shù),要寫參數(shù)名,不然會報錯。
>>> def demo_func(a, b, *, c): ... print(a) ... print(b) ... print(c) ... >>> >>> demo_func(1, 2, 3) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: demo_func() takes 2 positional arguments but 3 were given >>> >>> demo_func(1, 2, c=3) 1 2 3
3. 傳參的坑
函數(shù)參數(shù)傳遞的是實際對象的內(nèi)存地址。如果參數(shù)是引用類型的數(shù)據(jù)類型(列表、字典等),在函數(shù)內(nèi)部修改后,就算沒有把修改后的值返回回去,外面的值其實也已經(jīng)發(fā)生了變化。
>>> def add_item(item, source_list): ... source_list.append(item) ... >>> alist = [0,1] >>> add_item(2, alist) >>> alist [0, 1, 2]
到此這篇關(guān)于Python 函數(shù)參數(shù)11個案例講透的文章就介紹到這了,更多相關(guān)Python 函數(shù)參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python+Tensorflow+CNN實現(xiàn)車牌識別的示例代碼
這篇文章主要介紹了Python+Tensorflow+CNN實現(xiàn)車牌識別的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
解決pycharm不能自動補(bǔ)全第三方庫的函數(shù)和屬性問題
這篇文章主要介紹了解決pycharm不能自動補(bǔ)全第三方庫的函數(shù)和屬性問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python使用pandas處理大數(shù)據(jù)節(jié)省內(nèi)存技巧(推薦)
這篇文章主要介紹了python使用pandas處理大數(shù)據(jù)節(jié)省內(nèi)存技巧,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
使用EduBlock輕松學(xué)習(xí)Python編程
今天小編就為大家分享一篇關(guān)于使用EduBlock輕松學(xué)習(xí)Python編程的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
淺談numpy 函數(shù)里面的axis參數(shù)的含義
這篇文章主要介紹了numpy 函數(shù)里面的axis參數(shù)的含義,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05

