Summary of usage examples of Python variable parameters *args and **kwargs

不言
Release: 2018-04-27 11:09:50
Original
1562 people have browsed it

This article mainly introduces the usage of Python variable parameters *args and **kwargs. It summarizes and analyzes the functions, differences and specific usage skills of variable parameters *args and **kwargs in Python in the form of examples. Friends who need it You can refer to the following

The examples in this article describe the usage of Python variable parameters *args and **kwargs. Share it with everyone for your reference, the details are as follows:

A simple summary in one sentence: When the parameters of the function are uncertain, you need to use *args and **kwargs, The difference between the former and the latter is that the latter introduces a "variable" key Concept, while the former does not have the concept of key, please refer to the following usage examples and specific explanations:

#!usr/bin/env python
#encoding:utf-8
'''''
__Author__:沂水寒城
功能:*args 和 **kwargs
'''
def test_func1(*args):
  '''''
  *args
  当函数的参数数量不确定的时候可以使用*args,个人理解*args相当于一个大小可变地列表
  容器,有点类似于C语言中的指针,传给引用即可找到内容,在这里可以使用*+变量的形式
  来实现内容可变列表的输出
  '''
  for index, one_char in enumerate(args):
    print 'index={0}, one_char={1}'.format(index, one_char)
def test_func2(**kwargs):
  '''''
  **kwargs
  这个和上面的功能性质是一样的,只是*args没有key的概念,**kwargs加入了可变key的操作
  这个参数允许你使用未定义的参数名而不会出现KeyError
  '''
  for id_num, name in kwargs.items():
    print '{0}:{1}'.format(id_num,name)
def print_dict(one_dict):
  '''''
  直接输出字典内容
  '''
  for id_num, name in one_dict.items():
    print id_num, name
if __name__ == '__main__':
  print "脚本之家测试结果:"
  str_list=['沂','水','寒','城','We','Are','Friends']
  str_dict={'id_num':20123456, 'name':'yishuihancheng'}
  test_func1(*str_list)
  test_func2(**str_dict)
  print '-----------------------------------------------------------'
  print_dict(str_dict)
Copy after login

The results are as follows :

Script House test results:
index=0, one_char=悂
index=1, one_char=水
index=2, one_char=Han
index =3, one_char=城
index=4, one_char=We
index=5, one_char=Are
index=6, one_char=Friends
id_num:20123456
name:yishuihancheng
------------------------------------------------ ----------
id_num 20123456
name yishuihancheng

Screenshot of running results:

Related recommendations :

Explain the use of python parameters and scope

The above is the detailed content of Summary of usage examples of Python variable parameters *args and **kwargs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!