Magic asterisks in Python

Source: Internet
Author: User

When passing real parameters and defining parameters (the so-called real parameters are the parameters passed in when the function is called, and the shape parameters are defined parameters), you can also use two special syntaxes: ''*''**.

Used to call a function***

Test (* ARGs)

* In fact, each element in the ARGs is passed as a location parameter. For exampleCodeIf ARGs is equal to (1, 2, 3), the code is equivalent to test (1, 2, 3 ).

Test (** kwargs)

** The function is to convert the dictionary kwargs into a keyword parameter for transmission. For example, in the above Code, if kwargs is equal to {'A': 1, 'B': 2, 'C': 3}, then this code is equivalent to test (a = 1, B = 2, c = 3 ).

Used to define function parameters***

Def test (* ARGs ):
When defining function parameters, the meaning of * must be different. Here, * ARGs indicates that all the input location parameters are included in the tuple args. For example, if the preceding function calls test (1, 2, 3), the value of argS IS (1, 2, 3 ).

Def test (** kwargs ):
... Similarly, ** is for keyword parameters and dictionaries. If you call test (a = 1, B = 2, c = 3), The kwargs value is {'A': 1, 'B': 2, 'C ': 3.

Common parameter definitions and transmission methods can coexist with *, but it is clear that * must be placed at the end of all location parameters, and ** must be placed at the end of all keyword parameters, otherwise, there will be ambiguity.

Source: http://www.th7.cn/Program/Python/2011-07-07/28728.shtml

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.