The concept of FunctionalProgramming (functional programming) originated from LISP in the first place. it was founded by John input in 1958 and first proposed the idea of automatic garbage collection, this concept has also been used for reference by PythonJavaRuby and other languages. Today, LISP has developed many dialects. Compared with object-oriented programming, one of the major advantages of functional programming is ImmutableData, which is independent of external data and does not change the value of external data, this idea can greatly reduce bugs in our code, and functional programming also supports using functions like using variables. Python as an object-oriented language
Introduction
The concept of Functional Programming (Functional Programming) originated from LISP in the first place. it was founded by John input in 1958 and first proposed the idea of automatic garbage collection, this concept has also been used for reference by Python, Java, Ruby, and other languages. Today, LISP has developed many dialects. Compared with object-oriented programming, one of the major advantages of functional programming is Immutable Data, which is independent of external Data and does not change the value of external Data, this idea can greatly reduce bugs in our code, and functional programming also supports using functions like using variables. As an object-oriented language, Python also provides support for functional programming. although it is not so pure, it does not support tail recursive optimization.
Use of lambda
Lambda is an anonymous function. using lambda rationally not only reduces the amount of code, but also better depicts the code logic. for example, we have the following function.
>>> Def f (x):... return x + x # call this function >>> f (2) 4
If we use lamda to rewrite this function, we only need a line of code.
# X after lambda indicates the parameters to be received by the lambda function, and x + x indicates the value to be returned by the lambda function >>> f = lambda x: x + x # We can see that f is also a function object >>> f
> # Call lambda functions> f (2) 4
Map usage
Map (function, iterable) receives two parameters. The first parameter indicates receiving a function, and the second parameter indicates receiving an iteralbe type object, such as list.
The principle of the map function is: 1. each time a parameter is extracted from iterable, 2. pass this parameter to our function, 3. then add the value returned by the function to a list (this statement is not accurate, just to help you understand it. I will explain it later ). After all the iterable objects are traversed, map returns the list to our caller. Next, let's take a look at the usage of map through examples.
Example1
# Use the above lambda example> function = lambda x: x + x # to define an iterable object list> iterable = [1, 2, 3, 4, 5, 6, 7, 8, 9] # function fucntion extracts a parameter x from iterable each time, and then function returns the value of x + x, # add the returned value to a new list. after iterable is traversed, map returns the newly created list. >>> V = map (function, iterable) # note that the above statement is not accurate, just to help you understand it. In fact, map returns a map object, not a list >>> v# But we can call the built-in list function to convert map into a list to get the expected result >>> list (v) [2, 4, 6, 8, 10, 12, 14, 16, 18]
Example2
For the second parameter of map, we can also pass a list of functions, that is, the list contains multiple function objects.
>>> multiply = lambda x: x * x>>> add = lambda x: x + x>>> funcs = [multiply, add]>>> list(map(lambda f: f(1), funcs))[1, 2]
Reduce usage
Like map, reduce (function, iterable) also receives two parameters. The first parameter represents receiving a function, and the second parameter represents receiving an iteralbe object, for example, list. However, the difference is that this function in reduce must receive two parameters. next we will evaluate the example of a list sum to understand how to use reduce.
From functools import reduce # define a function using lambda. the function receives two parameters and returns the sum of the two parameters >>> function = lambda x, y: x + y >>> iterable = [1, 2, 3, 4, 5, 6, 7, 8, 9] # each time a function receives two parameters, except for the first time, each time an element is taken from iterable as a parameter # Another parameter is taken from the value returned by the previous function> reduce (function, iterable) 45
Filter usage
Similar to map/reduce, filter (function, iterable) also receives two parameters at a time. one parameter is a function, and the other parameter is an iterable object. it can be seen from the name, filter is used to filter iterble objects, such as list ).
The principle is to retrieve an element from the iterable object each time to act on our function. if the function returns True, this element is retained. if the function returns False, this element is deleted. Next we will look at the usage of filter through an example.
# Define a function. if the received character s is null, False is returned. if it is not empty, True >>> function = lambda s: s and s is returned. strip () >>> iterable = ['AJ ', '', 'stussy','', 'clot', 'fcb ', None] >>> filter (function, iterable)
>>> List (filter (function, iterable) ['AJ ', 'stussy', 'clot', 'fcb ']
Decorator
Decorator is an advanced Python syntax. The decorator can process a function, method, or class. The rational use of decorator can reduce the amount of code and improve the readable type of the program. in many Python frameworks, for example, Django, we can see a large number of decorator figures.
>>> def add(x, y):... return x + y... >>> def multiply(x, y):... return x * y...
Now we have the above two functions for addition and multiplication respectively, but now we feel that the functions are not enough. we want to add some output statements before the returned results. In general, we need to refactor the two functions, as shown below.
>>> def add(x, y):... print("input:", x, y)... return x + y... >>> def multiply(x, y):... print("input:", x, y)... return x * y...
If you use the decorator, we can do the following. although we seem to have no advantage in this situation, if we want to add more than one print function, in addition to add/multiply, we also have minus/pide and other functions. at this time, the power of the decorator is reflected. we only need to modify one code, this not only improves the readability of the program, but also saves a lot of work for us to refactor the code in the future.
def decorator(F): def new_function(x, y): print("input:", x, y) return F(x, y) return new_function@decoratordef add(x, y): return x + y@decoratordef multiply(x, y): return x * y
The above is the content of the Python function programming getting started tutorial. For more information, please follow the PHP Chinese network (www.php1.cn )!