Functions of Python

Source: Internet
Author: User

function definition
def functionname (parameters):   #没有参数可以不写 ()    "function _ Document String"       #存放函数说明    function_suite    return [ Expression]   #没有返回值可以不加 [] content, or you can omit return
    • DEF is the keyword that defines a function
    • The document string is enclosed in three quotes.
    • The function body under the document string
Parameter passing

function parameters are defined in 4 ways, positional parameters, key parameters, tuple parameters, and dictionary parameters.

Position parameters

Parameters are passed in the order of the function parameters, and the order in which the parameters are changed is different.

def func (x, y):    print ("x=%d y=%d"% (x, y)) func (3,4) #输出: x=3 y=4func (4,3) #输出: x=4 y=3
Key parameters

The key parameter is that when passing parameters, you do not have to take into account the specific order of formal parameters, in the form of "formal parameter = actual parameter", tell the interpreter directly, you want to pass the actual parameter to which argument.

def func (x, y):    print ("x=%d y=%d"% (x, y)) func (y=3,x=4) #output: x=4 y=3
Tuple delivery

Tuple parameters are mainly used in the actual business logic in the case of "variable parameters", that is, you can not accurately tell the number of arguments. This is the time to use the "variable parameters" strategy. The essence of a tuple parameter is to encapsulate several arguments into a tuple parameter, thus realizing the requirement of changing the number of parameters in a single pass. For example, when the parameter of a function represents the Pisa ingredient, the type of the ingredient will vary depending on the Pisa type. You can then use the "tuple parameters" policy.

def func (*toppings):    print (toppings) func (' mushrooms ', ' extra ', ' cheese ') func (' mushrooms ', ' extra ') #output:(' Mushrooms ', ' extra ', ' cheese ') (' mushrooms ', ' extra ')
Dictionary delivery

This strategy is used to pass a dictionary, in the form of a key-value pair.

def func (**peoples):    Print (Peoples) func (Name= ' Aaron ', age=25) #output: {' name ': ' Aaron ', ' Age ': 25}

As can be seen from the Code "func (name= ' Aaron ', age=25)", this is easily confused with the method of "key parameter". The key to the separation is that the name here represents the key in the dictionary, not the formal parameter in the function. If the key in the dictionary is the same as the parameter of the function, then the interpreter interprets it as "key parameter" rather than a dictionary parameter, so avoid the same parameters as other parameters when defining the key of the dictionary.

def func (name,**people):    print (name)    print (People) func (name= ' Aaron ', age= ', city= ' Beijing ') #output: aaron{' age ': ' + ', ' City ': ' Beijing '}
Parameter passing Mode priority problem

When the interpreter parses a function parameter, it parses the parameter in the order of the positional parameter, the key parameter, the tuple parameter, and finally the dictionary parameter. This explains the problem of "key parameters" and "dictionary transfer" which are easy to confuse.

def test (x,y=1,*a,**b):    print (X,Y,A,B) test (1) #output: 1 1 () {}test) #output: 1 2 () {}test () #output: 1 2 (3,) {}test (1,2,3,4) #output: 1 2 (3, 4) {}test (x=1,y=2) #output: 1 2 () {}test (1,a=2) #output: 1 1 () {' A ': 2}test (1,2,3,a=4) #outpu T:1 2 (3,) {' A ': 4}test (1,2,3,y=4) #output: Traceback (most recent call last): File "<pyshell#52>", line 1, In-topleve L-test (1,2,3,y=4) #output: typeerror:test () got multiple values for keyword argument
Delivery list Issues

There are three ways, which are positional parameters, key parameters, and meta-group arguments, respectively. However, in a tuple argument, you need to use the list method to convert the tuple to a list.

Position parameters
def func (names):    print (names)    names[1] = ' Jujinyi ' names = [' Aaron ', ' Jim ']func (names) print (names) #output: [' Aaron ', ' Jim ' [' Aaron ', ' Jujinyi ']

Passing the list in the above way is the original of the list, so it will change the value of the list permanently. If you want to pass only the attachments to the list, you can do so by using the slice method.

def func (names):    print (names)    names[1] = ' Jujinyi ' names = [' Aaron ', ' Jim ']func (names[:]) print (names) #output: [' Aaron ', ' Jim '] [' Aaron ', ' Jim ']
Key parameters

It is essentially the same as the positional parameter, but it is just a display of the arguments. Of course, the effect on an argument is the same, and it permanently affects the value of the argument.

def func (num,names):      print ("num=%d"%num)      print (names)      names[1] = ' Jujinyi ' names = [' Aaron ', ' Jim ']func ( NAMES=NAMES,NUM=25) print (names) #output: num=25[' Aaron ', ' Jim ' [' Aaron ', ' Jujinyi ']
Meta-group Transfer
def func (*toppings):    toppings = list (toppings)    print (toppings) func (' mushrooms ', ' extra ', ' cheese ') func (' Mushrooms ', ' extra ') #output: [' mushrooms ', ' extra ', ' cheese ' [' mushrooms ', ' extra ']

Problems with passing dictionaries

There are also three ways to pass a dictionary, namely positional parameters, key parameters, and dictionary arguments.

Position parameters
def func (names):     print (names)     names[' name '] = ' Jujinyi ' people = {' name ': ' Aaron ', ' age ': ' + '}func (people) Print (People) #output: {' name ': ' Aaron ', ' age ': 25}{' name ': ' Jujinyi ', ' Age ': 25}
Key parameters
def func (num,names):     print ("num=%d"%num)     print (names)     names[' name '] = ' Jujinyi ' people = {' name ': ' Aaron ' , ' age ': '}func ' (names=people,num=25) print (people) #output: num=25{' name ': ' Aaron ', ' age ': 25}num=25{' name ': ' Jujinyi ', ' Age ': 25}
Dictionary reference
def func (**people):     print (People) func (name= ' Aaron ', Age= ') #output: {' name ': ' Aaron ', ' Age ': 25}
return value

To get the result of the function execution, you can return the result using the return statement, note that:

    • The function stops executing and returns the result as soon as it encounters a return statement, so can also be understood as a return statement that represents the end of the function
    • If return is not specified in the function, the return value of this function is None

Functions of Python

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.