Parameters of the python--function

Source: Internet
Author: User

When defining a function, we determine the parameter name and position of the function, the interface definition of the function is complete, and when using the parameter, we can use the default parameter, the variable parameter and the keyword parameter in addition to the required parameter, and the function can handle the various incoming data.

First, Position parameters

1. Pass the parameter by location

1 def Mymax (x, y): 2     if Else y 3     return The_max 4 5 m = Mymax (10,20)6print(m)

2. Follow the key words

1 def Mymax (x, y): 2     if Else y 3     return The_max 4 5 m = Mymax (y=20, x=10)6print(m)

3. Mixing unknown and keyword parameters

Unknown parameter must precede the keyword argument

One parameter can only be assigned one time

1 def Mymax (x, y): 2     if Else y 3     return The_max 4 5 m = Mymax (y=20)6print(m)

Second, default parameters

Attention!

1 def defult_param (a,l = []):2    l.append (a)3     print  (l) 4 5 Defult_param ('Alex')6 defult_param ('  Egon')

[' Alex ']
[' Alex ', ' Egon ']

Three, dynamic parameters

1. Dynamic parameter *args by location

1 def mysum (*args):2     the_sum = 03      for in Args:4         the_sum+=i5     return  the_sum6  7 the_sum = mysum (1,2,3,4)8print(the_sum)

2. Dynamic parameter **kwargs by keyword

1 def Stu_info (* *Kwargs):2     print(Kwargs)3     Print (kwargs['name'], kwargs['sex')  45'Alex'male')

Iv. rules for defining functions

1. Definition: Thedef  keyword begins with a space followed by the function name and parentheses (). 2. Parameters: Parentheses are used to receive parameters.    If multiple parameters are passed in, the parameters are separated by commas.    Parameters can be defined in multiple or undefined.    There are many parameters, and if you are involved in defining multiple parameters, you should always follow the definition of positional parameters,*args, default parameters, * *Kwargs order. If a parameter type defaults during the above definition, the other parameters follow the above sort 3. Note: The first line of the function statement should add a comment. 4. Function Body: function content begins with a colon and is indented. 5. Return value: return [expression] End function. Return without an expression is equivalent to returning the Nonedef function name (parameter 1, parameter 2,*args, default parameter, * *Kwargs        ): "" " Note: function function and parameter description "" "         function body         ...  return value

Parameters of the python--function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.