Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the default parameter values and key parameters of python function

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you about the default parameter values and key parameters of the python function. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

Default parameter valu

For some functions, you may want some of its parameters to be optional, and if the user does not want to provide values for these parameters, these parameters use default values. This function is done with the help of default parameter values. You can specify the default parameter value by adding the assignment operator (=) and the default value after the formal parameter name of the function definition.

Note that the default parameter value should be a parameter. More precisely, the default parameter values should be immutable-which will be explained in more detail in later chapters. From now on, please remember this.

Use default parameter valu

Output

How does it work?

A function called say is used to print a string as many times as needed. If we do not provide a value, the string will be printed only once by default. We do this by assigning a default parameter value of 1 to the formal parameter times.

The first time we use say, we provide only one string, and the function prints the string only once. The second time we use say, we provide a string and parameter 5, indicating that we want to "say" the string message five times.

important

Only those parameters at the end of the formal parameter table can have default parameter values, that is, when you declare a function parameter, you cannot declare a parameter with a default value and then a parameter without a default value.

This is because the value assigned to the parameter is assigned according to the location. For example, def func (a, bread5) is valid, but def func (axi5, b) is invalid.

Key parameters

If you have a function that has many parameters and you only want to specify some of them, then you can assign values to these parameters by naming-- this is called the key parameter-- and we use the name (keyword) instead of the position (the method we've been using) to assign arguments to the function.

This has two advantages. First, because we don't have to worry about the order of parameters, it becomes easier to use functions. Second, assuming that all other parameters have default values, we can assign values to only those parameters we want.

Use key parameters

Output

How does it work?

The function named func has one parameter with no default value and two parameters with default values.

The first time you use the function, func (3,7), parameter a gets the value 3, parameter b gets the value 7, and parameter c uses the default value of 10.

The second time you use the function func (25, centering 24), you get a value of 25 based on the position variable an of the argument. According to the name, that is, the key parameter, parameter c gets the value 24. The variable b, according to the default value, is 5.

The third time we use func (caching 50, astat100), we use key parameters to fully specify the parameter values. Note that although an is defined before c in the function definition, we can still specify the value of the parameter c before a.

These are the default parameter values and key parameters of the python function. Xiaobian believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report