[转]Python函数的各种参数用法(含星号参数)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[转]Python函数的各种参数用法(含星号参数)相关的知识,希望对你有一定的参考价值。

Python中函数的参数有4种形式,分别是:

  1. 位置或关键字参数(Positional-or-keyword parameter)
  2. 仅位置的参数(Positional-only parameter)
  3. 任意数量的位置参数(var-positional parameter)
  4. 任意数量的关键字参数(var-keyword parameter)

第一种:位置或关键字参数

这种参数是Python中默认的参数类型,定义这种参数后,可以通过位置参数,或者关键字参数的形式传递参数:

## 位置或者关键字参数

## 这个是Python的默认参数类型

## 示例:arg2提供了默认value

def

func(arg1, arg2="World!"):

    print

arg1, arg2

 

## func可以通过位置参数形式调用

func("Hello", "MitchellChu")

 

## 也可以通过关键字参数的形式来调用func

func(arg1="Hello", arg2="World!")

 

## 当然,混合的方式也是完全没有问题的

func("Hello", arg2="World!")

 

## 不过如果你不能将关键字参数优先于位置参数传递给函数(方法)

## 这个调用方法是不能接受的,因为优先级不一样.后面会说

func(arg1="Hello", "World!") ## ERROR

 

第二种方式:仅适用位置参数的形式

这种形式在需要将参数传递给函数(方法)时,仅能通过位置参数的传递方式。这种形式对于Python的开发者来说,暂时并没有办法使用。这种形式现在仅存在Python的很多内建的函数上:

## Positional-only parameter has no syntax to define

## 虽然无定义方法,但内建的很多函数都是仅接受位置参数的

abs(-3) ## correct

abs(a=3) ## wrong

 

## Traceback (most recent call last):

##   File "<stdin>", line 1, in <module>

## TypeError: abs() takes no keyword arguments

 

 

pow(x=2,y=3)

## Traceback (most recent call last):

##   File "<stdin>", line 1, in <module>

## TypeError: pow() takes no keyword arguments

 

pow(2,3)

## 8

 

第三种:任意数量的位置参数(带单个星号参数

任意数量的位置参数在定义的时候是需要一个星号前缀来表示,在传递参数的时候,可以在原有参数的后面添加任意多个参数,这些参数将会被放在元组内提供给函数(方法):

## var-positional parameter

## 定义的时候,我们需要添加单个星号作为前缀

def

func(arg1, arg2, *args):

    print

arg1, arg2, args

 

## 调用的时候,前面两个必须在前面

## 前两个参数是位置或关键字参数的形式

## 所以你可以使用这种参数的任一合法的传递方法

func("hello", "Tuple, values is:", 2, 3, 3, 4)

 

## Output:

## hello Tuple, values is: (2, 3, 3, 4)

## 多余的参数将自动被放入元组中提供给函数使用

 

## 如果你需要传递元组给函数

## 你需要在传递的过程中添加*号

## 请看下面例子中的输出差异:

 

func("hello", "Tuple, values is:", (2, 3, 3, 4))

 

## Output:

## hello Tuple, values is: ((2, 3, 3, 4),)

 

func("hello", "Tuple, values is:", *(2, 3, 3, 4))

 

## Output:

## hello Tuple, values is: (2, 3, 3, 4)

 

第四种:任意数量的关键字参数(带两个星号参数)

任意数量的关键字参数在定义的时候,参数名称前面需要有两个星号(**)作为前缀,这样定义出来的参数,在传递参数的时候,可以在原有的参数后面添加任意多个关键字参数,关键字参数是使用[参数名称=参数值]的形式进行传递:

## var-keywords parameter

## 定义的时候,需要两个星号作为前缀

def

func(arg1, arg2, **kwargs):

    print

arg1, arg2, kwargs

 

func("hello""Dict, values is:", x=2, y=3, z=3)

 

## Output:

## 多余的参数将自动被放入字典中提供给函数使用

 

## 如果你需要直接传递字典给函数

## 你需要在传递的过程中添加**

## 此时如果还有关键字参数应在字典前提供完成

## 不能在字典后再提供

## 请看下面例子中的输出差异:

 

func("hello""Dict., values is:"**{‘x‘:2‘y‘:3‘z‘:3})

## hello Dict., values is: {‘y‘: 3, ‘x‘: 2, ‘z‘: 3}

 

func("hello""Dict., values is:"**{‘x‘:2‘y‘:3‘z‘:3,})

## hello Dict., values is: {‘y‘: 3, ‘x‘: 2, ‘z‘: 3}

 

func("hello""Dict., values is:", {‘x‘:2‘y‘:3‘z‘:3})

## Traceback (most recent call last):

##   File "<stdin>", line 1, in <module>

## TypeError: func() takes exactly 2 arguments (3 given)

 

func("hello""Dict., values is:", s=3**{‘x‘:2‘y‘:3‘z‘:3,})

## hello Dict., values is: {‘y‘: 3, ‘x‘: 2, ‘s‘: 3, ‘z‘: 3}

 

## 提供了重复的参数

func("hello""Dict., values is:", y=3**{‘x‘:2‘y‘:3‘z‘:3,})

## Traceback (most recent call last):

##   File "<stdin>", line 1, in <module>

## TypeError: func() got multiple values for keyword argument ‘y‘

 

 总结:四种参数形式中仅有第二种Python没有提供定义的方法,其他三种在定义的时候也需要注意,定义的时候应该根据Python的解析规律进行定义,其中:

  1. 位置或关键字参数应该在最前面,其中,没有默认值的应该在有默认值的参数前面
  2. 任意数量位置参数应该放在所有位置或关键字参数的后面
  3. 任意数量关键字参数应该放在任意数量位置参数的后面

注意:任意数量位置参数和任意数量关键字参数只能在定义中定义一次。

## 各种参数的混合使用例子

## Author: wjq310

 

def

func(arg1, arg2=‘default‘*args, **kwargs):

    print

"arg1=%s, arg2=%s, args=%s, kwargs=%s"  %  (arg1, arg2, args, kwargs)

 

 

func(1## correct

func(1,2## correct

func(1,2,3,4## correct

func(1,2,3,4,x=1,y=2## correct

func(1,2,x=1## correct

 

func(x=1## wrong

func(arg1=1## correct

func(1,x=1## correct

 

## 可以将例子保存到parameter.py文件

## 而后运行python /path/to/parameter.py

转自:http://blog.csdn.net/zhongbeida_xue/article/details/51442381

 

以上是关于[转]Python函数的各种参数用法(含星号参数)的主要内容,如果未能解决你的问题,请参考以下文章

[Python]-11-星号变量的特殊用法

python学习:python的星号(*)和双星号(**)用法

python中 单星号(*)与双星号(**)的使用

Python可变参数函数用法详解

python 函数星号参数说明

Python 函数参数前面一个星号(*)和两个星号(**)的区别