python中*args和**args的不同

上一段代码,大家感受一下

def test_param(*args):

  print(args)

def test_param2(**args):

  print(args)

test_param('test1','test2')

>>>('test1',test2')

test_param2(p1='test1',p2='test2')

>>>{'p1':'test1', 'p2':'test2'}

python提供了两种特别的方法来定义函数的参数:

1. 位置参数 *args,  把参数收集到一个元组中,作为变量args
  def show_args(*args):   =>  how_args("hello", "world")

2. 关键字参数 **kwargs, 是一个正常的python字典类型,包含参数名和值
  def show_kwargs(**args):  = > show_kwargs(foo="bar", spam="eggs")

原文地址:https://www.cnblogs.com/Blaxon/p/4520128.html