Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Parameter decompression by python

Shulou Source: shulou.com Published: 2022-06-01 05:18:56 10月03日 Update

This article mainly introduces python how to achieve parameter decompression, the article introduces in great detail, has a certain reference value, interested friends must read it!

Parametric decompression

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

All you have to do is print out each element in the list, separated by spaces, because the unpack operator accepts all the elements in the list and passes them as parameters, so the transformation of the above code will be printed out (1 meme 2 meme 3 pencils 4 5).

This Python technique is often used in functions to "package" all the parameters received by a method call into a variable.

For example:

The above function func can accept an unlimited number of arguments (args [0] and args [1] will 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 named person:

Person = {"name": "Paul", "age": 23, "location": "London"}

You can use the * operator to pass the dictionary to the function.

The incoming dictionary decomposes the key into a function keyword parameter and then takes that value as the actual value passed for that parameter.

For example:

These are all the contents of the article "how to decompress 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!

Tags: Parameters functions dictionaries operators operations elements keys keywords content articles between code values locations functions interests functions variables just actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Tech Info MariaDB Microsoft Huawei