Python variable parameters

Source: Internet
Author: User
1.python variable parameter type: 1.*args, 2.**kwargs 2. Difference: * Args does not have key, and **kwargs has key 3. How to use:
-1.*args can use arrays and tuples
-2.**kwargs can use a dictionary as a parameter
4. Note: 2 parameters are used at the same time, need to put *args in front, and **kwargs put in the back 5. Examples:
def T_args (P, *args): "" to pass array, tuple as parameters to *args:p Aram P::p Aram:: Return: "" "Print p for a in Args:print a def t_kw (P, **kwargs):" "" The dictionary as a variable parameter:p Aram p::p Aram Kwargs:: Return: "" "" "Print p for K in kwargs:print k, ' = ', Kwargs[k] def t_args_kw (P, *arg
    S, **kwargs): "" "*args and **kwargs parameters:p Aram P::p Aram args::p Aram Kwargs:: Return:" "" Print p for a in Args:print a for k in kwargs:print k, ' = ', kwargs[k] if __name__ = = ' __m ain__ ': # test array args = [1, 2, 3] # test tuple args1 = (' A ', ' B ', ' C ') # Test Dictionary d = {' id ': 1, ' name ': ' Dada '
    , ' Age ': T_args (' B ', *args) print ' ================== ' T_args (' C ', *args1) print ' ================== ' T_KW (' d ', **d) t_kw (' name ', A=1, b=2, c=3) print ' ================== ' t_args_kw (' e ', *args, **d) 

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.