函数的定义

在 Python 中,函数是一段可重复使用的代码块,它接受一些输入(也称为参数)并产生一些输出。函数可以通过 def 关键字来定义,语法如下:

1
2
3
4
def function_name(parameters):
  	"""This is a function"""
    # function body
    return value

其中,function_name 是函数的名称,parameters 是函数的参数列表,function body 是函数的主体部分,包括需要执行的代码和可能的返回语句,return value 是函数的返回值(如果有的话)。

下列代码创建一个可以输出限定数值内的斐波那契数列函数:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def fib(n):    # write Fibonacci series up to n
    """Print a Fibonacci series up to n."""
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a+b
    print()


fib(2000) # 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 159

定义 函数使用关键字 def,后跟函数名与括号内的形参列表。函数语句从下一行开始,并且必须缩进。

函数内的第一条语句是字符串时,该字符串就是文档字符串,也称为 docstring。利用文档字符串可以自动生成在线文档或打印版文档,还可以让开发者在浏览代码时直接查阅文档;Python 开发者最好养成在代码中加入文档字符串的好习惯。

函数在 执行 时使用函数局部变量符号表,所有函数变量赋值都存在局部符号表中;引用变量时,首先,在局部符号表里查找变量,然后,是外层函数局部符号表,再是全局符号表,最后是内置名称符号表。因此,尽管可以引用全局变量和外层函数的变量,但最好不要在函数内直接赋值(除非是 global 语句定义的全局变量,或 nonlocal 语句定义的外层函数变量)。

在调用函数时会将实际参数(实参)引入到被调用函数的局部符号表中;因此,实参是使用 按值调用 来传递的(其中的 始终是对象的 引用 而不是对象的值)。 1 当一个函数调用另外一个函数时,会为该调用创建一个新的局部符号表。

函数定义在当前符号表中把函数名与函数对象关联在一起。解释器把函数名指向的对象作为用户自定义函数。还可以使用其他名称指向同一个函数对象,并访问访该函数:

1
2
3
fib
f = fib
f(100) # 0 1 1 2 3 5 8 13 21 34 55 89

fib 不返回值,因此,其他语言不把它当作函数,而是当作过程。事实上,没有 return 语句的函数也返回值,只不过这个值比较是 None (是一个内置名称)。一般来说,解释器不会输出单独的返回值 None ,如需查看该值,可以使用 print()

1
2
fib(0)
print(fib(0))

函数参数

默认值参数

在 Python 中,函数参数可以具有默认值,这意味着如果在函数调用期间未提供该参数的值,则使用默认值。定义具有默认值的函数参数的语法如下:

1
2
3
def function_name(param1, param2=default_value):
    # function body
    return value

其中,param1 是必需的参数,它没有默认值;param2 是可选参数,如果未提供,则使用默认值 default_value。如果调用函数时提供了 param2 的值,则使用提供的值覆盖默认值。

以下是一个使用默认参数值的 Python 函数示例:

1
2
3
4
5
def greet(name, greeting="Hello"):
    print(f"{greeting}, {name}!")

greet("Alice")  # output: Hello, Alice!
greet("Bob", "Hi")  # output: Hi, Bob!

需要注意的是,当函数参数具有默认值时,应该将具有默认值的参数放在参数列表的末尾。这样可以使函数更加易于使用,并避免在调用函数时出现混淆。

以下是一个具有多个默认参数值的 Python 函数的示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def create_user(name, age=18, gender="male", email=None):
    user = {"name": name, "age": age, "gender": gender}
    if email:
        user["email"] = email
    return user

user1 = create_user("Alice")
user2 = create_user("Bob", 20)
user3 = create_user("Charlie", gender="female", email="[email protected]")

print(user1)  # output: {'name': 'Alice', 'age': 18, 'gender': 'male'}
print(user2)  # output: {'name': 'Bob', 'age': 20, 'gender': 'male'}
print(user3)  # output: {'name': 'Charlie', 'age': 18, 'gender': 'female', 'email': '[email protected]'}

关键字参数

kwarg=value 形式的关键字参数也可以用于调用函数。函数示例如下:

1
2
3
4
5
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
    print("-- This parrot wouldn't", action, end=' ')
    print("if you put", voltage, "volts through it.")
    print("-- Lovely plumage, the", type)
    print("-- It's", state, "!")

该函数接受一个必选参数(voltage)和三个可选参数(state, actiontype)。该函数可用下列方式调用:

1
2
3
4
5
6
parrot(1000)                                          # 1 positional argument
parrot(voltage=1000)                                  # 1 keyword argument
parrot(voltage=1000000, action='VOOOOOM')             # 2 keyword arguments
parrot(action='VOOOOOM', voltage=1000000)             # 2 keyword arguments
parrot('a million', 'bereft of life', 'jump')         # 3 positional arguments
parrot('a thousand', state='pushing up the daisies')  # 1 positional, 1 keyword

