Python variable parameter *args and **kwargs usage examples Summary

Source: Internet
Author: User
This article mainly introduced the python variable parameter *args and **kwargs usage, combined with the example form summarizes the variable parameter *args and the **kwargs function, the difference and the specific use skill in Python, the need friend can refer to the next

The examples in this article describe python variable parameter *args and **kwargs usage. Share to everyone for your reference, as follows:

A simple summary: when the parameters of the function are uncertain, the *args **kwargs difference between the former and the latter is that the latter introduces the concept of "variable" key, and the former does not have the concept of key. Look at the following examples of use and specific explanations:

#!usr/bin/env python#encoding:utf-8 ' __author__: Yishui Cold City Features: *args and **kwargs ' Def test_func1 (*args):  ' ' *args  When the parameter number of the function is indeterminate can use *args, personal understanding *args equivalent to a variable size list  container, a bit similar to the C language pointer, to the reference to find content, where you can use the form  of *+ variable To implement the output of the content mutable list  "for  index, One_char in Enumerate (args):    print ' index={0}, One_char={1} '. Format (index, ONE_CHAR) def test_func2 (**kwargs):  "  **kwargs  This is the same as the functional nature of the above, just *args no key concept, * * Kwargs added variable key operation  This parameter allows you to use undefined parameter name without appearing Keyerror  "for  Id_num, name in Kwargs.items ():    print ' {0}:{1} '. Format (id_num,name) def print_dict (one_dict):  ""  Output Dictionary contents directly ""  for Id_num, name in One_dict.items ():    print id_num, nameif __name__ = = ' __main__ ':  print "script home test Result:"  str_list=[' yi ', ' water ', ' Cold ', ' City ', ' We ', ' is ', ' Friends ']  str_dict={' id_num ': 20123456, ' name ': ' Yishuihancheng '}  test_func1 (*str_ List)  Test_func2 (**str_dict)  print '-----------------------------------------------------------'  print_dict (str_dict)

The results are as follows:

Script Home Test results:
index=0, One_char= Yi
Index=1, one_char= Water
index=2, one_char= Cold
Index=3, One_char= City
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

Operation Result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.