Environment of small master switch function of the road -9-

Function can be said programming language is extremely important piece, and can significantly reduce the amount of code, the code becomes clear and concise. Let's clear the main program logic. Behind will learn decorator function is achieved through function decorators can add new functions to the function, which allows the function becomes optimized extensions. This is a beginner learning experience function. Am learning progress is slow, not enough time to learn, uncomfortable.

 

Defined functions


Function definition: function refers to a collection of statements by a name (function name) encapsulates, in order to perform this function, simply call it the function name.
Characteristics:
1. reduce duplication of code
2 so that the process becomes scalable 3. making the program easy to maintain 

'' 'Form. 1' '' 
DEF test0 (X): # function name (parameter) (if no call function parameter memory space not occupied). Unified position parameter called 
    "your function definition (document described)" 
    X + = #. 1 block 
    return x # can return multiple values, it returns a tuple (which contains a plurality of values), function or procedure may be returned (return case is the address of a function or procedure) 
'' 'in the form of 2' '' 
DEF test1 (): # pass parameters not only execute a section of code. 
    "test1" 
    Print ( 'in The testing1') 
    return 0 # may not return a value 
    print ( 'fuck off') # code is not executed, because a later return.

 

Passing function parameters


'' 'Form. 1' '' 
DEF Test3 (X, Y): 
    'testing3' 
    Print (X) 
    Print (Y) 
Test (1,2) # correspondence with the parameter, called the position parameter. 
test (y = 2, x = 1) # regardless of the order parameter, called the critical parameter 
# Note: After the key parameters must be placed in a position parameter, or easy error. 
'' 'In the form of 2' '' 
DEF Test (* args): # is not fixed when the actual arguments, parameters accepted by * is not fixed. Receiving parameters N positions, converted into the form of tuples. 
    Print (args) 
Print (Test (1,2,3,4,5,6)) # output tuples (1,2,3,4,5,6,7) 
Print (Test ([l, 2,3 , 4,5,6,7])) # output a ([1,2,3,4,5,6,7]) 
'' 'form. 3' '' 
DEF test2 (** kwargs): # acceptable N in the form of a key-value keyword parameters, converted into a dictionary form 
    Print (. 1, kwargs) 
    Print (2, kwargs [ 'name']) 
    Print (. 3, kwargs [ 'Age']) 
    Print (. 4, kwargs [ 'Sex ']) 
test2 (name =' negu ', Age = 22 is,
    Output:. 1 { 'name': 'negu', 'Age': 22 is, 'Sex': 'MALE'} 
    2 negu 
    . 3 22 is 
    . 4 MALE 
    '' 'example' '' 
DEF Test3 (name, Age = 22 is, * args , ** kwargs): # position parameter can not be written back in a keyword parameter 
    Print ( 'name =', name) 
    Print ( 'args =', args) 
    Print ( 'Age =', Age) 
    Print ( 'kwargs =' , kwargs) 
    Print ( 'Hobby =', kwargs [ 'Hobby']) 
    Print ( 'args =', args) 
    Logger ( 'Test3') 

Test3 ( 'negu', 22 is, 'haha', 'Hehe', 'Bibi ', sex =' male ', hobby =' python ') # age available position or keyword assigned parametrically

 

Local variables


Local Variables Local variables play a role in local variables defined in the subroutine (function), the global variables play a role in other places.

def change_name(name):#可以改全局变量school,前面加上一个global school(少用,用了容易导致程序混乱)
    school = "mage linux"
    print("before change",name,school)输出:before change Xiada mage linux
    name = 'NEGU' #这个函数就是这个变量的作用域,这就叫局部变量
    print('after change is %s'%name)#输出after change is NEGU
name = 'negu'
change_name('Xiada')
print(school)#学校还是iue,全局变量。
print(name) #还是negu没有改变,因为name只是一个局部变量只在函数里生效。

 

高阶函数


高阶函数的定义:在某个函数中若输入函数参数或者返回函数参数则是高阶函数。

def add(a,b,f):
    return f(a)+f(b)

res=add(3,-6,abs)#abs为内置函数,取绝对值
print(res)#输出9

 

匿名函数


#这段代码
def calc(n):
    return n**n
print(calc(10))

#换成匿名函数
calc = lambda n:n**n
print(calc(10))
#匿名函数可以和filter、map、functools.reduce联用

 

递归函数


在函数内部,可以调用其他函数。如果一个函数在内部调用自身本身,这个函数就是递归函数。
递归特性: 1. 要有一个明确的结束条件
2. 每次进入更深一层递归时,问题规模比上次递归都应该有所减少
3. 递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出。

def calc(n):
    print(n)
    if int(n/2) > 0 :
        return calc((int(n/2)))
calc(10)
输出为:
10
5
2
1

 

Guess you like

Origin www.cnblogs.com/negu/p/11349819.html