Multiple formats and usages and tips for functions in Python _python

Source: Internet
Author: User
Tags anonymous closure in python

Let's explain a few concepts here.
-Position parameter: An argument set by position that implicitly saves the corresponding parameter with a tuple. Most of the time we use is by position. For example, there is a function def func (A,B,C), which calls Func (1,2,3). That is a=1,b=2,c=3
-Keyword parameters: You can set parameters through the keyword, do not care about the parameter position, implicit in the dictionary to save the formal parameters. For example, there is a function def func (a,b,c), called Func (b=1,c=2,a=3), that is, a=3,b=1,c=2

Normal format

Copy Code code as follows:

def func (Opt_args):
...
return value

functions with collection of positional parameters

Format is as follows

Copy Code code as follows:

def func (*params):
...
return value

Usage

When you use a function, instead of restricting the number of arguments, *params automatically collects incoming arguments as a tuple.

Instance

Copy Code code as follows:

def func (*params):
Print params

A = [1,2,3,4]
b = ' Hello '
c = 3
Func (A, B, c)

Output

Copy Code code as follows:

([1, 2, 3, 4], ' Hello ', 3)

Functions with collection of keyword parameters

Format is as follows

Copy Code code as follows:

def func (**params):
...
return value

Usage
**params automatically collects incoming parameters as a dictionary when a keyword is passed.

Instance

Copy Code code as follows:

def func (**params):
Print params
Func (A=1, b=2, c=3)

Output
Copy Code code as follows:

{' A ': 1, ' C ': 3, ' B ': 2}

Function Special Usage

Default parameters

Format

Copy Code code as follows:

def func (a = 1, b = 2)

The equal sign (=) number is the default, and you can call the function without having to pass the parameter to the default.
Instance
Copy Code code as follows:

def func (a = 1, b = 2):
Print A, b
Func (A=3)

Output
Copy Code code as follows:

3 2

function can return multiple values

Format

Copy Code code as follows:

Return a, B, c

Instance
Copy Code code as follows:

def func (a = 1, b = 2):
Return A, B

Print func (a=3)


Output
Copy Code code as follows:

(3, 2)

inline functions and closures

Format

Copy Code code as follows:

def foo () #外部函数
def bar () #内嵌函数
....
....

If the inline function references a variable of an external function (including external function arguments), the variable referred to is called a free variable, then the inline function is called a closure. Let's look at the professional explanation: Closure (Closure) is the abbreviation of lexical closure (lexical Closure), is a function that references a free variable. The referenced free variable will exist with this function, even if it has left the environment that created it.

Instance

Copy Code code as follows:

def foo (A, B):
x = 4
def bar ():
return x * a + B;
Return bar

f1= foo (1, 2)
f2= foo (2, 3)

Print F1 (), F2 ()


Output
Copy Code code as follows:

6 11

transfer Function

Python everything is object, function This grammatical structure is also an object, you can pass the function name as a parameter
Format

Copy Code code as follows:

def bar (*param1, **param2):
....

def foo (bar, *param1, **param2):
Bar (*param1, **param2)


Instance
Copy Code code as follows:

def bar (*param1, **param2):
Print param1
Print param2

def foo (bar, *param1, **param2):
Bar (*param1, **param2)

Foo (bar, 1, 2, 3, a =, B = 222, c = 333)


Output
Copy Code code as follows:

(1, 2, 3)
{' A ':, ' C ': 333, ' B ': 222}

anonymous functions and lambda

The lambda syntax can create an anonymous function, which is mainly about simplifying writing and is a syntactic sugar.
-Format

Copy Code code as follows:

Lambda [arg1[, arg2, ... argn]]: expression

Instance
Copy Code code as follows:

def foo (x, y):
return x + y
print "Call foo function, result is:", foo (3, 4)

Bar = lambda x = 2, y = 3:x + y
Print "Call lambda fucntion, result is:", bar (3,4)


Output
Copy Code code as follows:

Call foo function, result is:7
Call Lambda fucntion, result is:7

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.