Python基础-函数参数

写在前面

如非特别说明,下文均基于Python3

摘要

本文详细介绍了函数的各种形参类型,包括位置参数,默认参数值,关键字参数,任意参数列表,强制关键字参数;也介绍了调用函数时传递实参的各种方式,包括位置实参,关键字实参以及使用*和**来解包序列和字典。

1. 概述

函数在一定程度上是为了重用而创建的。如果有一段非常优秀的代码段,实现了网络资源下载的功能,如果没有函数,将会在每次需要实现网络资源下载的地方复制该段代码。懒惰即美德,将这段代码抽象为函数,在需要使用的地方调用即可。

函数的使用有以下好处:

  • 增加代码的可读性。如在需要下载网络资源的地方调用函数:download(),可以通过名字读懂程序的目的;
  • 增加代码可重用性。相比复制大段代码,调用函数的可操作性无疑更强;
  • 增加可维护性。如果需要更改下载网络资源的实现,没有使用函数的情况下,不得不在每个实用下载功能的地方修改,使用了函数,只需要修改函数即可;
  • 减少犯错误的可能性。在复制代码的过程中,无疑会因为各种原因出现一些差错,而函数不会。

函数定义非常简单:

def func([formal_parameter1, ... formal_parameter1]):
statement

以上函数定义的作用是创建函数对象,并且在当前作用域创建名字func,指向函数对象,在可及该作用域范围内,可以使用名字func调用函数。定义函数时候参数列表中的名字是函数形参,调用函数用的参数是实参。

Python函数的参数十分强大,但相应也为这种强大付出了相对复杂的代价。

函数定义时,函数的形参可以有以下几种类型:

  • 位置参数 positional parameters,最常用的形参形式,位置比名字重要;
  • 默认参数值 default argument values,param_name = argu_value形式,为形参提供默认值,必须放置在位置参数之后;
  • 任意参数列表 arbitrary argument lists,*args形式,args以元组的形式接收未匹配的位置实参;
  • 关键字形参字典 keyword arguments, **kwargs形式,kwargs以字典的形式接收未匹配的关键字实参,关键字参数需在任意参数列表之后;
  • 强制关键字参数 keyword-only arguments,在任意参数列表之后(或者在单独的*之后),调用是只能使用关键字实参。

函数调用时,实参可以由以下方式传递:

  • 位置实参按照位置从左到右匹配,位置比名字重要;
  • 关键字实参,通过明确形参的名字为其指定实参值。调用时关键字实参必须在位置实参之后,且形参列表中要有与之匹配的关键字形参;
  • 解包列表/字典,使用*(sequence)从序列中解包位置实参,使用**(dict)的方式从字典中解包关键字实参。

当这些不同的形参组合在一起时,构成的函数参数列表将会相当复杂,始终牢记实参形参匹配是位置参数优先。而且,任意参数列表与关键字参数组合的形参列表,可以匹配任意方式的函数调用。

2. 位置参数

位置形参是最常见的形参类型,其中,位置比名字重要,因为在实参匹配是是按照位置来的:

# positional argument, name is not important, but order matters
def positional_argument(name, age):
print('name->type:%s, value:%s' % (type(name), name))
print('age->type:%s, value:%s' % (type(age), age))

调用时,如果改变实参位置,意义完全不同:

positional_argument('Richard', 20)
positional_argument(20, 'Richard')

位置形参和位置实参(统称位置参数)是最重要的参数类型,在参数匹配中它的优先级是最高的。

3. 参数默认值

有其他高级语言(如java)经验的人知道,有重载函数这一说法,两个函数的名字相同,其参数列表不同,功能不同。调用者通过指定不同的实参,调用不同形参的重载函数。

但是在Python中没有重载函数的说法,因为默认参数值得存在,是的调用者在调用同一个函数的时候可以指定不同参数。虽然不支持重载,但是Python以默认参数值的方式实现了重载函数的功能。

指定了默认参数值的形参不能位于位置参数之前,因为实参匹配是位置优先的,这时在前面的指定了默认值的参数会被位置实参覆盖,导致后面的位置形参无法匹配到实参值而调用失败:

# default argument values, non-default argument cann't follow default argument
def default_argument_value(name, age = 20, id = '0001'):
print('name->type:%s, value:%s' % (type(name), name))
print('age->type:%s, value:%s' % (type(age), age))
print('id->type:%s, value:%s' % (type(id), id)) # 调用时,可以有多种实参形式
# 指定唯一的强制参数
default_argument_value('Richard')
# 指定其中一个默认参数
default_argument_value('Richard', 22)
# 指定全部参数
default_argument_value('Richard', 22, '002')

4. 任意参数列表

