Python high-order functions
Filter () For filtering sequences
The first parameter receives a function that determines whether to persist based on the return value, true to leave, and false to discard
def is_odd (n): return n%2 = = 1filter (is_odd, Range (1,16))def Not_empty (s) : Return and'A' " " ])
Built-in sorted () function for list sorting
return function
The function as the return value, which involves the "closure"
The return function does not refer to any loop variables, or subsequent variables that may change.
Remember the classic loop problem, and the last number you get is the same.
anonymous function : lambda expression
An anonymous function can have only one expression , without writing a return
Map (Lambda n:n*n, Range (1,11))# The syntax for an anonymous function is to follow the function arguments after the Lambda keyword, plus a colon: no return
An anonymous function can also be used as the return value of a function
Decorative Device
In Python, everything is an object. The function is also an object, and the function object has the __name__ property
def Now (): Print ' 2016-03-23 ' = nownow. __name__ # ' Now 'f.__name__# ' Now '
The way to add functionality dynamically during code runs is called Adorner decorator, which is essentially a high-order function that returns a function.
Import Functools
#one of the simplest non-parametric adorners#define a function that can print the log as an adorner#the essence of the adorner is to return a functiondeflog (func):
@functools. Wraps (func)defWrapper (*args, * *kw):Print 'Call %s ():'%func.__name___ returnFunc (*args, * *kw)returnwrapper#the syntax for using adorners is to place them on top of other functions using the @ symbol@log#equivalent to now = log (now)defNow ():Print '2016-03-23'Now ()#an adorner that needs to pass in parametersdeflog (text):defDecorator (func):
@functools. Wraps (func)defWrapper (*args, * *kw):Print '%s%s ()'% (text, func.)__name__) returnFunc (*args, * *kw)returnwrapperreturnDecorator@log ('Execute')#equivalent to log (' Execute ') (now)defNow ():Print '2016-03-23'Now ()
In order not to modify the __name__ property of the original function, it is necessary to use the built-in functools.wraps adorner to ensure that the property is not modified.
#!/usr/bin/env python#python decorator Little Exercise#Write a @log adorner that supports both parameterless and parametricImportFunctoolsImport Timedeflog (obj):ifisinstance (obj, (str,int,float,bool)): param=objdefDecorator (func): @functools. Wraps (func)defWrapper (*args, * *kw):Print 'Call %s () begin with Param%s:'% (func.__name__, param) Res= Func (*args, * *kw)Print 'Call %s () End:'%func.__name__ returnResreturnwrapperreturnDecoratorElse: @functools. Wraps (obj)defWrapper (*args, * *kw):Print 'Call %s () begin:'%obj.__name__Res= obj (*args, * *kw)Print 'Call %s () End'%obj.__name__ returnResreturnwrapper#write an adorner to print the ' Begin call ' and ' begin end ' logs before and after function callsImportFunctoolsImport TimedefShowfunname (func): @functools. Wraps (func)defWrapper (*args,**kw):Print 'Begin Call'Res= Func (*args, * *kw)Print 'begin End' returnResreturnWrapper@showfunnamedefMyFunc ():Print 'Executing ...'MyFunc ()
Partial function
The Functools.partial () function is to fix the values of some parameters of a function, thus returning a new function, making it easier to invoke the function.
Because any function can receive the definition parameter, the variable parameter, the keyword parameter. So you can also pass in both parameters when you call the Functools.partial () method.
[Python notes] Python Learning note Three