How to realize Parameter unpacking in python
This article mainly introduces how to unpack the parameters of python. It is very detailed and has a certain reference value. Friends who are interested must read it!
Parameter unpacking
Sometimes called the Splat or Scatter operator * they work when you need to decompress the parameters in the list / tuple for function calls that require separate positional arguments.
For example, the built-in range () function requires separate start and stop parameters. When writing function calls, you can use the * operator to unpack parameters from a list or tuple:
Let's define a simple list x = [1, 2, 3, 4, 5]
A common example of a unpacking operator is the use of print:
Print (* x) / / Result: 1 2 3 4 5
This simply prints out each element in the list, separated by spaces, because the unpack operator passes all the elements in the list to them as parameters, so the translation of the above code will be print.
This Python technique is often used in functions to "package" all the parameters received by the method call into a variable.
For example:
The above function func accepts an unlimited number of arguments (args [0] and args [1] provide you with the first and second arguments, respectively).
In a similar manner, dictionaries can use the * operator to pass keyword parameters.
Let's define a Python dictionary called person:
Person = {"name": "Paul", "age": 23, "location": "London"}
You can use the * operator to pass the dictionary to the function.
The incoming dictionary will decompose the key into the function keyword parameter, and then pass the value as the actual value of that parameter.
For example:
The above is all the contents of the article "how to unpack parameters in python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!