python函数参数可变性

 

1.参数可变性基础:

#!/usr/bin/env python
# encoding: utf-8

#________________________________________________只传入一个dict_______________________________________________________________
def chen(*a,**b):
    print(*a)#输出普通字典
    print(b)#{}
    print(a)#以元组形式输出字典,后面有逗号
ee={'cc':'hhjhjh','ppp':'kdjwoej','yuyu':'qwkd56'}
chen(ee)#传入一个字典

#______________________________________________同时传入两个dict___________________________________________________________________
def chen(*a,**b):
    print(*a)#以二维形式输出字典
    print(b)#{}
    print(a)#以元组形式输出字典
    print(a[0])#以下标方式显示字典(超出就越界)
bbb={'chen':'9090'}
ee={'cc':'hhjhjh','ppp':'kdjwoej','yuyu':'qwkd56'}
chen(ee,bbb)#转入两个字典


#_______________________________________同时传入list和dict_________________________________________________________________________
def chen(*a,**b):
    print(*a)#以二维形式输出list及dict
    print(b)#{}
    print(a)#以元组形式输出list及dict
    print(a[0])#以下标方式显示
cc=['423','42342chen']
ee={'cc':'hhjhjh','ppp':'kdjwoej','yuyu':'qwkd56'}
chen(cc,bbb)#传入list及dict



#_____________________________________________________都传入list_______________________________________________________________________
def chen(*a,**b):
    print(*a)#以二维形式输出list
    print(b)#{}
    print(a)#以元组形式输出list
    print(a[0])#以下标方式显示list
cc=['423','42342chen']
ddd=['wei','56655','errrr','64rwr']
chen(cc,ddd)


#_________________________________________________同时转入list和指定变量_____________________________________________________________
def chen(*a,**b):
    print(*a)#打印list
    print(b)#打印字典
    print(*b)#打印字典的key
    print(a)#打印元组

ee=['123456','chen456','090909']
chen(ee,a=123,b='dalan456',c='99999999999999')#传入一个字典



#_______________________________________________指定变量/赋值调用___________________________________________________________________
def chen(*a,**b):
    print(*a)
    print(b)#获取字典
    print(b['name'])#获取key的value
    print(*b)#获取所有key

#赋值方式调用(指定变量)
#chen(chen='123',test='test123',name='hallo word')

#混合方式调用
chen('test','9999',chen='123',test='test123',name='hallo word')



#传入两个list#——————————————————————补充————————————————————————————————————
#* 与 ** 在调用函数时的作用(可以作为字典key或value为参数)
def t(b,c):
    print(b)
    print (c)
d = {'b':32, 'c':321}
t(*d)
t(**d)

 

 

2.参数可变性-进阶:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

def foo(a, b, *c):
    print (a)
    print (b)
    print (c)#从第2个元素开始变成元组
    print(*c)#单独打印

foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)



def foo1(a, **b):
    print (a)#a=100
    print(b)#为字典
    for key,value in b.items():
        print('{}:{}'.format(key,value))

foo1(100, name = "huoty", age = "24", addr = "beijing.haidian")



def foo2(aa,bb, *args, **kwargs):
    print (aa)#打印第一个参数
    print (bb)#打印第二个参数(若函数没有bb参数,那么['chen','wei']将传入到args参数中)
    print ("------- split -------")
    print (args)#打印非字典的参数
    print ("------- split -------")
    print (kwargs)#打印字典

foo2(1, ['chen','wei'], 3, x = 4, y = 5, *[1, 2, 3], **{'a':1,'b': 2})



def foo3(aa,bb, *args, **kwargs):
    print (aa)#打印第一个参数
    print (bb)#打印第二个参数(若函数没有bb参数,那么['chen','wei']将传入到args参数中)
    print ("------- split -------")
    print (args)
    print ("------- split -------")
    print (kwargs)#打印字典

foo3(1, 2,6, 3, {'name':'dalan_chen','test': 'youxi'},x = 4, y = 5, *[1, 2, 3], **{'a':1,'b': 2})



def foo1(*a,**b):
    print(a)
    print(b)
    print(b['age'])#直接获取字典的key的值

#纸只传字典类型
#foo1(name = "huoty", age = "24", addr = "beijing.haidian")

#传字典和list
#foo1(['123','777'],name = "huoty", age = "24", addr = "beijing.haidian")

#传字典、list、字符串
foo1('5555','chen',['123','777'],name = "huoty", age = "24", addr = "beijing.haidian")

 

 

备注参数中如果使用“*”元组参数 或者“**”字典参数,  这两种参数应该放在参数列表最后。并且“*”元组参数位于“**”字典参数之前。

一言概之,*主要使用在函数参数中,在定义函数中添加是为了在调用该函数时直接将多个参数转换成一个元组,而在调用函数时参数添加*,是为了将列表/元组转化成多个参数。
**也主要使用在函数参数中,在定义函数中添加是为了在调用该函数时可以添加任意个键值对转出一个字典,而在调用函数时在列表中添加**,是为了将字典转化成多个参数。

好处:这种模式函数在接收参数时,不用考虑具体的形参,而且形参在函数内部也可自由拆分

 

posted on 2019-11-08 20:28  chen_2987  阅读(150)  评论(0)    收藏  举报

导航