关于python:11个案例讲透-Python-函数参数

7次阅读

共计 3491 个字符,预计需要花费 9 分钟才能阅读完成。

明天给大家分享一下本人整顿的一篇 Python 参数的内容,内容十分的干,全文通过案例的模式来了解知识点,自认为比网上 80% 的文章讲的都要明确,如果你是入门不久的 python 老手,置信本篇文章应该对你会有不小的帮忙。

接下来是注释。

1. 参数分类
函数,在定义的时候,能够有参数的,也能够没有参数。

从函数定义的角度来看,参数能够分为两种:

  • 必选参数:调用函数时必须要指定的参数,在定义时没有等号
  • 可选参数:也叫默认参数,调用函数时能够指定也能够不指定,不指定就默认的参数值来。

例如上面的代码中,a 和 b 属于必选参数,c 和 d 属于可选参数

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

从函数调用的角度来看,参数能够分为两种:

  • 关键字参数:调用时,应用 key=value 模式传参的,这样传递参数就能够不按定义程序来。
  • 地位参数:调用时,不应用关键字参数的 key-value 模式传参,这样传参要留神依照函数定义时参数的程序来。
def func(a,b,c=0, d=1):
    pass

  # 关键字参数传参办法
func(a=10, c=30, b=20, d=40)

  # 地位参数传参办法
func(10, 20, 30, 40)

最初还有一种十分非凡的参数,叫做可变参数。

意思是参数个数可变,能够是 0 个或者任意个,然而传参时不能指定参数名,通常应用 args 和 *kw 来示意:

  • *args:接管到的所有依照地位参数形式传递进来的参数,是一个元组类型
  • **kw:接管到的所有依照关键字参数形式传递进来的参数,是一个字典类型
def func(*args, **kw):
    print(args)
    print(kw)

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

输入如下

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

2. 十一个案例
案例一:在上面这个函数中,a 是必选参数,是必须要指定的

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

案例二:在上面这个函数中,b 是可选参数(默认参数),能够指定也能够不指定,不指定的话,默认为 10

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

案例三:在上面这个函数中,name 和 age 都是必选参数,在调用指定参数时,如果不应用关键字参数形式传参,须要留神程序

>>> def print_profile(name, age):
...     return f"我的名字叫 {name},往年{age} 岁了"
...
>>> print_profile("iswbm", 27)
'我的名字叫 iswbm,往年 27 岁了'
如果参数太多,你不想太花精力去留神程序,能够应用关键字参数形式传参,在指定参数时附上参数名,比方这样:>>> print_profile(age=27, name="iswbm")
'我的名字叫 iswbm,往年 27 岁了'

案例四:在上面这个函数中,args 参数和下面的参数名不太一样,在它后面有一个 *,这就表明了它是一个可变参数,能够接管任意个数的不指定参数名的参数。

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

案例五:在上面这个函数中,kw 参数和下面的 args 还多了一个 ,总共两个 **,这个意思是 kw 是一个可变关键字参数,能够接管任意个数的带参数名的参数。

>>> def demo_func(**kw):
...     print(kw)
... 
>>> demo_func(a=10, b=20, c=30)
{'a': 10, 'b': 20, 'c': 30}

案例六:在定义时,必选参数肯定要在可选参数的后面,不然运行时会报错

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

案例七:在定义时,可变地位参数肯定要在可变关键字参数后面,不然运行时也会报错

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

案例八:可变地位参数能够放在必选参数后面,然而在调用时,必选参数必须要指定参数名来传入,否则会报错

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

案例九:可变关键字参数则不一样,可变关键字参数肯定得放在最初,上面三个示例中,不论关键字参数前面接地位参数,还是默认参数,还是可变参数,都会报错。

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

案例十:将下面的知识点串起来,四种参数类型能够在一个函数中呈现,但肯定要留神程序

def demo_func(arg1, arg2=10, *args, **kw):
    print("arg1:", arg1)
    print("arg2:", arg2)
    print("args:", args)
    print("kw:", kw)

试着调用这个函数,输入如下:

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

案例十一:应用独自的 *,当你在给前面的地位参数传递时,对你传参的形式有严格要求,你在传参时必须要以关键字参数的形式传参数,要写参数名,不然会报错。

>>> 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. 传参的坑
函数参数传递的是理论对象的内存地址。如果参数是援用类型的数据类型(列表、字典等),在函数外部批改后,就算没有把批改后的值返回回去,里面的值其实也曾经产生了变动。

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

最近整顿了几百 G 的 Python 学习材料,蕴含新手入门电子书、教程、源码等等,收费分享给大家!想要的返回“Python 编程学习圈”,发送“J”即可收费取得

正文完
 0