Describes the variable parameter definitions of Python functions and the parameter passing methods.

Source: Internet
Author: User

Describes the variable parameter definitions of Python functions and the parameter passing methods.

Variable Parameter definition of Python functions and parameter transmission methods

The definition of variable parameters of functions in python is as follows:

1. func (* args)

The input parameters exist in args as tuples, for example:

Def func (* args): print args >>>> func (1, 2, 3) (1, 2, 3) >>> func (* [1, 2, 3]) # In this method, all elements in a list can be passed as an indefinite parameter (1, 2, 3)

2. func (** kwargs)

The input parameters exist in args as dictionaries, for example:

Def func (** kwargs): print kwargs >>>> func (a = 1, B = 2, c = 3) {'A': 1, 'C': 3, 'B': 2 }>>> func (** {'A': 1, 'B': 2, 'C': 3 }) # In this way, all key-value pairs in a dictionary can be passed as keyword parameters {'A': 1, 'C': 3, 'B': 2}

3. You can also mix the two func (* args, ** kwargs)

The input sequence must be the same as the defined sequence. Here, the parameter list is not fixed, and then the keyword parameter dictionary, for example:

Def func (* args, ** kwargs): print args print kwargs >>> func (1, 2, 3) (1, 2, 3) {}>>> func (* [1, 2, 3]) (1, 2, 3) {}>> func (a = 1, B = 2, c = 3) () {'A': 1, 'C': 3, 'B': 2 }>>> func (** {'A': 1, 'B ': 2, 'C': 3}) () {'A': 1, 'C': 3, 'B': 2 }>>> func (, 3, a = 4, B = 5, c = 6) (1, 2, 3) {'A': 4, 'C': 6, 'B ': 5} </span> # Skip transfer is not feasible> func (1, 2, 3, a = 4, B = 5, c = 6, 7) SyntaxError: non-keyword arg after keyword arg

If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!

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.