Python函数定义、函数参数定义、函数相关变量知识点总结

函数

函数的定义

1
2
3
def 函数名([输入参数]):
函数体
[return xxx]

上述输入参数表示的是形式参数,简称形参

调用函数的调用者传入的数值,称为实际参数,简称实参。实参的位置是函数的调用处。

函数的参数传递

位置实参

根据形参对应的位置进行实参传递

1
2
3
4
5
6
7
# 自定义函数
def calc(x, y):
print('x=', x, 'y=', y)
return x + y

# 位置实参
print(calc(10, 20))

关键字实参

根据形参名称进行实参传递

1
2
3
4
5
6
7
# 自定义函数
def calc(x, y):
print('x=', x, 'y=', y)
return x + y

# 关键字实参
print(calc(y=40, x=30))

函数的参数传递——可变实参与不可变实参

1
2
3
4
5
6
7
8
9
10
11
12
13
# 自定义函数
def fun(arg1, arg2):
arg1 = 10
arg2.append(10)
return arg1, arg2

a = 20
b = [1, 2, 3, 4]

print(fun(a, b)) # 输出(10, [1, 2, 3, 4, 10])

print(a) # 输出20
print(b) # 输出[1, 2, 3, 4, 10]

上述代码中,fun() 函数中的形参 arg1 的作用域只在函数体内,fun() 函数中的形参 arg2 是数组的地址,因此操作数组地址对应的数组内容,则影响所有使用到该数组地址的地方。

使用图解描述上述的现象:

函数的返回值

  1. 如果函数没有返回值,return 可以省略不写。如果调用者一定要接受没有返回值的函数,则返回None

  2. 函数的返回值,如果是 1 个,直接返回类型。

  3. 函数的返回值,如果是多个,返回的数据类型为元组。

无返回值,示例:

1
2
3
4
5
6
# 自定义函数
def fun(arg1, arg2):
print(arg1, arg2)


print(fun(10, 20)) # 没有返回值的函数,返回 None

只有一个返回值,示例:

1
2
3
4
5
6
7
# 自定义函数
def fun(arg1, arg2):
list1 = [arg1, arg2]
return list1

print(fun(10, 20)) # 输出 [10, 20]
print(type(fun(10, 20))) # 输出 <class 'list'>

多个返回值,示例:

1
2
3
4
5
6
# 自定义函数
def fun(arg1, arg2):
return arg1, arg2

print(fun(10, 20)) # 输出 (10, 20)
print(type(fun(10, 20))) # 输出 <class 'tuple'>

函数参数

位置参数

1
2
3
4
5
6
7
8
9
def power(x, n):
s = 1
while n > 0:
n = n - 1
s = s * x
return s


print(power(3, 3)) # 输出 27

默认参数

1
2
3
4
5
6
7
8
9
def power(x, n=2):
s = 1
while n > 0:
n = n - 1
s = s * x
return s


print(power(3)) # 输出 9

设置默认参数时,有几点要注意:

  • 必选参数在前,默认参数在后,否则Python的解释器会报错。

  • 如何设置默认参数:当函数有多个参数时,把变化大的参数放前面,变化小的参数放后面。变化小的参数就可以作为默认参数。

个数可变的位置参数

  • 定义函数时,可能无法事先确定传递的位置实参的个数时,使用可变的位置参数

  • 使用*定义个数可变的位置形参

  • 结果为一个元组

1
2
3
4
5
6
7
8
9
def fun(*args):
print(args)
print(type(args))


fun(1, 2, 3)
# 输出
# (1, 2, 3)
# <class 'tuple'>

如果想把 list 中每一个元素传入 fun() 函数中,则需要在 list 变量之前加*

1
2
3
4
5
6
7
8
9
10
def fun(*args):
print(args)
print(type(args))


list1 = [4, 5, 6, 7]
fun(*list1)
# 输出
# (4, 5, 6, 7)
# <class 'tuple'>

个数可变的关键字参数

  • 定义函数时,可能无法事先确定传递的关键字实参的个数时,使用可变的关键字参数
  • 使用**定义个数可变的关键字形参
  • 结果为一个字典
1
2
3
4
5
6
7
8
9
def fun(**kw):
print(kw)
print(type(kw))


fun(a=1, b=2, c=3)
# 输出
# {'a': 1, 'b': 2, 'c': 3}
# <class 'dict'>

如果想把 dict 中每一个元素传入 fun() 函数中,则需要在 dict 变量之前加**

1
2
3
4
5
6
7
8
9
10
def fun(**kw):
print(kw)
print(type(kw))


dict1 = {'x': 1, 'y': 2, 'z': 3}
fun(**dict1)
# 输出
# {'a': 1, 'b': 2, 'c': 3}
# <class 'dict'>

命名关键字参数

命名关键字参数需要一个特殊分隔符**后面的参数被视为命名关键字参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
def fun(x, y, *, name, age):
print('x', x)
print('y', y)
print('name=', name)
print('age=', age)


'''
fun(10, 20, 'woodwhales', 40)
上述这种调用方式会报错,因为 fun() 函数的最后俩个参数指定了 name 和 age
'''
fun(10, 20, name='woodwhales', age=20)
fun(10, 20, age=20, name='woodwhales')

如果函数定义中已经有了一个可变参数,后面跟着的命名关键字参数就不再需要一个特殊分隔符*了:

1
2
def person(name, age, *args, city, job):
print(name, age, args, city, job)

多种参数的组合

在 Python 中定义函数,可以用必选参数、默认参数、可变参数、关键字参数和命名关键字参数,这5种参数都可以组合使用。但是请注意,参数定义的顺序必须是:必选参数、默认参数、可变参数、命名关键字参数和关键字参数。

比如定义一个函数,包含上述若干种参数:

1
2
3
4
5
def f1(a, b, c=0, *args, **kw):
print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)

def f2(a, b, c=0, *, d, **kw):
print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)

变量的作用域

函数体外的变量

函数体之外的变量可以作用于函数体内外:

1
2
3
4
5
6
7
name = 'woodwhales.cn'


def fun():
print('name=', name)

fun() # 输出 name= woodwhales.cn

函数体内的变量

函数体内的变量只作用于函数体内,称为局部变量。如果想让函数体内的变量能在函数体之外被使用到,则需要给局部变量前面加global

1
2
3
4
5
6
def fun():
global name
name = 'woodwhales'

fun() # 注意想使用全局变量时,需要调用一次函数
print(name) # 输出 woodwhales
updated updated 2024-01-05 2024-01-05
本文结束感谢阅读

本文标题:Python函数定义、函数参数定义、函数相关变量知识点总结

本文作者:

微信公号:木鲸鱼 | woodwhales

原始链接:https://woodwhales.cn/2021/02/16/079/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%