Python variable parameter, the parameter *, ** parameters, and the parameters passed *, unpacking

1. Define a function that takes two arguments

def print_str(first, second):
    print first
    print second
 
if __name__ == "__main__":
    print_str("hello", "world")

If you pass a parameter called, print_str ( "hello"), then it must be thrown in (you need to explicitly tell exactly two parameters, now only to determine a parameter)

TypeError: print_str() takes exactly 2 arguments (1 given)

2, the above problems, we have a demand, what I want to pass a parameter is also OK (there are kinds of way is to use the default parameters), pass two parameters or more parameters there are also a function of the line? Like java, must have, let us transform what the final parameters for the variable parameters (that is variable parameter, you can not pass)

def print_str(first, *second):
    print first
    print second

At this call: print_str ( "hello")

Output:

hello 

()

You can see the string "hello" to print out, but did not pass the Senate * second, print out a tuple tuple, tuple of zero elements, because I pass a parameter

If we do more than pass several parameters: the print_str ( "the Hello", "One", "TWO", "Three")

Output:

hello 

('one', 'two', 'three')

Sure enough, from the start of the variable parameters, i.e., the second argument "one" begins, as the first element of the tuple, followed by the parameter will have become elements of the tuple.

3, to sum up parameters *

When calling, starting from * parameters of the incoming parameters, automatic composition tuple.

4, the function call, passing * parameter will be passed parameter a parameter tuple unpacking function, each element automatically as a function of

print_str (* numbers_strings), incoming * tuple and see how unpacked, the first element of the tuple, still represents the position of the first parameter

等同于print_str("1","2","3","4","5")

numbers_strings = ("1","2","3","4","5")
 
def print_str(first, *second):
    print first
    print second
 
if __name__ == "__main__":
    print_str(*numbers_strings)

Output:

1

('2', '3', '4', '5')

5, if it is not a variable parameter of the function, the function can unpack one time pass more parameters do? Is of course possible, examples facie

numbers_strings = ("1","2")
 
def print_str(first, second):
    print first
    print second
 
if __name__ == "__main__":
    print_str(*numbers_strings)

Output:

1

2

6, ashamed ah, missed ** parameters, the first parameter name two **, on behalf of keyword arguments, also known as dictionary parameters, look at the example

def printStr(**anything):
    print anything
 
printStr(first = 5, second = 100)

Output: { 'SECOND': 100, 'First': . 5}

6-1, a function call, you can pass a dictionary function of the piece will unpack it would dictionary of all the key parameters for a keyword conversion pass in

def printStr(first, **dict):
    print(first)
    print(dict)
printDic = {"name": "tyson", "age":"99"}
printStr(100, **printDic)
printStr(100, name = "tyson", age = "99") #等同于上面**printDic

Output:

100
{'name': 'tyson', 'age': '99'}
100
{'name': 'tyson', 'age': '99'}

7. Conclusions

In python, when the symbols * and ** in the simultaneous function parameters defined, a parameter indicating a list of acceptable are any number of parameters, each variable parameter

* Second to represent any number (including 0) unknown parameters, also called tuple parameters, the final type tuple (Note: Even if a transfer element, will eventually become tuple)

** anything represent any number of keyword arguments, also known as a dictionary parameter, the ultimate type of dictionary

Note: When two persons exist, then we need to put ** * second before anything (important things to say three times)

Note: When two persons exist, must be placed before the * second ** anything needs

Note: When two persons exist, must be placed before the * second ** anything needs

 

8, precautions, saying again

a, as long as the first parameter is a variable (parameter or keyword unknown parameter), parameter 0 can be transferred, a transfer may be, also can transmit a plurality of

B back, as long as the variable parameter (or parameters dictionary parameter tuples), must in general parameters (also called the position parameter)

c, * ** parameters must be in front of the parameter (parameter tuple dictionary parameters exist, a certain parameter tuple First)

d, * parameters, although called tuples parameter, you can not pass a complete tuple objects come in Kazakhstan, if it is a tuple, remember to do re-opened, or you'll be back pot, it is to pass in this * turple

e, ** parameters, although called the dictionary parameter, you can not pass a complete dictionary objects come in Kazakhstan, if it is a dictionary, do remember to also unpack, right, it is to pass in this ** dict

f, * parameter, called tuples parameter, if passed into an argument, it also went to the final composition turple, should pay attention to this point, I almost forced to look at the source code senseless today

g, ** parameters, I believe Dictionary parameters, too, a pass into the time, the same will eventually be converted to dict


Original link: https://blog.csdn.net/cadi2011/article/details/84871401

Guess you like

Origin www.cnblogs.com/USTC-ZCC/p/11599630.html