Examples of Variable Parameter definitions and passing methods of Python functions, and details of python

Source: Internet
Author: User

Examples of Variable Parameter definitions and passing methods of Python functions, and details of python

This document describes how to define variable parameters of Python functions and how to transmit parameters. Share it with you for your reference. The specific analysis is as follows:

In python, variable function parameters are defined 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

I hope this article will help you with Python programming.

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.