Home  >  Article  >  Backend Development  >  Detailed analysis of Python functional programming (code examples)

Detailed analysis of Python functional programming (code examples)

不言
不言Original
2018-09-18 15:27:111600browse

This article brings you a detailed analysis (code example) of Python functional programming. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Functional Programming, functional programming. Python provides partial support for functional programming. For pure functional programming, for any function, as long as the input is certain, the output is certain, which can be said to have no side effects.

1. Higher-order functions

1. Variables point to functions

We know that the calculation results of functions can be assigned to variables , for example x = abs(-5).
Similarly, variables can also point to functions, such as f = abs.
If a variable points to a function, then we directly call abs(x) and the result returned by executing f(x) is exactly the same.

2. The function name is also a variable

We can also regard the function name as a variable, such as the abs() function. Execute the statement abs=-5, and then call abs(-5) and an error will be reported, because at this time the variable abs no longer points to the absolute value function. Instead, it points to an integer -5.

3. Function as formal parameter

Since variables can point to functions and functions can also act as variables, then a function can receive another function as its own form Parameters, that is, implementing higher-order functions. For example:

>>> def add(x , y ,  f ): # 把函数作为参数传入。
...     return f(x) + f(y)
>>> add (-5,-2,abs)
7

2. Built-in functions of functional programming

1. map(func,seq)

  • Syntax: map(func,seq), receives two parameters, one is the function func and the other is the iterable object.

  • Function: Apply the passed func function to each element of the seq sequence in a loop and return a new iterable object.

  • Note: The map() function returns a <map object>, we can use the list() function to obtain The results are returned in list form.

For example: use map() to implement f(x)=x*x

>>> def f(x):
...     return x*x
...
>>> map(f,[1,2,3,4,5])
<map object at 0x0327F670>
>>> list(map(f,[1,2,3,4,5]))     # 传入的函数f作用于序列的每个元素调并用list()
[1, 4, 9, 16, 25]

Of course, you can also use other methods to achieve it:
exp1:list( map((lambda x:x*x),[1,2,3,4,5]))
exp2:[x*x for x in [1,2, 3,4,5]]

2. reduce(func,list)

  • Syntax: reduce(func,list), must accept two parameters.

  • Action: Apply the func function to the list sequence[x1,x2,x3, ...]. Take out the first two elements of the sequence x1 and x2, apply it to func, take a single value, such as a, and then a does the func operation with the next element of the sequence x3, and repeats this process. The effect is equivalent toreduce(f,x1,x2,x3)=f(f(x1,x2),x3)

  • Note: To use reduce(), you must import the module from functools import reduce

You can find a chestnut to play with~~ ~

3. filter()

  • Syntax: filter(func,[sequence]), receives a "filter" function, and a sequence. What is returned is an Iterator iterable object.

  • Function: Filter the sequence according to the func rules, filter()Put the incoming function func Acts on each element accordingly, and then retains the corresponding element as True according to the return value TrueorFalse.

  • Note: Similar to map(), filter() returns an iterable object, so You need to use list() to view the obtained results and return the list type.

For example: in a list, delete even numbers and keep odd numbers.

>>> def is_odd(n):
...     return n % 2 == 1  # 返回0(false) 或1(True)
...
>>> list( filter(is_odd,[1,2,3,4,5,6,7,8,9]) )
[1, 3, 5, 7, 9]

The above is the detailed content of Detailed analysis of Python functional programming (code examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn