In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 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 transfer of python function parameters to you, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
The transfer of function parameters in python is through assignment. The use of function parameters has two aspects worth paying attention to: 1. How are function parameters defined? 2. How parameters are parsed in the process of calling a function
Let's take a look at the first question. There are four main ways to define function parameters in python:
1.F (arg1,arg2,...)
This is the most common way of definition. A function can define any parameter, each separated by a comma. A function defined in this way must also provide an equal number of values (actual parameters) in parentheses after the function name, and the order must be the same, that is to say, in this way of calling, the number of parameters and arguments must be the same, and must correspond one by one. That is to say, the first parameter corresponds to the first parameter. For example:
Def a (XBI y):
Print x,y
When this function is called, a (1), then x takes 1, and the shape participates in the argument 2. If a (1) or a (1) or a (1, 2, 3) will report an error.
2.F (arg1,arg2=value2,...)
This approach is an improved version of the first, providing default values
Def a (xQuery yellow3):
Print x,y
When this function is called, a (1Magne2) is also x to take 1, but if a (1), the error will not be reported. At this time, x is still 1, and the default is 3. In the above two ways, you can also change the position of the parameters, for example, a (yearly 8 ~ 4 ~ 3) can also be used in this form.
3.F (* arg1)
The above two methods are passed in as many arguments as there are parameters, but sometimes the third method is more useful if you are not sure how many parameters there are. It uses a * plus formal parameter name to indicate that the number of arguments to this function varies, either 0 or n. Note that no matter how many there are, they are stored inside the function in a tuple with the formal parameter name as the identifier.
> def a (* x):
If len (x) = = 0:
Print 'None'
Else:
Print x
> a (1)
(1,) # stored in tuples
> > a ()
None
> a (1, 2, 2, 3)
(1, 2, 3)
> a (masks 1, 2, 2, 3)
Traceback (most recent call last):
File "", line 1, in-toplevel-
A (masks 1, 2, 2, 3)
TypeError: a () got an unexpected keyword argument 'm'
4.F (* * arg1)
The formal parameter name is preceded by two * to indicate that the parameter will be stored in the dictionary with the formal name as the identifier inside the function, and the method of calling the function needs to be in the form of arg1=value1,arg2=value2.
> def a (* * x):
If len (x) = = 0:
Print 'None'
Else:
Print x
> > a ()
None
> a (xylene 1 ~ 1 ~ 2)
{'Yee: 2,' x: 1} # is stored in the dictionary
> a (1J2) # this call returns an error
Traceback (most recent call last):
File "", line 1, in-toplevel-
A (1BI 2)
TypeError: a () takes exactly 0 arguments (2 given)
Four definitions are described above, and then we will see how the function parameters are parsed during the call. In fact, just remember that the priority of the above four methods is reduced in turn, first 1, then 2, 3, and finally 4, that is, first parsing the arg in mode 1, then parsing the arg=value in mode 2, and then parsing mode 3, that is, passing the extra arguments in the form of arg into a tuple. Finally, the remaining arguments in the form of key=value are passed into a dictionary to the parameter with two asterisks, which is also method 4.
> def test (xQuinyzhenglingjiajiajiajiaob):
Print x,y,a,b
> > test (1)
1 1 () {}
> test (1Pol 2)
1 2 () {}
> test (1pm 2pm 3)
1 2 (3,) {}
> test (1, 2, 3, 4)
1 2 (3, 4) {}
> test (xalone 1, dint 2)
1 2 () {}
> > test (1)
1 1 () {'averse: 2}
> test (1, 2, 3, and 4)
1 2 (3,) {'averse: 4}
> test (1, 2, 3, and 4)
Traceback (most recent call last):
File "", line 1, in-toplevel-
Test (1, 2, 3, 3, 4)
TypeError: test () got multiple values for keyword argument 'y'
The above is the transfer of python function parameters to you, the editor 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.