What does it mean to add an asterisk before the reference of a tuples?

Source: Internet
Author: User
Tags define function
Rect (, screen. width (), screen. height () pixbufGdk. pixbuf_get_from_window (rootwin, * rect) # Where * rect is?
Rect = (0, 0, screen. width (), screen. height () pixbuf = Gdk. pixbuf_get_from_window (rootwin, * rect) # What is * rect?
Reply: I will not use PyGObject for pure discussion. Asterisk ParameterThis syntax.

Similar Problem (may be helpful to you ):
Define function def func (** kwargs): Do print kwargs need func (a = 1, B = 2, c = 3) when calling a function? Dict_t = {'A': 1, 'B': 2, 'C': 3} Can't the dictionary be directly transmitted? For example, func (dict_t ). Is there any good way?

Python parameter knowledge (meaning of adding a star before a variable)



Let's get the meaning of the asterisk parameter and two examples:

Use an asterisk before the parameter name to make the function accept any number of positional parameters.
The two asterisks accept any number of keyword parameters.


Example of location parameters:
Assume that you have the following requirements: Output all parameters received by a function except the first parameter.



Keyword parameter example:
No matter how many keyword parameters you pass in, I can find them in kw.




Next, let's take a look at the example in the supplementary information you provided in the question:


The following example accepts any number of location parameters and only outputs the first parameter.
(Here the type of s is tuples)


Let's define a new metagroup, which will be thrown to the mean function later.


Differences between the two usage methods:

As you can see, after an asterisk is used, a is unpacked. Split them into parameters and threw them in.
The above example is equivalent:




======================== 2013-2-26 update ==============================


S is a tuple, so the value cannot be modified.
Here I am trying to change the 1st values in the location parameter to 5 and then output them.


Use the tuples to see:

Let's switch to the list to see:
Use a string to see:

Here we split the list, the tuples, and the string is converted into a location parameter and then passed in.
You can see that. S is a tuples.
Type of the s variable that stores the Location Parameter It does not depend on the type of the input parameter.




======================== Write more examples =========================:
Common functions:

Literary functions:





The mean traverses all output location parameters. S is a tuples. Don't forget.
Let's split the list and strings.
Split tuples:


Dictionary Splitting:


If you split the dictionary, it only transmits the [Key]. You cannot obtain the value.



====== The value of the dictionary to be split. You can use two asterisks for function parameters. ========

This means that all keyword parameters are thrown to s (excluding location parameters)
When using functions, When using functions,
The two asterisks in front of c only represent the form of splitting c into a keyword parameter.

When used, we split the dictionary into keyword parameters.
Function receiving is also a keyword parameter. Therefore, no error is reported.


It is equivalent to the following: (No error reported, haha .) (No error reported, haha .)



If you use a function with only one asterisk:

Or do not give an asterisk:
Errors are reported. Errors are reported.


Why ??

Put one asterisk:
Put an asterisk in front of the dictionary.
If you split the dictionary with One asterisk, only the [Key] is passed in. The previous example proves that.

The problem is that our function mean only accepts the keyword parameter (Let's review the definition of mean before ): There is nothing in s. Output? Error! There is nothing in s. Output? Error!


Without an asterisk:
Without an asterisk, you can directly throw the dictionary.
The problem is that function parameters ** s only accept keyword parameters.
S. There is nothing in it. An error is returned!



( Function NameAnd Parameter NameThey are all random and have no special significance .)
I feel that my example is not very good. If you get confused.
Welcome to discussion in the comment area .:)



Extended reading:

StackOverflow explains the asterisk Syntax:
Syntax-Python: Once and for all. What does the Star operator mean in Python?

Reference Value passing:
Python: How do I pass a variable by reference? This parameter is expanded and passed in. It is equivalent:

pixbuf = Gdk.pixbuf_get_from_window(rootwin, 0, 0, screen.width(), screen.height())
Call (caller)
Func (* sequence) Pass all objects in sequence as inpidual positional arguments
Seq = [1, 2, 3]
Func (* seq)-> func (1, 2, 3)

Func (** dict) Pass all key/value pairs in dict as inpidual keyword arguments
Dict = {'A' = 1, 'B' = 2}
Func (* dict)-> func (a = 1, B = 2)

Function-defined
Def func (* name) Matches and collects remaining positional arguments in a tuple
Func (1, 2, 3)-> name = [1, 2, 3]

Def func (** name) Matches and collects remaining keyword arguments in a dictionary
Func (a = 1, B = 2)-> dict = {'A' = 1, 'B' = 2}

See The asterisks in Chapter 18. Argumentspython have two meanings:
1. When defining a function
Generally, function parameters take a specified number of parameters, such

def func(a, b, c):    print a, b, c
We recommend a post: Understanding '*', '* args', '**' and '** kwargs' >>> Def test (* args, ** kwargs ):
... Print args, kwargs

>>> A = {"a": 1, "B": 2}

>>> Test ()
({'A': 1, 'B': 2 },){}
>>> Test (* a) # It is equivalent to test ('A', 'B ')
('A', 'B '){}
>>> Test (** a) # It is equivalent to test (a = 1, B = 2)
() {'A': 1, 'B': 2}

An asterisk is used to disassemble a sequence. If the variable itself is a dictionary, it degrades to a sequence of keys.
The two asterisks are used to disassemble and pass in the key-value pair.
Let's just consider it a syntactic sugar. PORTER: 1.2 decompress the files that can be iterated and assign values to multiple variables
This decompress is very dedicate and has a lot of elegant skills.
Summary:
(1)
No exchange of tem intermediate variables is required at all:
A, B = B,
(2)
Sometimes, you may only want to extract a part of it and discard other values. In this case, Python does not provide special syntax. However, you can use any variable name to place a placeholder, and then you can discard these variables.

>>> data = [ 'ACME', 50, 91.1, (2012, 12, 21) ]>>> _, shares, price, _ = data

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.