码迷,mamicode.com
首页 > 其他好文 > 详细

study note10

时间:2018-01-10 20:13:38      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:接受   dea   int   div   not   color   rgs   note   st3   

参数组:

def test(*args):
    print(args)
test(1,2,3,4,5,6)
test(*[1,2,3])#转成元祖

def test1(a,*args):  #args只能接收位置参数,不能接受关键字参数
    print(a)
    print(args)
test1(1,2,3,4,5) #1会传给a,2345传给args

def test2(**kwargs):  #kwargs接收关键字参数,转成字典
    print(kwargs)
    print(kwargs[‘name‘])
    print(kwargs[‘age‘])
test2(name=‘deakin‘,age=28)#字典
test2(**{‘name‘:‘deakin‘,‘age‘:28})

def test3(name,**kwargs):
    print(name)
    print(kwargs)
test3(‘john‘,age=10,sex=‘M‘)

def test4(name,age=18,**kwargs):
    print(name)
    print(age)
    print(kwargs)
test4(‘deakin‘,job=‘IT‘)

def test5(name,age=28,*args,**kwargs):
    print(name)
    print(age)
    print(args)
    print(kwargs)
test5(‘amy‘,20,2,3,4,5,‘deakin‘,job=‘IT‘)  #2345,deakin都是位置参数,传给args转为元祖

打印结果:
test:
(1, 2, 3, 4, 5, 6)
(1, 2, 3)

test1:
1 (
2, 3, 4, 5) test2: {‘age‘: 28, ‘name‘: ‘deakin‘} deakin 28 {‘age‘: 28, ‘name‘: ‘deakin‘} deakin 28 test3: john {‘sex‘: ‘M‘, ‘age‘: 10} test4: deakin 18 {‘job‘: ‘IT‘} test5: amy 20 (2, 3, 4, 5, ‘deakin‘) {‘job‘: ‘IT‘}

 

study note10

标签:接受   dea   int   div   not   color   rgs   note   st3   

原文地址:https://www.cnblogs.com/Deakin-Du/p/8260119.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!