Python quick learning 09: function parameters

Source: Internet
Author: User

Preface

Series of articles:[Portal]

Continue !!

Body

We have already touched on functions. functions can be referenced (access or use other variables as their alias) and passed into functions as parameters, as well as the parameters (arguments) of the elements (functions) of the list, dictionary, and other container objects.

Transfer Function

Format parameters

Location parameters

Default parameters

Keyword variable parameters

Location Transfer

Example:

def f(a,b,c):    return a+b+cprint(f(1,2,3))

# When f is called, 1, 2, 3 are passed to a, B, and c respectively based on their locations.

Transmission of formal parameter keywords

Using location transfer will feel rigid. Key word transmission is based on the name of each parameter. Keyword does not follow the corresponding relationship of location.

Example:

print(f(c=3,b=2,a=1))

Keyword transfer can be mixed with location transfer. However, the location parameter must appear before the keyword parameter:

print(f(1,c=3,b=2))

# A unique required parameter must exist.

Default Value

When defining a function, you can assign a default value to the parameter in the form of a = 19 ). If this parameter is not passed, the default value is used.

def f(a,b,c=10):    return a+b+cprint(f(3,2))print(f(3,2,1))

# When function f is called for the first time, we do not have enough values. c is not assigned a value, and c uses the default value 10.

# When the second function is called, c is assigned a value of 1, and the default value is no longer used.

Variable Length Parameter non-Keyword variable length parameter (tuples)

When defining a function, we sometimes do not know how many parameters will be passed during the call. In this case, the packing location parameter or the packing keyword parameter is very useful for parameter transfer.

The following is an example of parcel location transfer:

def func(*name):    print (type(name))    print (name)func(1,4,6)func(5,6,7,1,2,3)

# Two Calls, although the number of parameters is different, are defined based on the same func. In the func parameter table, all parameters are collected by name and merged into a tuple according to the position. This is the transfer of the package location.

# Name is the name of the tuples used for parcel location transfer. When defining func, add the * sign before the name.

Transfer of package keywords

Example:

def func(**dict):    print type(dict)    print dictfunc(a=1,b=9)func(m=2,n=1,c=11)

# Similar to the preceding example, dict is a dictionary that collects all the keywords and passes them to the function func.

# The dict parameter is the dictionary used to pass the wrap keyword. Add ** before dict **. The key to package transfer is to add * or ** before the corresponding tuples or dictionaries when defining functions **.

Unpack the package as well.

* And ** can also be used when calling, that is, unpacking. The following is an example:

def func(a,b,c):    print a,b,cargs = (1,3,4)func(*args)

# The so-called solution package is to allow each element of the tuple to correspond to a location parameter when passing the tuple. * Is used when calling func to remind Python: I want to split args into three scattered elements and pass them to a, B, and c respectively. (Imagine that when calling func, what are the consequences of not * Before args ?)

Correspondingly, there is a solution package for the dictionary, using the same func definition, and then:

dict = {'a':1,'b':2,'c':3}func(**dict)

# When passing the dictionary dict, each key-value pair of the dictionary is passed as a keyword to func.

Summary

  Keyword, default value,

Package location, package keyword

Unpack

Thanks and Resource Sharing

    

One step on the road, I hope everyone will join me.

Thank you! I like your support. If yes, click like.

Knowledge Source: http://book.douban.com/doulist/3870144/

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.