Python-Functional programming

Source: Internet
Author: User

1 Assigning a function to a variable
>>> F = ABS
>>> F (-10)
10
2 Higher order functions
def add (x, Y, f):
return f (x) + f (Y)
The 3 map function receives two parameters, one is a function, the other is a sequence, and the incoming function functions sequentially to each element of the sequence and returns the result as a new list.

 >>> def f (x): ...  return  x * x ...  >>> map (f, [1 , 3 , 4 , 5 , 6 , 7 , 8 , 9  ]) [ , 4 , 9 , 16 , 25 , , style= "color: #800080;" >49 , 64 , 81 ] 

4 Reduce. Reduce functions a function in a sequence [X1, x2, x3 ...] , the function must receive two parameters, and reduce will continue to accumulate the result and the next element of the sequence.

def fn (x, y): ...      return x * ten + y ... >>> reduce (FN, [1, 3, 5, 7, 9])13579

5.filter:filter () also receives a function and a sequence. When different from map (), filter () applies the incoming function to each element sequentially, and then decides whether to persist or discard the element based on whether the return value is true or false.

def is_odd (n):     return n% 2 = = 1filter (is_odd, [1, 2, 4, 5, 6, 9, ten, +])#  results: [1, 5, 9, []
def Not_empty (s):     return  and S.strip () filter (Not_empty, ['A'"'B')  'C'  ])#  result: [' A ', ' B ', ' C ']

6.sorted: Sort and specify sort functions

>>> sorted ([5, 9, 5, 9, 12, 21, 36])
def reversed_cmp (x, y):     if x > y:        return -1    if x < y:        return 1    return  0>>> Sorted ([9, 5,reversed_cmp) [36, 21, 12, 9, 5]

7. function as return value

def lazy_sum (*args):    def  sum ():        = 0        for in  args:            = ax + n        return  ax    return  sum >>> f = lazy_sum (1, 3, 5, 7, 9)>>> F ()25

8. Anonymous functions

>>> map (lambda x:x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9]) [1, 4, 9, 16, 25, 36, 49, 64, 81]

The X in front of the colon represents the function parameter.

There is a limit to the anonymous function, that is, there can be only one expression, without writing return, the return value is the result of that expression.

Assigning anonymous functions to variables

Lambda x:x * x>>> F<function <lambda> at 0x10453d7d0>> >> F (5)25

Return anonymous function

def build (x, y):     return Lambda: x * x + y * y

9 Decorators

def log (func):     def Wrapper (*args, * *kw)        :print'call%s ():' % func.  __name__        return func (*args, * *kw    )return Wrapper
@log def Now ():     Print ' 2013-12-25 '

10 partial function: To fix some parameters of a function (that is, to set default values), return a new function, it is easier to call this new function.

Import functools>>> int2 = functools.partial (int, base=2)>>> int2 ('  1000000')64>>> int2 ('1010101')85
def int2 (x, base=2):    return int (x, Base)

Python-Functional programming

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.