Python *args **kwargs, passing in an invariant argument to a function, or passing in a lot of content to a function, commonly used in constructors.

Source: Internet
Author: User

" "Example 1: Show the usage of *args, pass in multiple parameters, do not pre-define. In this example, 3 parameters are passed in. There is no pre-defined. Automatically generate tuples within a function ()" "defQ1 (*args):Print('Example 1')    Print(args)Print(Type (args))Print(args[0]) T1= 123T2= 234T3= 345Q1 (T1, T2, T3)Print('-----------------')" "Example 2, as in Example 1, but the incoming string, tuple, list, will be in the function of the Narimoto group () Note the introduction of parameters to have *, no asterisks, the function of your incoming argument list (tuple, string) as a parameter to deal with. When you have an asterisk, the function treats your incoming argument list (tuples, strings) as multiple arguments, and Narimoto groups () within the function as the last call to Q2 (T3)" "defQ2 (*args):Print('Example 2')    Print(args)Print(Type (args))Print(args[0]) T1= 123,234,345Q2 (*t1) T2= (234,345,456) Q2 (*T2) T3=[345,456,567]Q2 (*T3) Q2 (T3)Print('-----------------')" "Example 3, this example shows the use of **kwargs, passed in the dictionary, the function is also a dictionary. " "defQ3 (* *Kwargs):Print('Example 3')    Print(Kwargs)Print(Type (Kwargs))Print(kwargs['name1']) di= {    'name1':'Jack',    'name2':'Rose',}q3 (**di)Print('-----------------')" "Example 4: This example shows that when a function is defined, a function can be defined using *args,**kwargs at the same time. However, references can be referenced separately, referencing only *args, or only **kwargs, temporarily shielding the use of tuples and dictionaries within the function, to avoid error. " "defQ4 (*args,**Kwargs):Print('Example 4')    Print(args)#print (args[0])    Print(Kwargs)#Print (Kwargs.get (' name1 '))T1 = 123,234,345di= {    'name1':'Jack',    'name2':'Rose',}q4 (*T1)Print('---') Q4 (**di)Print('-----------------')" "Example 5, this example shows that the *args,**kwargs is defined at the same time and is used simultaneously. " "defQ5 (*args,**Kwargs):Print('Example 5')    Print(args)Print(Args[0])Print(Kwargs)Print(Kwargs.get ('name1')) T1= 123,234,345di= {    'name1':'Jack',    'name2':'Rose',}q5 (*t1,**di)Print('-----------------')" "Example 6, this example shows the *args, as the redundant parameters passed into the function, but also see the code often see. " "defQ6 (t1,t2,*args):Print('Example 6')    Print(t1)Print(T2)Print(args)Print(args[0]) T1= 123,234,345Q6 ('t123','t234',*T1)Print('-----------------')" "Example 7, this example shows the **kwargs, as the redundant parameters passed into the function, but also see the code often see. " "defQ7 (k1,k2,**Kwargs):Print('Example 7')    Print(K1)Print(K2)Print(Kwargs)Print(Kwargs.get ('name1'))    Passdi= {        'name1':'Jack',        'name2':'Rose',}q7 (K1='t123', k2='t234',**di)Print('-----------------')" "Example 8, this example shows the common use of *args,**kwargs, as redundant parameters passed into the function, but also see the code often see. In addition to the function definition of the K1,K2, and in the reference, added ' t345 ', k3=1, the result t123 (K1) t234 (K2) (' t345 ', 123, 234, 345) (args) {' K3 ': 1, ' K4 ': 2, ' name1 ': ' Jac K ', ' name2 ': ' Rose '} (Kwargs) Jack kwargs[' name1 ']" "defQ8 (k1,k2,*args,**Kwargs):Print('Example 8'    )    Print(K1)Print(K2)Print(args)Print(Kwargs)Print(Kwargs.get ('name1'))    PassT1= 123,234,345di= {        'name1':'Jack',        'name2':'Rose',}q8 ('t123','t234','t345', k3=1,k4=2,*t1,**di)Print('-----------------')

It's basically that much.

Python *args **kwargs, passing in an invariant argument to a function, or passing in a lot of content to a function, commonly used in constructors.

Related Article

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.