Python的函数相较于其他高级语言强大的地方在于,可以收集多余的未匹配到形参的实参。使用如下格式的形参:*args,收集到尚未匹配到形参的实际参数。

接收的额外位置实参以元组的形式存储,且任意参数列表需要在位置参数之后:

# Arbitrary Argument Lists
# It receives a tuple containing the positional arguments beyond the formal parameter list. (*name must occur before **name.)
# be last in the list of formal parameters, because they scoop up all remaining input arguments that are passed to the function def arbitrary_arguments_list(name, age, *args):
print('name->type:%s, value:%s' % (type(name), name))
print('age->type:%s, value:%s' % (type(age), age))
print('args->type:%s, value:%s' % (type(args), args)) # 实参1, 2, 3没有位置形参匹配,被任意参数列表收集
arbitrary_arguments_list('Richard', 20, 1, 2, 3)

output:

name->type:<class 'str'>, value:Richard
age->type:<class 'int'>, value:20
args->type:<class 'tuple'>, value:(1, 2, 3)

5. 关键字参数

在调用函数时,通过位置参数方式调用,每个参数到底匹配哪个形参是不容易发现的,之后查看函数定义才能知道。可以通过指定形参对应的实参值的方式调用,这样实参形参的匹配更加明了。

还是以位置形参为例:

# positional argument, name is not important, but order matters
def positional_argument(name, age):
print('name->type:%s, value:%s' % (type(name), name))
print('age->type:%s, value:%s' % (type(age), age))

在调用时可以通过关键字方式:

# keyword arguments.
# In a function call, keyword arguments must follow positional arguments.
# All the keyword arguments passed must match one of the arguments accepted by the function, and their order is not important.
positional_argument(age = 20, name = 'Richard')

关键字实参必须在位置实参之后,并且可以在形参列表中匹配到形参名字,否则调用失败:

# 形参中没有名为id的参数,所以调用失败
positional_argument(age = 20, name = 'Richard', id = '003')

收集多余关键字实参

任意参数列表能够接收没有匹配到位置形参的实参,而关键字形参字典能够接受为匹配到关键字参数的实参。通过如**kwargs的方式,收集尚未匹配的关键字实参,关键字参数字典也要在位置参数之后:

# keyword arguments dict **kwargs.
# It receives a dictionary containing all keyword arguments except for those corresponding to a formal parameter.
def keyword_argument_dict(name, age, **kwargs):
print('name->type:%s, value:%s' % (type(name), name))
print('age->type:%s, value:%s' % (type(age), age))
print('kwargs->type:%s, value:%s' % (type(kwargs), kwargs))
keyword_argument_dict(name = 'Richard', age = 20, id = '0001', type = 'it')

output:

name->type:<class 'str'>, value:Richard
age->type:<class 'int'>, value:20
kwargs->type:<class 'dict'>, value:{'id': '0001', 'type': 'it'}

另外,关键字形参字典需要在任意参数列表之后。

6. 强制关键字参数

任意出现在*arg或者*之后的形参都是命名关键字参数,意味着它们只能作为关键字实参匹配,而非位置实参。

# Keyword only argument.
# Any formal parameters which occur after the *args parameter are ‘keyword-only’ arguments,
# meaning that they can only be used as keywords rather than positional arguments.
def keyword_only_argument(name, *, age, id):
print('name->type:%s, value:%s' % (type(name), name))
print('age->type:%s, value:%s' % (type(age), age))
print('id->type:%s, value:%s' % (type(id), id)) keyword_only_argument('Richard', age = 20, id = '001')

output:

name->type:<class 'str'>, value:Richard
age->type:<class 'int'>, value:20
id->type:<class 'str'>, value:001

7. 序列和字典实参的解包

在函数调用时,使用*sequence将序列解包为位置实参;

使用**dict将字典解包为关键字实参。

def mix_param(name, *args, **kwargs):
print('name->type:%s, value:%s' % (type(name), name))
print('args->type:%s, value:%s' % (type(args), args))
print('kwargs->type:%s, value:%s' % (type(kwargs), kwargs)) mix_param('Richard', *(1, 2, 3), **{'age':20, 'id':'001'})

output:

name->type:<class 'str'>, value:Richard
args->type:<class 'tuple'>, value:(1, 2, 3)
kwargs->type:<class 'dict'>, value:{'age': 20, 'id': '001'}

注意到*args解包为位置参数,而**kwargs解包为关键字参数,涵盖了Python中所有可能出现的实参类型。因此,可以使用这两个组合调用任意形参实行的函数:

def foo(x, y, z, m = 0, n = 0):
print(x, y, z, m, n) def call_foo(*args, **kwargs):
print('Call foo!')
foo(*args, **kwargs)

