欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

十一個(gè)案例帶你吃透Python函數(shù)參數(shù)

 更新時(shí)間:2022年08月07日 16:04:34   作者:我愛(ài)Python數(shù)據(jù)挖掘  
這篇文章主要通過(guò)十一個(gè)案例帶大家一起了解一下Python中的函數(shù)參數(shù),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下

大家好,今天給大家分享一下自己整理的一篇 Python 參數(shù)的內(nèi)容,內(nèi)容非常的干,全文通過(guò)案例的形式來(lái)理解知識(shí)點(diǎn),自認(rèn)為比網(wǎng)上 80% 的文章講的都要明白,如果你是入門不久的 python 新手,相信本篇文章應(yīng)該對(duì)你會(huì)有不小的幫助。

1. 參數(shù)分類

函數(shù),在定義的時(shí)候,可以有參數(shù)的,也可以沒(méi)有參數(shù)。

從函數(shù)定義的角度來(lái)看,參數(shù)可以分為兩種:

必選參數(shù):調(diào)用函數(shù)時(shí)必須要指定的參數(shù),在定義時(shí)沒(méi)有等號(hào)

可選參數(shù):也叫默認(rèn)參數(shù),調(diào)用函數(shù)時(shí)可以指定也可以不指定,不指定就默認(rèn)的參數(shù)值來(lái)。

例如下面的代碼中,a 和 b 屬于必選參數(shù), c 和 d 屬于可選參數(shù)

def func(a,b,c=0, d=1):
    pass

從函數(shù)調(diào)用的角度來(lái)看,參數(shù)可以分為兩種:

關(guān)鍵字參數(shù):調(diào)用時(shí),使用 key=value 形式傳參的,這樣傳遞參數(shù)就可以不按定義順序來(lái)。

位置參數(shù):調(diào)用時(shí),不使用關(guān)鍵字參數(shù)的 key-value 形式傳參,這樣傳參要注意按照函數(shù)定義時(shí)參數(shù)的順序來(lái)。

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ù)個(gè)數(shù)可變,可以是 0 個(gè)或者任意個(gè),但是傳參時(shí)不能指定參數(shù)名,通常使用 *args 和 **kw 來(lái)表示:

*args:接收到的所有按照位置參數(shù)方式傳遞進(jìn)來(lái)的參數(shù),是一個(gè)元組類型

**kw :接收到的所有按照關(guān)鍵字參數(shù)方式傳遞進(jìn)來(lái)的參數(shù),是一個(gè)字典類型

def func(*args, **kw):
    print(args)
    print(kw)

func(10, 20, c=20, d=40)

輸出如下

(10, 20)
{'c': 20, 'd': 40}

2. 十一個(gè)案例

案例一:在下面這個(gè)函數(shù)中, a 是必選參數(shù),是必須要指定的

>>> def demo_func(a):
...     print(a)
... 
>>> demo_func(10) 
10
>>> demo_func()  # 不指定會(huì)報(bào)錯(cuò)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: demo_func() missing 1 required positional argument: 'a'

案例二:在下面這個(gè)函數(shù)中,b 是可選參數(shù)(默認(rèn)參數(shù)),可以指定也可以不指定,不指定的話,默認(rèn)為10

>>> def demo_func(b=10):
...     print(b)
... 
>>> demo_func(20)
20
>>> demo_func()
10

案例三:在下面這個(gè)函數(shù)中, name 和 age 都是必選參數(shù),在調(diào)用指定參數(shù)時(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í)附上參數(shù)名,比如這樣:

>>> print_profile(age=27, name="iswbm")
'我的名字叫iswbm,今年27歲了'

案例四:在下面這個(gè)函數(shù)中,args 參數(shù)和上面的參數(shù)名不太一樣,在它前面有一個(gè) *,這就表明了它是一個(gè)可變參數(shù),可以接收任意個(gè)數(shù)的不指定參數(shù)名的參數(shù)。

>>> def demo_func(*args):
...     print(args)
... 
>>> 
>>> demo_func(10, 20, 30)
(10, 20, 30)

案例五:在下面這個(gè)函數(shù)中,kw 參數(shù)和上面的 *args 還多了一個(gè) * ,總共兩個(gè) ** ,這個(gè)意思是 kw 是一個(gè)可變關(guān)鍵字參數(shù),可以接收任意個(gè)數(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ù)一定要在可選參數(shù)的前面,不然運(yùn)行時(shí)會(huì)報(bào)錯(cuò)

>>> 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í),可變位置參數(shù)一定要在可變關(guān)鍵字參數(shù)前面,不然運(yùn)行時(shí)也會(huì)報(bào)錯(cuò)

>>> 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ù)必須要指定參數(shù)名來(lái)傳入,否則會(huì)報(bào)錯(cuò)

>>> 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ù)一定得放在最后,下面三個(gè)示例中,不管關(guān)鍵字參數(shù)后面接位置參數(shù),還是默認(rèn)參數(shù),還是可變參數(shù),都會(huì)報(bào)錯(cuò)。

>>> 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í)點(diǎn)串起來(lái),四種參數(shù)類型可以在一個(gè)函數(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)用這個(gè)函數(shù),輸出如下:

>>> demo_func(1,12, 100, 200, d=1000, e=2000)
arg1:  1
arg2:  12
args:  (100, 200)
kw:  {'d': 1000, 'e': 2000}

案例十一:使用單獨(dú)的 *,當(dāng)你在給后面的位置參數(shù)傳遞時(shí),對(duì)你傳參的方式有嚴(yán)格要求,你在傳參時(shí)必須要以關(guān)鍵字參數(shù)的方式傳參數(shù),要寫參數(shù)名,不然會(huì)報(bào)錯(cuò)。

>>> 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)

3. 傳參的坑

函數(shù)參數(shù)傳遞的是實(shí)際對(duì)象的內(nèi)存地址。如果參數(shù)是引用類型的數(shù)據(jù)類型(列表、字典等),在函數(shù)內(nèi)部修改后,就算沒(méi)有把修改后的值返回回去,外面的值其實(shí)也已經(jīng)發(fā)生了變化。

>>> def add_item(item, source_list):
...     source_list.append(item)
...
>>> alist = [0,1]
>>> add_item(2, alist)
>>> alist
[0, 1, 2]

以上就是十一個(gè)案例帶你吃透Python函數(shù)參數(shù)的詳細(xì)內(nèi)容,更多關(guān)于Python函數(shù)參數(shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論