Python advanced: advanced functions

Source: Internet
Author: User
A function is an encapsulation supported by Python. by splitting a large code segment into a function and calling a function at a layer, we can break down a complex task into a simple task, this decomposition can be called process-oriented programming. A function is the basic unit of process-oriented programming.

This article describes Python advanced: a detailed description of high-order functions. For more information, see

Functional programming

A function is an encapsulation supported by Python. by splitting a large code segment into a function and calling a function at a layer, we can break down a complex task into a simple task, this decomposition can be called process-oriented programming. A function is the basic unit of process-oriented programming.

Functional Programming (note that there is an extra "style")-Functional Programming, although it can also be attributed to process-oriented Programming, its idea is closer to mathematical computing.

First, we need to understand the concepts of Computer and Compute.

At the computer level, the CPU executes the command code for addition, subtraction, multiplication, division, and various condition judgment and jump commands. Therefore, the assembly language is the language closest to the computer.

Computing is exponential computing. the more abstract computing is, the farther away it is from computer hardware.

Corresponding to the programming language, the lower the language, the closer it is to the computer, the lower the abstraction level, and the higher the execution efficiency, such as the C language. the more advanced the language, the closer it is to computing and the higher the abstraction level, low execution efficiency, such as the Lisp language.

Function programming is a programming paradigm with a high degree of abstraction. functions written in pure functional programming languages do not have Variables. Therefore, any function, as long as the input is definite, the output is definite. this pure function is called with no side effects. The programming language that allows the use of variables, because the variable state inside the function is not sure, the same input may have different outputs, so this function has side effects.

One feature of functional programming is that you can pass the function itself as a parameter to another function and return a function!

Python provides partial support for functional programming. Because Python allows variables, Python is not a pure functional programming language.

High-order functions

The English name of a Higher-order function is her-order function. What is a high-level function? Let's take the actual code as an example and go into the concept step by step.

Variable can point to function

Take the abs () function built in Python for absolute value calculation as an example. to call this function, use the following code:

>>> Abs (-10) 10

But what if I only write abs?

>>> Abs

It can be seen that abs (-10) is a function call, while abs is the function itself.

To obtain the function call result, we can assign the result to the variable:

>>> X = abs (-10) >>> x10

But what if I assign the function itself to the variable?

>>> F = abs
>>> F

Conclusion: the function itself can also be assigned a value to the variable, that is, the variable can point to the function.

If a variable points to a function, can this function be called through this variable? Verify with code:

>>> F = abs >>> f (-10) 10

Successful! It indicates that the variable f has now pointed to the abs function itself. Directly calling the abs () function is the same as calling the variable f.

The function name is also a variable.

So what is the function name? The function name is actually a variable pointing to the function! For the abs () function, you can regard the function name abs as a variable, which points to a function that can calculate the absolute value!

What happens if abs points to another object?

>>> Abs = 10
>>> Abs (-10)
Traceback (most recent call last ):
File" ", Line 1, in
TypeError: 'int' object is not callable

After pointing abs to 10, you cannot call this function through abs (-10! The abs variable does not point to the absolute value function, but to an integer 10!

Of course, the actual code cannot be written like this. this is to show that the function name is also a variable. To restore the abs function, restart the Python interaction environment.

Note: Because the abs function is actually defined in the import builtins module, you must use import builtins; builtins. abs = 10 to make the point of modifying the abs variable take effect in other modules.

Input function

Since variables can point to functions and function parameters can receive variables, one function can receive another function as a parameter, which is called a higher-order function.

One of the simplest high-level functions:

Def add (x, y, f): return f (x) + f (y)

When we call add (-5, 6, abs), parameters x, y, and f receive-5, 6, and abs respectively. according to the function definition, we can deduce the calculation process as follows:

X =-5
Y = 6
F = abs
F (x) + f (y) ==> abs (-5) + abs (6) ==> 11 return 11

Verify with code:

>>> Add (-5, 6, abs) 11

Writing a high-order function allows the function parameters to receive other functions.

Summary

Passing in a function as a parameter is called a high-order function. function programming is a highly abstract programming paradigm.

Map/reduce

Python has built-in map () and reduce () functions.

If you have read the famous Google paper "MapReduce: Simplified Data Processing on Large Clusters", you can understand the concept of map/reduce.

Let's first look at map. The map () function receives two parameters. one is a function and the other is an Iterable function. map sequentially applies the input function to each element of the sequence and returns the result as a new Iterator.

For example, if we have a function f (x) = x2, we need to apply this function to a list [1, 2, 3, 4, 5, 6, 7, 8, 9], you can use map () to implement the following:

The result is 10.

Summary

When the number of parameters of a function is too large and needs to be simplified, you can use functools. partial to create a new function. This new function can fix some parameters of the original function, making it easier to call.

The above is Python advanced: detailed description of high-order functions. For more information, see other related articles in the first PHP community!

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.