python 变量解压赋值

 * 数组

** 字典

>>> record = [  
    ('bar',1,2),  
    ('foo','hello'),  
    ('bar', 3, 4),  
]  
>>> def do_bar(x,y):  
    print('bar',x,y)  
  
      
>>> def do_foo(s):  
    print('foo',s)  
  
  
>>> for name,*args in record:  
    if name == 'bar':  
        do_bar(*args)  # 解压赋值  
    elif name == 'foo':  
        do_foo(*args)  
  
          
bar 1 2  
foo hello  
bar 3 4  

字典:

>>> def test(a=1,b=2,c=3):  
    print(a,b,c)  
  • >>> d = dict(a=4,b=5,c=6)  
  • >>> test(**d)  

 

重要参考:https://blog.csdn.net/zvall/article/details/79052576

posted on 2018-05-16 00:10  BioinformaticsMaster  阅读(196)  评论(0编辑  收藏  举报

导航