以下调用函数的方式都无效:

1
2
3
4
parrot()                     # required argument missing
parrot(voltage=5.0, 'dead')  # non-keyword argument after a keyword argument
parrot(110, voltage=220)     # duplicate value for the same argument
parrot(actor='John Cleese')  # unknown keyword argument

函数调用时,关键字参数必须跟在位置参数后面。所有传递的关键字参数都必须匹配一个函数接受的参数(比如,actor 不是函数 parrot 的有效参数),关键字参数的顺序并不重要。这也包括必选参数,(比如,parrot(voltage=1000) 也有效)。不能对同一个参数多次赋值,下面就是一个因此限制而失败的例子:

1
2
3
4
5
6
7
def function(a):
     pass

function(0, a=0)
#Traceback (most recent call last):
#  File "<stdin>", line 1, in <module>
#TypeError: function() got multiple values for argument 'a'

最后一个形参为 **name 形式时,接收一个字典,该字典包含与函数中已定义形参对应之外的所有关键字参数。**name 形参可以与 *name 形参(下一小节介绍)组合使用(*name 必须在 **name 前面), *name 形参接收一个元组,该元组包含形参列表之外的位置参数。例如,可以定义下面这样的函数:

1
2
3
4
5
6
7
8
def cheeseshop(kind, *arguments, **keywords):
    print("-- Do you have any", kind, "?")
    print("-- I'm sorry, we're all out of", kind)
    for arg in arguments:
        print(arg)
    print("-" * 40)
    for kw in keywords:
        print(kw, ":", keywords[kw])

该函数可以用如下方式调用:

1
2
3
4
5
cheeseshop("Limburger", "It's very runny, sir.",
           "It's really very, VERY runny, sir.",
           shopkeeper="Michael Palin",
           client="John Cleese",
           sketch="Cheese Shop Sketch")

输出结果如下:

1
2
3
4
5
6
7
8
-- Do you have any Limburger ?
-- I'm sorry, we're all out of Limburger
It's very runny, sir.
It's really very, VERY runny, sir.
----------------------------------------
shopkeeper : Michael Palin
client : John Cleese
sketch : Cheese Shop Sketch

注意,关键字参数在输出结果中的顺序与调用函数时的顺序一致。

特殊参数

默认情况下,参数可以按位置或显式关键字传递给 Python 函数。为了让代码易读、高效,最好限制参数的传递方式,这样,开发者只需查看函数定义,即可确定参数项是仅按位置、按位置或关键字,还是仅按关键字传递。

函数定义如下:

1
2
3
4
5
6
def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):
      -----------    ----------     ----------
        |             |                  |
        |        Positional or keyword   |
        |                                - Keyword only
         -- Positional only

