程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

5.python function advanced

編輯:Python

Famous function

Used multiple times , Will call repeatedly

def run(x,y):
print(x,y)
return x,y
run(1,2)

Anonymous functions

1 Anonymous functions : It's a function without a name
2 Why use :
For scenarios that are used only temporarily , There is no need for reuse

Definition of anonymous function

print(lambda x, y: x + y)

expression

(lambda x, y: print(x + y))(1, 2)

Anonymous function general sum max,min,sorted Continuous use

 salaries= {
'xialuo':3000000,
'xishi':30000,
'dahai':3000
}
def func(name):
return(salaries[name]
# Compare value value
# For maximum
print(max(salaries,key=lambda name:salaries[name]))
# For the minimum
print(min(salaries,key=lambda name:salaries[name]))
# trans
print(sorted(salaies,key=lambda name:salaries[name],reverse=True))
# positive sequence
print(sorted(salaies,key=lambda name:salaries[name],reverse=False))

Recursive function

1. What is a recursive function
Recursive call of function is a special form of nested call of function , In the process of calling a function, the function itself is called directly or indirectly , Call it a recursive call to a function
Recursive loops are meaningless

Recursive calls must have two explicit phases :
1. to flash back : Call it recursively again and again , To put it bluntly, it is a repeated process ,
However, it should be noted that the scale of each repeated problem should be reduced ,
Until it approaches a final result , That is, the backtracking stage must have a clear end condition
2. Recurrence : Go back layer by layer to calculate the result

Recursion is the calling of nested functions in the definition of a function
***** The difference between recursion and loop , Every time in the cycle, judge , How many times do I have to think about
Recursion only needs to determine the end condition , Repeat the call according to the rule , No need to consider the number of times

The fifth person is the 4 Personal plus 2 year

def age(n):
if n ==1:
return 18
if n>1:
return age(n-1)+2
print(age(5))

Value cycle

L = [1,[2,[3,[4,[5,[6,[7,[8,[9,]]]]]]]]]
def search(L):
for n in L:
# print(n)
if type(n) is not list:
print(' I'm the number %s'%n)
else:
search(n)
search(L)

Closure function

Closure
Closed means : This function is an internal function
What does Bao mean : It means that the internal function name is referenced externally

def outer(x,y):
def func():
print(x+y)
return func
# The same parameter is passed in every time
func1=outer(3,2)
func1()
func1()
func1()

Decorator

Decorator is a special closure function
1、 What is a decorator ( It's just a function , But this function is not for your own use , Add functions to other functions )
A tool is a tool , The function in the program is a tool with a certain function
Decorating refers to adding additional functionality to the decorated object
2、 Why use decorators
Software maintenance should follow the open and closed principle
The open and closed principle refers to :
Once the software goes online, it is closed to modify the source code , Is open to extended functionality
This uses the decorator

 There are two principles to be followed in the realization of decorators :
1、 Do not modify the source code of the decorated object ( Man's original character , Life style )
2、 Do not modify the call method of the decorated object ( The original appearance of a person , name )

example

name=' The sea '
# Decorator
def decorate(func): #func Wait a minute to pass in run
def new_func(name):#run(name) Of name
print(" Decorate the front code ")
func(name)
print(" The code behind the decoration ")
return new_func
# Decorated function
def run(name):
print('=================')
print(' I am a %s' %name)
# Run the decorator
@decorate Equivalent to run = decorate(run)
run = decorate(run)
run(name)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved