Python Training chapter III, Functions, adorners, modules, functions of one built-in function

Source: Internet
Author: User

Directory:

function Example
Decorative Device
Module
Built-in functions
First, the function example:

1. Define the function:
def fun (args):
' Description information '
function body
return value

Define the three forms of a function:
No parameter function
def foo ():
Print (' In the Foo ')

                        foo()                         有参函数:                        def bar(x,y):                        print(‘in the bar‘)                        bar(1,2)                        空函数:                        def func():                        pass        空函数的应用示例:

def put ():
Pass

Def get ():
Pass

def CD ():
Pass

def ls ():
Pass

def auth ():
Pass
3. Call Function:
Three different forms:
Statement form:

def foo ():
Print (' In the Foo ')

Foo ()
form of an expression:

def my_max (x, y)
If X>y:
return x
Else
Return y

res = My_max (UP)
res = ten * My_max (+)
As an argument to another function: My_max (1,my_max (2,3)):
#先比较2与3的最大值, compared with 1;

4, the function of the return value of three forms:
Does not return a function:

def foo ():
Print (' In the Foo ')

res = foo ()
Print (RES)

Returns a function:

def foo ():
Return 1
res = foo ()
Print (RES)

Return multiple:

def foo ():
Return 1, ' s ', [All-you-do]
res = foo ()
Print (RES)

5, the parameters of the function:

def func (x, y): #形参在调用时才真正占用内存空间, the end of the call frees up memory space;
Print (x)
Print (y)

Func (#实参真实占用内存空间);

def func (x, y):
If type (x) is int. and type (y) is int:
Return X+y

Func (1, ' a ')

def func (x, y): #使用文档注释来提醒用户输入参数类型;
‘‘‘
‘‘‘
Return X+y

Func (1, ' a ')

def func (x:int,y:int)->int: #只是一个提示作用;
Pass
Print (func. Annotations)

The angle of the argument,
Pass Value by Location:
def foo (x, y):
Print (x, y)
Foo (ON)

Parameter by keyword: key = value
def foo (x, y):
Print (x, y)
Foo (y=2,x=1) #优点: Do not enter parameters by location;

For the same parameter, either by the location or by the keyword as a parameter to pass the value; keyword parameters must be written to the right of the position parameter;
such as: foo (1,x=1,y=2) this method will error;

From the angle of the formal parameter: position parameter, default parameter,
Variable length parameter *args;**kwargs;
Formal parameters defined by location:
def foo (x, Y, z): #位置参数, which is the required value parameter;
Print (x)
Print (y)
Print (z)

Foo (+/-)
Foo (y=2,x=1,z=3)

Parameters by default parameter:
def foo (x,y=1): #y就是默认参数; But it is also possible to assign values; The default parameter is not to use mutable types such as lists or dictionaries; must be placed after default parameters;
Print (x)
Print (y)
Foo (1)

Parameters by variable length parameter:
def foo (X,y=1,*args): #可变长参数必须放在位置参数与默认参数的后面; This condition generally does not use positional parameters;
Print (x)
Print (y)
Print (args)

Foo (1,2,3,4,54,6,3,y=2) #错
Foo (1,2,3,4,54,y=2,3,5) #错
Foo (1,2,3,4,54,6,3) #对

def foo (x, y,args):
Print (x)
Print (y)
Print (
args)

L=[' A ', ' B ']
Foo (1) #the form of args equals the 1,2,3,4,5 solution;

(' A ', ' B ')

def foo (x, Y, z):
Print (x)
Print (y)
Print (z)

Foo (+/-)

l=[1,2,3]
Foo (*l)

def foo (X,**kwargs):
Print (x)
Print (Kwargs)

Foo (1,y=3,z=2)
DIC = {' A ': 1, ' B ': 2}
Foo (1,**dic) #foo (1,a=1,b=2)

def foo (x, Y, z):
Print (x, y, z)

Foo ({' A ': 1, ' B ': 2, ' C ': 3}) #foo (a=1,b=2,c=3) to change a,b,c to X, Y, Z
Foo (
{' x ': 1, ' Y ': 2, ' Z ': 3})

Note: The default parameter, location parameter, *args, *Kwargs
1,
args equivalent to expand in accordance with the location of the way to write;
2, **kwargs equivalent to the Kwargs in accordance with the key word to write;

Functions can be passed as data

def func ()
Print (' In the fun ')

FL = Fun
FL ()

Functions can be used as parameters: higher-order functions:
def foo (x):
X ()
Foo (func)

The return value can be a function:
##########################################

Can be an element of a container type:
func_dic={
' Func ': func
}

Func_dic[' func '] ()

Can be used as the return value of a function
"""
function to accept one or more functions as input or function output (return) value is a function, we call such a function as a higher order function
"""
def foo ():
Print ("return value")

def bar (func):
return func
f = Bar (foo)
F ()
Bar (foo ())
function nesting
1. Nested calls
#嵌套函数的意义相当于把一个大需求拆成多个小需求然后组合一起, take the following example
Max2 function to do two worth of size for example, if you want to do more than 10 100, you need to combine in max4
def max2 (x, y):
return x if x > y else y

def max4 (a,b,c,d):
Res1=max2 (A, B)
RES2=MAX2 (RES1,C)
RES3=MAX2 (RES2,D)
Return Res3

Print (MAX4 (10,99,31,22))

2. Function Nesting definition
#函数的嵌套定义
Def f1 (): #第一步进入f1函数
def f2 (): #第二部f1函数体中有f2函数声明
Print (' from F2 ')
Def f3 (): #第四部f2函数体中有f3函数声明
Print (' from F3 ')
F3 () #第五部f2函数体中运行f3函数
F2 () #第三部f1函数体重运行f2内容

F1 ()

Python Training chapter III, Functions, adorners, modules, functions of one built-in function

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.