Functional programming of Python

Source: Internet
Author: User

#-*-Coding:utf-8-*-#把函数作为参数传入, such functions are called higher-order functions, and functional programming refers to this highly abstract programming paradigm #python内建map, the usage of the Reduce function map (f, [X1, x2, X3, X4]) = [F (x1), F (x2), F (x3), F (x4)] Map (Lambda x,y:x+y, [1,1,1], [2,3,4]) reduce (f, [X1, x2, X3, x4]) = f (f (f (x1, x2),  x3), x4) filter (f, [1, 2, 4, 5, 6, 9, ten, +]) #筛选符合f的数据, returns the list sorted ([X1, x2, X3, X4], f) #以f的形式进行排序, returns the list #一个例子  def str2int (s): DEF fn (x, y): return x * + y def char2num (s): return {' 0 ': 0, ' 1 ': 1, ' 2 ':  2, ' 3 ': 3, ' 4 ': 4, ' 5 ': 5, ' 6 ': 6, ' 7 ': 7, ' 8 ': 8, ' 9 ': 9}[s] return reduce (FN, map (Char2num, s)) #用lambda函数进一步简化 def char2num (s): return {' 0 ': 0, ' 1 ': 1, ' 2 ': 2, ' 3 ': 3, ' 4 ': 4, ' 5 ': 5, ' 6 ': 6, ' 7 ': 7 ', ' 8 ': +/-+---+--+--+--        R2int (s): return reduce (lambda x,y:x*10+y, map (Char2num, s)) #关键字lambda表示匿名函数 (minimum function for fast definition line) (lambda x:x*2) (3)  "'" is a well-known design pattern, often used in the scene needs of the scenes, more classic have insert log, performance testing, transaction processing, Web permissions check, cache and so on. Well-known examples are coffee, sweetened coffee, and coffee with milk.  In essence, or coffee, just on the original thing, did the "decoration" to add some features or features.For example, logging, the need for some functions to record the stupid way, each function to add code, if the code changes, it is sad to urge the adorner approach, define a special log of the adorner, the need to decorate the function, the advantages of extracting a lot of functions in the function itself is independent of the functions of the same code and continue to reuse that,   Functions can be "decorated" as a completely different behavior, effectively orthogonal decomposition of business logic, such as the use of permissions and authentication from the business to generalize, the role of the adorner is to add an existing object for the additional function "'
#Essentially, decorator is a higher-order function that returns a functiondeflog (func):defWrapper (*args, * *kw):Print 'Call %s ():'% func.__name__          returnFunc (*args, * *kw)returnwrapper @log#equals now = log (now)defNow ():Print '2013-12-25'------------------------------>>>Now () Call Now ():2013-12-25#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. For example, to customize the text of a log:deflog (text):defDecorator (func):defWrapper (*args, * *kw):Print '%s%s ():'% (text, func.)__name__)              returnFunc (*args, * *kw)returnwrapperreturnDecorator @log ('Execute')#equals now = log (' Execute ') (now)defNow ():Print '2013-12-25'------------------------------>>>Now () execute now ()

There are some details to note. After the decoration (function properties such as the name of the functions will change). So when writing an adorner, it is best to add the warp of the Functools module before implementation, which preserves the name and docstring of the original function.

ImportFunctoolsdeflog (func): @functools. Wraps (func)defWrapper (*args, * *kw):Print 'Call %s ():'% func.__name__          returnFunc (*args, * *kw)returnwrapper @logdefNow ():Print '2013-12-25'  

Functional programming of Python

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.