[Turn] Python variable parameter, the parameter *, ** parameters, and the parameters passed *, unpacking

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

1, defines a function that takes two arguments

the print_str DEF (First, SECOND,):
Print First
Print SECOND,

IF __name__ == "__main__":
the print_str ( "the Hello", "world")
If you pass a parameter called, print_str ( "hello"), it will throw an exception

You need to explicitly tell exactly two parameters, now only one parameter to determine

TypeError: the print_str () Takes exactly 2 arguments The (1 GIVEN)
2, the above problem, 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 or more parameters the argument of the function will do there? Like java, must have, let us transform what the final parameters for the variable parameters (that is variable parameter, you can not pass)

the print_str DEF (First, SECOND, *):
Print First
Print SECOND,
this time calling: print_str ( "hello")

Output:

the Hello
()
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 a few parameters

print_str("hello","one","two","three")
输出结果:

Hello
( 'One', 'TWO', 'Three')
really starts from the variable parameters, i.e., the second parameter "one" begins, as the first element of the tuple, followed by the parameter will have become membered group elements

 

3, to sum up parameters *

When calling, starting from the * parameter, parameter passing, and automatically form tuple, cattle 13

 

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)


输出结果:
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)
输出结果:

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

printStr DEF (** Anything):
Print Anything

printStr (First =. 5, SECOND = 100)
{ 'SECOND': 100, 'First':. 5}
keyword parameter allows you to pass containing 0 or any number of parameter name parameter these keyword arguments within the function to automatically assemble a dict
6-1, the function is called, can also pass a dictionary function of the piece will unpack it would dictionary of all the key-value pairs converted into keyword arguments passed go


def printStr(first, **dict):
print str(first) + "\n"
print dict


printDic = {"name": "tyson", "age":"99"}


printStr(100, **printDic)

printStr(100, name = "tyson", age = "99") #等同于上面**printDic
 

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)

def printStr (parameter, parameter *, ** parameters):
Pass
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 scapegoat, 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

jsonify DEF (* args, ** kwargs):
indent = None
Separators = ( ',', ':')

IF current_app.config [ 'JSONIFY_PRETTYPRINT_REGULAR'] or current_app.debug:
indent = 2
Separators = ( ',', ' : ')

IF args and kwargs:
The raise TypeError (' jsonify () When undefined behavior and passed both args kwargs')
elif len (args). 1 ==: SINGLE # Directly args are passed to dumps ()
Data = args [0] # See Note here
the else:
Data = args or kwargs

return current_app.response_class (
dumps (Data, indent = indent, = Separators Separators) + '\ n-',
mimeType = current_app.config [ 'JSONIFY_MIMETYPE']
)
G, parameters ** I believe the same parameter dictionary, a pass into the time, the same will eventually be converted to dict

h, there is a big bang, and the above example f or bar, that is the tuple parameters, then I pass a dict argument, especially at this time of confusion, a great relationship with e bars, attention

Guess you like

Origin www.cnblogs.com/exmyth/p/11234696.html