注意到call_foo函数中,args是一个元组,kwargs是一个字典,所以可以解包他们组合调用任意形参形式的函数。这一种方式在调用父类构造函数时非常有用!

Python基础-函数参数的更多相关文章

  1. python基础——函数参数

    课上老师已经讲过函数(func)的参数(args)传递. 之前学习了根据位置传递对应的参数,下面会介绍其他参数传递方式. 之前的位置传参: def f(a,b,c): return a+b+c pri ...

  2. python基础——函数的参数

    python基础——函数的参数 定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复 ...

  3. python基础—函数嵌套与闭包

    python基础-函数嵌套与闭包 1.名称空间与作用域 1 名称空间分为: 1 内置名称空间   内置在解释器中的名称 2 全局名称空间   顶头写的名称 3 局部名称空间 2 找一个名称的查找顺序: ...

  4. python基础—函数装饰器

    python基础-函数装饰器 1.什么是装饰器 装饰器本质上是一个python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能. 装饰器的返回值是也是一个函数对象. 装饰器经常用于有切 ...

  5. (转)python中函数参数中如果带有默认参数list的特殊情况

    在python中函数参数中如果带有默认参数list遇到问题 先看一段代码 1 2 3 4 5 6 7 8 9 def f(x,l=[]):     for i in range(x):         ...

  6. python基础,函数,面向对象,模块练习

    ---恢复内容开始--- python基础,函数,面向对象,模块练习 1,简述python中基本数据类型中表示False的数据有哪些? #  [] {} () None 0 2,位和字节的关系? # ...

  7. 速战速决 (3) - PHP: 函数基础, 函数参数, 函数返回值, 可变函数, 匿名函数, 闭包函数, 回调函数

    [源码下载] 速战速决 (3) - PHP: 函数基础, 函数参数, 函数返回值, 可变函数, 匿名函数, 闭包函数, 回调函数 作者:webabcd 介绍速战速决 之 PHP 函数基础 函数参数 函 ...

  8. python中函数参数的引用方式

    值传递和引用传递时C++中的概念,在python中函数参数的传递是变量指向的对象的物理内存地址!!! python不允许程序员选择采用传值还是传引用.Python参数传递采用的肯定是“传对象引用”的方 ...

  9. Python基础(函数,函数的定义,函数的调用,函数的参数,递归函数)

    1.函数 我们知道圆的面积计算公式为: S = πr2 当我们知道半径r的值时,就可以根据公式计算出面积.假设我们需要计算3个不同大小的圆的面积: r1 = 12.34 r2 = 9.08 r3 = ...

随机推荐

  1. Codeforces Round #563 (Div. 2) A. Ehab Fails to Be Thanos

    链接:https://codeforces.com/contest/1174/problem/A 题意: You're given an array aa of length 2n2n. Is it ...

  2. Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) A

    Petya is a big fan of mathematics, especially its part related to fractions. Recently he learned tha ...

  3. net core (上)

    net core (上) 本文是基于Windows10的. 下载地址: https://code.visualstudio.com/ insider 版下载地址: https://code.visua ...

  4. 057 Insert Interval 插入区间

    给出一个无重叠的按照区间起始端点排序的区间列表.在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间).示例 1:给定区间 [1,3],[6,9],插入并合并 ...

  5. Codeforces Round #377 (Div. 2) 被坑了

    http://codeforces.com/contest/732/problem/B 题目要求任意两个连续的日子都要 >= k 那么如果a[1] + a[2] < k,就要把a[2]加上 ...

  6. ML.NET 示例:目录

    ML.NET 示例中文版:https://github.com/feiyun0112/machinelearning-samples.zh-cn 英文原版请访问:https://github.com/ ...

  7. Spring注入属性、对象

    对Category和Product注入属性,并且对Product对象,注入一个Category对象 一.新建项目 二.导包 三.新建Category类 package com.yyt.pojo; pu ...

  8. SharePoint 2016 如何修改Library 地址

    Scenario #1 如何为一个Library 修改下访问 网络路径地址 1.点击library,点开open with explorer,使用Windows资源管理器打开文档库 2.在文件夹层次结 ...

  9. selenium+python之python多线程

    程序.进程及线程的区别 计算机程序是磁盘中可执行的二进制数据(或者其他类型)他们只有在被读取到内存中,被操作系统调用才开始他们的生命周期. 进程是程序的一次执行,每个进程都有自己的地址空间,内存,数据 ...

  10. selenium报错信息-- Python 中 'unicodeescape' codec can't decode bytes in position XXX: trun错误解决方案

    本以为是缺少utf-8造成的错误,但是加完这个还是报错,于是在网上百度了一下是因为上传的路劲粗无偶导致的 正确的写法是把路劲中“\”变为“\\”,或者在路劲的开头加上“r”,或者在路劲的开头加上“r” ...