函数定义
示例
def func([formal_parameter1, ... formal_parameter1]):
statement
以上函数定义的作用是创建函数对象,并且在当前作用域创建名字func,指向函数对象,在可及该作用域范围内,可以使用名字func调用函数。定义函数时候参数列表中的名字是函数形参,调用函数用的参数是实参。
参数介绍
位置参数
位置比名字重要
def positional_argument(name, age):
必须依照对应位置输出参数
参数默认值
Python没有重载(java中的函数重载)
默认参数值的方式可实现重载函数功能
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))
输入单个参数,这种可以实现可选参数效果:
if u'__main__' == __name__:
default_argument_value("Maggie");
结果:
name->type:<class 'str'>, value:Maggie
age->type:<class 'int'>, value:20
id->type:<class 'str'>, value:0001
输入部分参数,可让选中参数改变值:
if u'__main__' == __name__:
default_argument_value("Maggie",22);
结果
name->type:<class 'str'>, value:Maggie
age->type:<class 'int'>, value:22
id->type:<class 'str'>, value:0001
输入与默认参数类型不匹配的值并不会影响最终结果,依旧依照顺序去确定:
if u'__main__' == __name__:
default_argument_value("Maggie",'3r2rq34r');
结果
name->type:<class 'str'>, value:Maggie
age->type:<class 'str'>, value:3r2rq34r
id->type:<class 'str'>, value:0001
任意参数列表
可以收集多余的未匹配到形参的实参
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))
输入多个参数,未匹配到位置参数的均被任意参数获取到:
arbitrary_arguments_list('Richard', 20, 1, 2, 3)
输出
name->type:<class 'str'>, value:Richard
age->type:<class 'int'>, value:20
args->type:<class 'tuple'>, value:(1, 2, 3)
在不填可选参数的时候,将会输出空列表:
arbitrary_arguments_list('Richard', 20)
输出
name->type:<class 'str'>, value:Richard
age->type:<class 'int'>, value:20
args->type:<class 'tuple'>, value:()
在多个可选参数的时候(如果第二个也为*arg也就是输入元组的形式则会报错):
def arbitrary_arguments_list(name, age, *args,**args2):
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(args2), args2))
输入:
arbitrary_arguments_list('Richard', 20, 1, 2, 3)
输出:
name->type:<class 'str'>, value:Richard
age->type:<class 'int'>, value:20
args->type:<class 'tuple'>, value:(1, 2, 3)
args2->type:<class 'dict'>, value:{}
输入:
arbitrary_arguments_list('Richard', 20, 1, 2, 3,a='b',c='d')
输出:
name->type:<class 'str'>, value:Richard
age->type:<class 'int'>, value:20
args->type:<class 'tuple'>, value:(1, 2, 3)
args2->type:<class 'dict'>, value:{'a': 'b', 'c': 'd'}
Ps:注意字典key值不能重复哦。
在多个可选参数的时候,输入顺序如果不匹配也不会通过:
arbitrary_arguments_list('Richard', 20, 1, 7,a='b',2, 3,c='d')
结果:
编译不通过,不可以交叉着来哦
关键字参数
输入带key值的实参:
default_argument_value(age=23, name='Richard', id='003')
输出:
name->type:<class 'str'>, value:Richard
age->type:<class 'int'>, value:23
id->type:<class 'str'>, value:003
若有未匹配到的则会报错
关键字形参字典能够接受未匹配到关键字参数的实参,关键字形参字典需要在任意参数列表之后。依照如下形式
def keyword_argument_dict(name, age, **kwargs):
强制关键字参数
任意出现在*arg之后的形参都是命名关键字参数,意味着它们只能作为关键字实参匹配,而非位置实参。
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(23, 'Richard', '003')
输出
keyword_only_argument() takes 1 positional argument but 3 were given
序列和字典实参的解包
在函数调用时,使用*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'})
输出:
name->type:<class 'str'>, value:Richard
args->type:<class 'tuple'>, value:(1, 2, 3)
kwargs->type:<class 'dict'>, value:{'age': 20, 'id': '001'}
参考文档:
https://www.cnblogs.com/crazyrunning/p/7100359.html