Python Day 6 (5) Python functional programming 3

Source: Internet
Author: User
Tags call back

One: Decorator

1 function object has a __name__ property, can get the name of the function

>>> def now():... print(‘2015-3-25‘)...>>> f = now>>> f()2015-3-25


 >>> now.__name__  Span class= "string" > ' now '   >>> f.__name__   ' now ' 
2 to enhance now () functions, such as automatically printing the log before and after a function call, but do not want to modify the definition of the now () function, a way to dynamically add functionality during the run of the code, called an "adorner" (Decorator). Essentially, decorator is a higher-order function that returns a function.
def log(func): def wrapper(*args, **kw): print(‘call %s():‘ % func.__name__) return func(*args, **kw) return wrapper

The above log , because it is a decorator, takes a function as an argument and returns a function. We will use the Python @ syntax to place the decorator at the definition of the function:

@logdef now(): print(‘2015-3-25‘)

Calling now() a function will not only run the now() function itself, but will also now() print a line of logs before running the function:

>>> now()call now():2015-3-25

Put @log to now() the definition of the function, the equivalent of executing a statement:

now = log(now)

Since log() it is a decorator that returns a function, the original now() function still exists, but now the variable with the same name points to the new function, and the now call executes the now() new function, which is the log() function returned in the function wrapper() .

wrapper()The function's parameter definition is (*args, **kw) , therefore, the wrapper() function can accept calls of arbitrary arguments. wrapper()inside the function, the log is printed first and then the original function is called.

3 If the decorator itself needs to pass in parameters, it is necessary to write a higher-order function that returns decorator, which is more complex to write.

 def logdef decoratordef wrapper (*args, **kw): Print ( '%s%s (): '% (text, func.__name_ _)) return func (*args, **kw) return wrapper return Decorator 
@log(‘execute‘)def now(): print(‘2015-3-25‘)

The results of the implementation are as follows:

>>> now()execute now():2015-3-25

Compared to the two-layer nested decorator, the 3-layer nesting effect is this:

>>> now = log(‘execute‘)(now)

Let's parse the above statement, execute it first, log(‘execute‘) return the decorator function, call back the function, the argument is the now function, the return value is the wrapper function.

More than 4 of the definitions of the two decorator are no problem, but the final step. Because we're talking about functions and objects, and it has __name__ properties like that, but you see the functions after the decorator decoration, and they __name__ have changed from the original ‘now‘ ‘wrapper‘ :

>>> now.__name__‘wrapper‘

Because the name of the function returned is the same, wrapper() ‘wrapper‘ so you need to copy the properties of the original function __name__ into the wrapper() function, otherwise, some code that relies on the function signature will be executed with an error.

Python built-infunctools.wraps可以解决这个问题。记住:

A import the Functools module before defining the function. Import Functools

b 在定义wrapper()的前面加上@functools.wraps(func)即可。

Two: Partial function (one of functools the functions in the module)

1在介绍函数参数的时候,通过设定参数的默认值,可以降低函数调用的难度。而偏函数也可以做到这一点。

2 int() Functions can convert a string to an integer, and when passed in only the string, the int() function defaults to decimal conversion:

int(‘12345‘)12345

But int() the function also provides additional base parameters, the default value is 10 . If you pass in base a parameter, you can do an n-binary conversion:

 >>> int ( ' 12345 ', base= 8) 5349 #把字符串当做8进制 and returns the corresponding decimal. >>> int ( ' 12345 ', 16) 74565 #把字符串当做16进制 and returns the corresponding decimal. 

 >>> import functools>>> int2 = functools.partial (int, Base=2) >>> int2 ( ' 1000000 ') 64< Span class= "Prompt" >>>> int2 ( ' 1010101 ') 85             


A simple summary of functools.partial is to fix certain parameters of a function (that is, to set default values), to return a new function, and to call this new function is simpler.
When a function has too many arguments and needs to be simplified, use functools.partial to create a new function that can fix some of the parameters of the original function, making it easier to invoke.
 
 
 
 


Python Day 6 (5) Python functional programming 3

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.