/* 是可选的。这些符号表明形参如何把参数值传递给函数:位置、位置或关键字、关键字。关键字形参也叫作命名形参。

  • 位置或关键字参数。函数定义中未使用 /* 时,参数可以按位置或关键字传递给函数。

  • 仅位置参数。特定形参可以标记为 仅限位置仅限位置 时,形参的顺序很重要,且这些形参不能用关键字传递。仅限位置形参应放在 / (正斜杠)前。/ 用于在逻辑上分割仅限位置形参与其它形参。如果函数定义中没有 /,则表示没有仅限位置形参。/ 后可以是 位置或关键字仅限关键字 形参。

  • 仅限关键字参数。把形参标记为 仅限关键字,表明必须以关键字参数形式传递该形参,应在参数列表中第一个 仅限关键字 形参前添加 *

请看下面的函数定义示例,注意 /* 标记:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def standard_arg(arg):
     print(arg)

def pos_only_arg(arg, /):
     print(arg)

def kwd_only_arg(*, arg):
     print(arg)

def combined_example(pos_only, /, standard, *, kwd_only):
     print(pos_only, standard, kwd_only)

第一个函数定义 standard_arg 是最常见的形式,对调用方式没有任何限制,可以按位置也可以按关键字传递参数:

1
2
3
standard_arg(2) # 2

standard_arg(arg=2) # 2

第二个函数 pos_only_arg 的函数定义中有 /,仅限使用位置形参:

1
pos_only_arg(1) # 1

第三个函数 kwd_only_args 的函数定义通过 * 表明仅限关键字参数:

1
kwd_only_arg(arg=3) # 3

最后一个函数在同一个函数定义中,使用了全部三种调用惯例:

1
2
3
combined_example(1, 2, kwd_only=3) # 1 2 3

combined_example(1, standard=2, kwd_only=3) #1 2 3

下面的函数定义中,kwdsname 当作键,因此,可能与位置参数 name 产生潜在冲突:

1
2
def foo(name, **kwds):
    return 'name' in kwds

调用该函数不可能返回 True,因为关键字 'name' 总与第一个形参绑定。例如:

1
2
3
4
foo(1, **{'name': 2})
#Traceback (most recent call last):
#  File "<stdin>", line 1, in <module>
#TypeError: foo() got multiple values for argument 'name'

加上 / (仅限位置参数)后,就可以了。此时,函数定义把 name 当作位置参数,'name' 也可以作为关键字参数的键:

1
2
3
4
def foo(name, /, **kwds):
     return 'name' in kwds

foo(1, **{'name': 2}) # True

换句话说,仅限位置形参的名称可以在 **kwds 中使用,而不产生歧义。

任意实参列表

调用函数时,使用任意数量的实参是最少见的选项。这些实参包含在元组中。在可变数量的实参之前,可能有若干个普通参数:

1
2
def write_multiple_items(file, separator, *args):
    file.write(separator.join(args))

variadic 参数用于采集传递给函数的所有剩余参数,因此,它们通常在形参列表的末尾。*args 形参后的任何形式参数只能是仅限关键字参数,即只能用作关键字参数,不能用作位置参数:

1
2
3
4
5
def concat(*args, sep="/"):
    return sep.join(args)

concat("earth", "mars", "venus")
concat("earth", "mars", "venus", sep=".")

解包实参列表

函数调用要求独立的位置参数,但实参在列表或元组里时,要执行相反的操作。例如,内置的 range() 函数要求独立的 startstop 实参。如果这些参数不是独立的,则要在调用函数时,用 * 操作符把实参从列表或元组解包出来:

1
2
3
4
list(range(3, 6))            # normal call with separate arguments

args = [3, 6]
list(range(*args))            # call with arguments unpacked from a list

同样,字典可以用 ** 操作符传递关键字参数:

1
2
3
4
5
6
7
def parrot(voltage, state='a stiff', action='voom'):
    print("-- This parrot wouldn't", action, end=' ')
    print("if you put", voltage, "volts through it.", end=' ')
    print("E's", state, "!")

d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
parrot(**d)

Lambda 函数

Python 中还有一种特殊的函数称为 lambda 函数,它是一种匿名函数,用于创建简短的函数。以下是一个 lambda 函数示例:

1
2
3
add_numbers = lambda x, y: x + y
result = add_numbers(2, 3)
print(result)

在上面的示例中,我们使用 lambda 关键字创建了一个匿名函数 add_numbers,它接受两个参数 xy,并返回它们的和。我们可以使用 add_numbers(2, 3) 调用这个匿名函数,并将结果存储在变量 result 中。

函数注解

Python 函数注解是一种在函数定义中添加元数据的功能。这些注解可以用于指定函数参数和返回值的类型、参数的默认值、函数的文档字符串等信息。虽然注解并不会影响 Python 函数的行为,但它们可以提供有用的信息,使代码更加清晰易读。

Python 函数注解使用的语法是在函数定义的参数列表后面添加冒号和注解。以下是一个函数注解的示例:

1
2
def add_numbers(x: int, y: int) -> int:
    return x + y

在上面的示例中,我们使用注解指定了函数 add_numbers() 的两个参数 xy 的类型为 int,并指定函数返回值的类型也为 int。在函数体中,我们使用加法运算符将这两个参数相加,并返回结果。

函数注解还可以包含参数的默认值、参数的可变性和函数的文档字符串等信息。以下是一个包含这些信息的函数注解示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def greet(name: str = "World", *, times: int = 1) -> str:
    """
    Greet a person by name and optionally repeat the greeting multiple times.

    :param name: The name of the person to greet. Default is "World".
    :param times: The number of times to repeat the greeting. Default is 1.
    :return: A string containing the greeting message.
    """
    message = "Hello, " + name + "!\n" * times
    return message

在上面的示例中,我们使用注解指定了函数 greet() 的参数 nametimes 的类型为 strint,并指定了参数 name 的默认值为 "World",参数 times 的默认值为 1。我们还使用了函数的文档字符串来提供更详细的函数说明。

标注 以字典的形式存放在函数的 __annotations__ 属性中,并且不会影响函数的任何其他部分。 形参标注的定义方式是在形参名后加冒号,后面跟一个表达式,该表达式会被求值为标注的值。 返回值标注的定义方式是加组合符号 ->,后面跟一个表达式,该标注位于形参列表和表示 def 语句结束的冒号之间。 下面的示例有一个必须的参数,一个可选的关键字参数以及返回值都带有相应的标注:

1
2
3
4
5
6
def f(ham: str, eggs: str = 'eggs') -> str:
    print("Annotations:", f.__annotations__)
    print("Arguments:", ham, eggs)
    return ham + ' and ' + eggs

f('spam')