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

How to customize functions and Infinite parameters in Python

2025-04-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how to customize functions and infinite parameters in Python. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Why do you need functions?

If you are asked to express 1 + 2, beginners can write the following code:

This is not particularly remarkable, but next time it will still be the addition of two values, but I hope that two values can be easily modified:

To facilitate modification, just define two variables to represent two values, at the top of the code

Now, however, if I want to execute in another place, then the variable name can not be changed at will, which is very bad.

At this point, we prefer to have something that combines two variables and the additive behavior between variables into one thing, which is a function:

Line 1:def function name (parameter 1, parameter 2): this defines a function

Line 2: the behavior in the function is to "add an and b and return the result". Reutrn returns a value, which can return anything, such as lists, tuples, dictionaries, etc., which we have learned before, here we just return the result of a + b (which is a numeric value)

Line 4: this function is actually called, and the function name () indicates that a function is executed. The parentheses here are very important.

Now that mysum is a function that represents the addition of two values, you can call him anywhere to get the result:

Before, I made a special emphasis on combining two variables and the additive behavior between variables into one thing.

As you can see, although the parameters in the mysum function are also called an and b, they are only valid in the function and will not be affected by external variables of the same name.

The function looks good now, but it's still a long way from what we expected. If we want to sum three numbers, we have to write:

This is too bad!

Infinite parameter

We want the mysum function to accept any number of values and then return the sum of those values.

Any number? This is a little similar to the list. For example:

Line 3RV x = x + n, which first executes the x + n to the right of the equal sign, and the result is assigned to the variable x

Line 5: the x accumulated after traversal at this time is the sum of the list values

So, the way to get the mysum function to accept countless numbers is:

This may seem like a solution, but the code is ugly when called (see line 8 above)

How beautiful the previous call to mysum (1J2) is.

Is there a way for Python to collect the parameters passed in by mysum for me and put them in a list?

Look at the expression of this mechanism:

Line 1: add an asterisk (*) before the parameter nums to indicate that any parameter can be passed in when calling, all of which are collected into a list parameter

Now, mysum is a nice custom function.

On a whim, you try that ugly call before:

Unexpectedly, the report was wrong.

It doesn't seem to matter, but most of the time we take a list of values and want to call our mysum function and expect it to give me the correct result directly.

Unpack

Is there an operation that automatically breaks down the elements in a list into individual elements?

The solution is still an asterisk, which is a coincidence:

When calling, add an asterisk (*) before the list to indicate that the list is disassembled.

It's a little confusing. Remember this picture:

The above is how to customize functions and infinite parameters in Python shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to 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

Internet Technology

Wechat

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

12
Report