What does it mean to add an asterisk to a tuple's reference?

Source: Internet
Author: User
Tags define function
rect = (0,0,screen. ) width (),  Screen . Height ())  pixbuf=Gdk. Pixbuf_get_from_window (rootwin,*rect) What is #其中 *rect? 

Reply content:

I'm not going to use pygobject, pure discussion. asterisk parameterThis syntax.

Similar questions (may be helpful to you):
define function def func (**kwargs):p rint Kwargs call a function must be func (a=1,b=2,c=3) like this? Dict_t={' A ': 1, ' B ': 2, ' C ': 3} Can't you just pass the dictionary? For example, Func (dict_t). Is there any good way?

Python parameter knowledge (meaning of the asterisking number before the variable)



Let's get this done. The meaning of the asterisk parameter and 2 examples:

Using 1 asterisks before the parameter name is to allow the function to accept any number of positional arguments.
A 2 asterisk is an arbitrary number of keyword parameters.


Examples of positional parameters:
Suppose you have this requirement: The output of all parameters, except the first one, received by a function.



Examples of keyword parameters:
No matter how many keyword parameters you pass in, I can find it in kw.




Let's take a look at this example of your supplementary information in the question:


The following example accepts any number of positional parameters and outputs only the first
(the type of s here is a tuple)


Let's define a new tuple and throw it to the mean function later.


2 ways to use the difference:

As you can see, after using an asterisk, the tuple A is unpacked (unpack). Split into a parameter and throw it in.
The above example is equivalent to:




================== 2013-2-26 Update ================


S is a tuple, so the value cannot be modified.
I'm trying to change the value of 1th in the positional parameter to 5 and then output.


Use tuples to see:

Let's change to a list to see:
Take a look at the string:

Here we split the list, the tuple, the string into positional parameters and then pass it in.
Can see no matter how. s are tuples.
The type of this s variable where the location parameter is stored does not depend on the type of parameter passed in.




================= more casually write some examples =================:
Common functions:

Literary Function:





Mean traverse the output all positional parameters. S is a tuple, don't forget it.
Let's split the list and string.
Split tuple:


To disassemble a dictionary:


It will only pass in the [key], you cannot get the value.



====== to accept the value of the split dictionary. A function parameter is 2 asterisks. =======

This means: Throw all the keyword arguments to s (excluding positional parameters)
when using a function, When using a function,
The 2 asterisks in front of c simply represent the form of splitting C into a keyword parameter.

we split the dictionary into keyword parameters when used.
the function receives the keyword argument as well. So this won't be an error.


This is equivalent to using: (no error, haha.) ) (no error, haha.) )



If you only give 1 asterisks when you use a function:

Or do not give an asterisk:
will be the error. will be the error.


Why??

put 1 asterisks:
Putting a 1 asterisk in front of the dictionary is really the meaning of the disassembly.
1 asterisks splitting the dictionary will simply pass in the [key]. A previous example proves it.

The problem is that our function mean it only accepts the keyword parameters (review our previous definition of mean): There 's nothing inside S. Output what? Error! There's nothing inside S. Output what? Error!


do not put asterisks:
Don't put an asterisk on your behalf you throw that dictionary straight in.
The problem is that the function parameter **s accepts only the keyword argument.
s inside or nothing, error!



(All of the examples used Name of functionAnd Name of parameterThey are all casually taken, no special significance.)
It's not good to feel like an example, if you get confused.
Welcome to discuss in the comment area. :)



extended reading session:

An issue explaining the asterisk syntax on StackOverflow:
Syntax-python:once and for all. What does the Star operator mean in Python?

Problem with reference value:
Python:how do I pass a variable by reference? means that the parameter is expanded to pass in. Equivalent to:
pixbuf = Gdk.pixbuf_get_from_window(rootwin, 0, 0, screen.width(), screen.height())
Call (caller)
func (*sequence) Pass all objects in sequence as individual positional arguments
seq = [[+]
Func (*SEQ), func (1, 2, 3)

func (**dict) Pass all key/value pairs in dict as individual 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}

can refer to Chapter 18. There are two meanings of asterisks in Argumentspython
1. When defining a function
In general, the parameters of the function accept the specified number of arguments, such as
deffunc(a,b,c):    printa,b,c
Recommend an article post:understanding ' * ', ' *args ', ' * * ' and ' **kwargs ' >>> def Test (*args, **kwargs):
.. print args, Kwargs

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

>>> Test (a)
({' A ': 1, ' B ': 2},) {}
>>> Test (*a) #这里等同于test (' A ', ' B ')
(' A ', ' B ') {}
>>> Test (**a) #这里等同于test (a=1, b=2)
() {' A ': 1, ' B ': 2}

An asterisk is a sequence that is disassembled into a series, and if the variable itself is a dictionary, it will degenerate into a sequence of keys.
Two asterisks are the key-value to be disassembled.
Just think of it as a grammatical sugar. Porter: 1.2 Unzip an iterative object to assign values to multiple variables
This decompression is very dedicate, with a lot of elegant tricks.
Summarized as follows:
(1)
There is no need to exchange the TEM intermediate variables at all:
A, B =b,a
(2)
Sometimes, you might just want to unzip a part and discard the other values. For this scenario, Python does not provide a special syntax. But you can use any variable name to take a place, and then lose those variables.
>>> data = [ 'ACME', 50, 91.1, (2012, 12, 21) ]>>> _, shares, price, _ = data
  • 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.