python参数 分类: python基础学习 python 2013-08-23 15:06 217人阅读 评论(0) 收藏

位置参数:

def chartotuple(x,y,*z):  #使用*z收集实参中的参数
    print x,y,z

chartotuple(1,2,3,4,5)   # 结果:1 2 (3, 4, 5)

=========================================================================
关键字参数:


def chartodict(x='a',**d): #使用**d收集实参中的关键字参数
    print x,d

chartodict(y='b',z='c',m='d')   # a {'y': 'b', 'z': 'c', 'm': 'd'}

=========================================================================

将元组内容转化为参数:


t = ('sam','25','male')


def tupletochar(x,t1,t2,t3): #接收的形参t1、t2、t3个数要与元组t的元素个数相同

    print x,'my name is:%s,%s years olde,i am a %s' %(t1,t2,t3)

tupletochar('ABC',*t) #将元组t的元素转化为形参   ABC my name is:sam,25 years olde,i am a male
 
=========================================================================

将字典的键转化为参数:

d={'a':'A','b':'B','c':'C'}
def dicttokeyword(a,b,c):
    print a,b,c

dicttokeyword(**d)  #将字典的键转化为形参,且字典中的key名称与函数参数名称相同,输出键对应的值:  
A B C

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/think1988/p/4628084.html