python有多少个参数_Python中的参数

Python中的参数

1. python函数参数有多重形式:

* test(arg1,arg2,`*args`)

* test(arg1,arg2,`*args`,`**kwargs`)

2. 其中比较糊弄人的是:*args和**kwargs

*args 变长的占位参数列表 -

**kwargs 变长的键值对参数列表 -

3. 什么是占位参数:

test(arg1,arg2) 参数括弧中列出的标识符就是占位参数

*args变长占位参数是用来发送一个非键值对的可变数量的参数列表给一个函数, 可以遍历得到它

def test_var_args(f_arg, *argv):

print(type(argv))

#

print("first normal arg:", f_arg)

for arg in argv:

print("another arg through *argv:", arg)

test_var_args('yasoob', 'python', 'eggs', 'test')

4. 什么是键值对参数:

**kwargs 允许你将变长度的键值对, 作为参数传递给一个函数, 说白了就是函数的参数是个dict,但是不能直接传个dict给函数,得加上前导**解包

def test_kwarg(**kwargs):

print(type(kwargs))

#

for key, value in kwargs.items():

print("{0} == {1}".format(key, value))

test_kwarg(name="foo")

test_kwarg(name="foo", age=26)

test_kwarg(**{'name': "foo", 'age': '26'})

5. 综合看一个更复杂的例子

def test_kwarg(name, age, *args, **kwargs):

print('--------------------------------------')

print('all positional args:')

print('name:{0}'.format(name))

print('age:{0}'.format(age))

print('\n')

print('all optional positional *args:')

for arg in args:

print('args:{0}'.format(arg))

print('\n')

print('all keywords **kwargs:')

for key, value in kwargs.items():

print("{0} == {1}".format(key, value))

# 只有占位参数

test_kwarg("foo", 26)

# 占位参数 + 可选占位参数

test_kwarg("foo", 26, 'opt1')

# 占位参数 + 键值对参数

test_kwarg("foo", 26, kw1=100, kw2=200)

# 占位参数 + 可选占位参数 + 键值对参数

test_kwarg("foo", 26, 'opt1', 'opt2', kw1=100, kw2=200)

# --------------------------------------

# all positional args:

# name:

# foo

# age:

# 26

# all optional positional * args:

# all keywords ** kwargs:

# --------------------------------------

# all positional args:

# name:

# foo

# age:

# 26

# all optional positional * args:

# args:

# opt1

# all keywords ** kwargs:

# --------------------------------------

# all positional args:

# name:

# foo

# age:

# 26

# all optional positional * args:

# all keywords ** kwargs:

# kw1 == 100

# kw2 == 200

# --------------------------------------

# all positional args:

# name:

# foo

# age:

# 26

# all optional positional * args:

# args:

# opt1

# args:

# opt2

# all keywords ** kwargs:

# kw1 == 100

# kw2 == 200

6. Note: 函数调用时,*args, **kwargs是可选的,但是前面的常规参数是必须的

7. over

98.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值