Python function Dynamic Parameters

Source: Internet
Author: User

Def show (*arg):
Print (Arg,type (ARG))
Show ([11,22],[33])
Results: ([one, one], []) <class ' tuple ' >
Conclusion: A parameter passed by a * is defined by default as Ganso type

Def show (**arg):
Print (Arg,type (ARG))
Show (k1= ' v1 ', k2= ' v2 ', k3= ' v3 ')
Result: {' K1 ': ' v1 ', ' K3 ': ' V3 ', ' K2 ': ' v2 '} <class ' dict ' > definition
Conclusion: The parameters passed by the two * * numbers are defined by default as dictionary type

Def show (*arg,**kwargs):
Print (Arg,type (ARG))
Print (Kwargs,type (Kwargs))
Show ([11,22,33],[44],[' A ', ' B '],k1= ' v1 ', k2= ' v2 ')
Results:

 ([One, One, "], [[], [' A ', ' B ']) <class ' tuple ' >
{' K2 ': ' v2 ', ' K1 ': ' v1 '} <class ' Dict ' >

Conclusion: At the same time pass two parameters with the * number, in the formal parameter definition, the first parameter can only be with an * number, the second parameter can only be with two * number, while in the parameter transfer, the first argument can only be a normal definition, the second argument is the definition of a dictionary

Def show (*arg,**kwargs):
Print (Arg,type (ARG))
Print (Kwargs,type (Kwargs))
#show ([11,22,33],[44],[' A ', ' B '],k1= ' v1 ', k2= ' v2 ')

Li = [[11,22,33],[44],[' A ', ' B ']]
Di = {' K1 ': ' v1 ', ' K2 ': ' V2 '}
Show (Li,di)

Show (*li,**di)

Results:

([[One, all,], [] [], [' A ', ' B ']], {' K1 ': ' v1 ', ' K2 ': ' v2 '} ' <class ' tuple ' >
{} <class ' dict ' >
([One, One, "], [[], [' A ', ' B ']) <class ' tuple ' >
{' K1 ': ' v1 ', ' K2 ': ' v2 '} <class ' Dict ' >

Conclusion: By using the first method to pass parameters, Python defaults to a formal parameter that is passed to the first * number, and the second * number does not receive parameters. The second way to pass the arguments is to pass the matching parameters to the corresponding formal parameter.

  



Python function Dynamic Parameters

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.